8
Answers

Using Statement in C#

Pankil Bhatt

Pankil Bhatt

8y
5.7k
1
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)