Here is my code where i want to insert data into the database if "testheader" is not null.
and I get error
"
ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.
"
when I tried it without any validation then data inserted successfully.
What I have tried:
c# code....
public bool InsertCustomizeRowtwo(string cocode,string dept, string testcode,string testname,string testheader,string sub1,string sub2,string sub3,string sub4,string sub5,string sub6,string sub7,string sub8, string sub9,string sub10)
{
if (testheader == "")
testheader = null;
try
{
//connection
theConnection = new SqlConnection();
if (conString != "")
{
theConnection.ConnectionString = conString;
theConnection.Open();
}
//command
theCommand = new SqlCommand();
theCommand.Connection = theConnection;
if (testheader != null)
{
theCommand.CommandText = "insert into PH_TestMaster_CustRowtwo(COMPCODE,DEPARTMENT,TESTCODE,TESTNAME,TEST_HEADER,TEST_SUB1,TEST_SUB2,TEST_SUB3,TEST_SUB4,TEST_SUB5,TEST_SUB6,TEST_SUB7,TEST_SUB8,TEST_SUB9,TEST_SUB10) VALUES('" + cocode + "','" + dept + "','" + testcode + "','" + testname + "','" + testheader + "','" + sub1 + "','" + sub2 + "','" + sub3 + "','" + sub4 + "','" + sub5 + "','" + sub6 + "','" + sub7 + "','" + sub8 + "','" + sub9 + "','" + sub10 + "')";
}
else
{
theConnection.Dispose();
theCommand.Dispose();
}
theCommand.CommandType = CommandType.Text;
theCommand.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
finally
{
theConnection.Dispose();
theCommand.Dispose();
}
}