Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello Guys, i hope you will be fine well i am here with new issue am facing that is i want to select values from database and pass that value in data grid view to data table
the problem is it's working fine but when I add 1 or more items so with new values previous one values also added in data grid view and second is when I delete single of data grid view so,i get an error on delete button

What I have tried:

Delete button
C#
private void button7_Click_1(object sender, System.EventArgs e)
{
    foreach (DataGridViewRow item in dataGridView1.Rows)
    {
        if (bool.Parse(item.Cells[1].Value.ToString()))
        {
            con_string.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = con_string;
            command.CommandText = "Delete from [Runtime] Where [Values] = " + item.Cells[0].Value.ToString() + "";
            command.ExecuteNonQuery();
            con_string.Close();
        }
con_string.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0;Data Source =|DataDirectory|\Restaurant.accdb;Persist Security Info=False";
            DataSet dsa = new DataSet();
            DataTable dt = new DataTable();
            dsa.Tables.Add(dt);
            OleDbDataAdapter da = new OleDbDataAdapter();
            da = new OleDbDataAdapter("SELECT * FROM [Runtime] where [column3] = '"+dineinno.ToString()+"'", con_string);
            da.Fill(dt);
            //dataGridView3.DataSource = dt;
            foreach (DataRow item in dt.Rows)
            {
                int n = dataGridView1.Rows.Add();
                dataGridView1.Rows[n].Cells[2].Value = item["Column3"].ToString();
                dataGridView1.Rows[n].Cells[0].Value = item["Values"].ToString();
                
            }
            dt.Clear();
            con_string.Close();
Posted
Updated 15-Jun-17 7:34am
v2
Comments
RickZeeland 15-Jun-17 12:53pm    
And what exactly is the error message you are getting ? Can you tell also on what line the error occurs ?

1 solution

I guess that you are accessing a deleted and so invalid data.

I am not sure, but the last line
C++
con_string.Close();
looks like a closing a closed connection.

Isnt there an error message? What about debugging your code for finding the error code-line and Google for a minute?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900