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();
}
}
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?



Surya KantRank #958 · 2.1K reputationPosted Jun 5, 2016, 10:20 PM EDT
Deeksha PanditRank #1,956 · 748 reputationPosted Jun 5, 2016, 5:09 AM EDT
Rajeesh MenothRank #118 · 18.4K reputationPosted Jun 5, 2016, 5:43 AM EDT
gnana bhaskar4 reputationPosted Aug 29, 2016, 11:03 PM EDT
Vinay SinghRank #604 · 4K reputationPosted Aug 29, 2016, 11:14 PM EDT
SqlConnection con = new SqlConnection(constr)
con.Open();
SqlCommand cmd = new SqlCommand()
cmd.CommandText = "delete FROM [DetailTable] where ID = '" + prodID +"'";
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
}
Bikesh SrivastavaRank #108 · 20.3K reputationPosted Aug 30, 2016, 2:45 AM EDT
Tapan PatelRank #614 · 3.9K reputationPosted Aug 30, 2016, 12:49 PM EDT