Pankil Bhatt

Pankil Bhatt

  • 1.3k
  • 489
  • 77.2k

Using Statement in C#

Jun 5 2016 7:34 AM
string constr = ConfigurationManager.ConnectionStrings["SampleAppConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "delete FROM [DetailTable] where ID = '" + prodID +"'";
cmd.Connection = con;
cmd.ExecuteNonQuery();
}
 
ERROR :

ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.

while I am Using statement needs to write connection open () and close()??
 
which is the better why to use the using statement?
 
 

Answers (8)