Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my code is below there is a error while running(i.e) connection was not closed,the connection's current state is open.

What I have tried:

C#
private void button2_Click(object sender, EventArgs e)
        {
            con.Open();
            
            OleDbCommand delcmd = new OleDbCommand();
            //if (dataGridView1.Rows.Count > 1 && dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count-1)
            if(dataGridView1.SelectedRows.Count > 0 && dataGridView1.SelectedRows[0].Index!=dataGridView1.Rows.Count-1)
            
            {
                delcmd.CommandText = "delete from table123 where Idno=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "";
               
                delcmd.Connection = con;
                delcmd.ExecuteNonQuery();
                con.close();
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                MessageBox.Show("Row Deleted");
                bind();
                clear();
            }
}
Posted
Updated 22-Feb-17 20:31pm
v2
Comments

1 solution

The close for the connection is embedded within an if condition, move it outside of the if as it will only close if the condition is true and you should close the connection regardless of the condition.

C#
private void button2_Click(object sender, EventArgs e)
        {
            //if (dataGridView1.Rows.Count > 1 && dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count-1)
            if(dataGridView1.SelectedRows.Count > 0 && dataGridView1.SelectedRows[0].Index!=dataGridView1.Rows.Count-1)
            
            {
                using (OleDbCommand delcmd = new OleDbCommand())
                {
                   delcmd.CommandText = "delete from table123 where Idno=@ID";
               
                   delcmd.Connection = con;
                   delcmd.Parameters.Add("@ID", dataGridView1.SelectedRows[0].Cells[0]);

                   con.Open();
                   delcmd.ExecuteNonQuery();
                   con.close();
                }
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                MessageBox.Show("Row Deleted");
                bind();
                clear();
            }

}


Improved answer, no need to open connection if nothing to do, added proper parameterised string.
 
Share this answer
 
v4
Comments
Graeme_Grant 23-Feb-17 6:35am    
Wouldn't it be better to use recommend a Try...Finally? That way if an error is thrown, the connection is automatically closed.
Michael_Davies 23-Feb-17 6:38am    
That is up to the programmer, his original code would only close the connection if the condition was true, thus leaving it open, the rest is down to experience.
Richard Deeming 23-Feb-17 15:56pm    
Even better would be to create the connection as a local variable, rather than as a field. Then you can wrap it in a using block. :)

But the SQL Injection[^] vulnerability is a much bigger concern.

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

  Print Answers RSS


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