Sheffer Fernandes

Sheffer Fernandes

  • NA
  • 15
  • 3.2k

how to insert value in access db using datagridview checkbx

Jan 21 2015 12:38 PM
Im trying to insert values in ms acess db using datagridview checkbox .
I want to insert attendance recordsto the db so i have written the code such that the user can mark the attendance of current date ,and if he trys to insert attendance for the same ppl on the same date then i want to show error
ill show my code but the error is that im not able to insert the values , im getting an error saying connection open,when infact i want the connection open only

code:
foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells[1].Value != null)
                {
                    connect.Open();
                    OleDbCommand command = new OleDbCommand();
                    command.Connection = connect;
                    string query = "select WorkerName from AttendanceTable  where AttendanceDate= Date()";

                    command.CommandText = query;

                    OleDbDataReader reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        string workername = reader["WorkerName"].ToString();

                        reader.Close();
                        if (DateTime.Now.ToString() != workername)
                        {
 

                        if ((Boolean)row.Cells[1].Value == true)
                        {
                            try
                            {

                                string a = Convert.ToString(row.Cells[0].Value.ToString());
                                int b = Convert.ToInt32(row.Cells[1].Value = -1);

                                command.CommandText = "insert into AttendanceTable(WorkerName,AttendanceDate,AtWork) values('" + a + "','" + dateTimePicker1.Text + "','" + b + "')";

                                command.ExecuteNonQuery();

                            }

                            catch (Exception ex)
                            {
                                MessageBox.Show("Error" + ex);

                            }

                        }

                        if ((Boolean)row.Cells[1].Value == false)
                        {
                            try
                            {


                                string a = Convert.ToString(row.Cells[0].Value.ToString());
                                int b = Convert.ToInt32(row.Cells[1].Value = 0);



                                command.CommandText = "insert into AttendanceTable(WorkerName,AttendanceDate,AtWork) values('" + a + "','" + dateTimePicker1.Text + "','" + b + "')";


                                command.ExecuteNonQuery();

                            }

                            catch (Exception ex)
                            {
                                MessageBox.Show("Error" + ex);

                            }



                        }


                        connect.Close();
                    }
                    }
                }
            }
            MessageBox.Show("Data Saved");

Answers (7)