ExecuteNonQuery requires an open and available Connection. The connection's current state is closed
My Code :
public bool Insertcustomer()
{
try
{
SqlCommand cmd = new SqlCommand("Insertcustomermaster", dal.con);
cmd.Parameters.Add("@customercode", SqlDbType.Int).Value = customercode;
cmd.Parameters.Add("@customername", SqlDbType.NChar).Value = customername;
cmd.Parameters.Add("@address1", SqlDbType.NChar).Value = address1;
cmd.Parameters.Add("@address2", SqlDbType.NChar).Value = address1;
cmd.Parameters.Add("@phoneno", SqlDbType.Int).Value = phoneno;
cmd.Parameters.Add("@mobileno", SqlDbType.Int).Value = mobileno;
cmd.Parameters.Add("@mailid", SqlDbType.NChar).Value = mailid;
cmd.Parameters.Add("@website", SqlDbType.NChar).Value = website;
cmd.Parameters.Add("@occupation", SqlDbType.NChar).开发者_运维知识库Value = occupation;
cmd.Parameters.Add("@status", SqlDbType.Bit).Value = status;
cmd.CommandType = CommandType.StoredProcedure;
return Convert.ToBoolean(cmd.ExecuteNonQuery()); //Error
}
catch (Exception ex)
{
throw ex;
}
}
App Config :
im new in c# .net..im not able to find this error..anybody help..
You should open the connection and close it .
string connectionString = "";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
//do your coding
con.Close();
The error message says what's wrong. The connection is not open, but closed.
Ensure the connection is opened. Use the Open()
-Method to open a connection.
BTW, get rid of the try/catch-block. What's the point in catching an exception and throw it again?
From your code you would need to open the connection specified in the SqlCommand declaration (dal.con
)
You should open it with
dal.con.Open()
before you ExecuteNonQuery but remember to close it after (dal.con.Close()
).
I would suggest changing
return Convert.ToBoolean(cmd.ExecuteNonQuery());
to
dal.con.Open();
bool result = Convert.ToBoolean(cmd.ExecuteNonQuery());
dal.con.Close();
return result;
dal.con
hasn't been set or opened or whatever. As per the error message.
Could you post more information regarding the connection string?
Try this in the Webconfig:
<connectionStrings>
<add name="connName" connectionString="//ConnectionString here"/>
</connectionStrings>
In the Class:
public static SqlConnection GetConnection()
{
string conString = ConfigurationManager.ConnectionStrings["connName"].ConnectionString;
SqlConnection con = new SqlConnection(conString);
return con;
}
And instead of this:
SqlCommand cmd = new SqlCommand("Insertcustomermaster", dal.con);
cmd.Parameters.Add("@customercode", SqlDbType.Int).Value = customercode;
cmd.Parameters.Add("@customername", SqlDbType.NChar).Value = customername;
cmd.Parameters.Add("@address1", SqlDbType.NChar).Value = address1;
cmd.Parameters.Add("@address2", SqlDbType.NChar).Value = address1;
cmd.Parameters.Add("@phoneno", SqlDbType.Int).Value = phoneno;
cmd.Parameters.Add("@mobileno", SqlDbType.Int).Value = mobileno;
cmd.Parameters.Add("@mailid", SqlDbType.NChar).Value = mailid;
cmd.Parameters.Add("@website", SqlDbType.NChar).Value = website;
cmd.Parameters.Add("@occupation", SqlDbType.NChar).Value = occupation;
cmd.Parameters.Add("@status", SqlDbType.Bit).Value = status;
cmd.CommandType = CommandType.StoredProcedure;
return Convert.ToBoolean(cmd.ExecuteNonQuery()); //Error
}
catch (Exception ex)
{
throw ex;
}
Do this:
//Method + Insert string (Guessing you want to create a register page)
try
{
SqlCommand cmd = new SqlCommand(sql, GetConnection());
//You don't have to point out the data type if it's just going to be filled in like that.
cmd.Parameters.AddWithValue("@customercode", customercode);
cmd.Parameters.AddWithValue("@customername", customername);
cmd.Parameters.AddWithValue("@address1", address1);
cmd.Parameters.AddWithValue("@address2", address2);
cmd.Parameters.AddWithValue("@phoneno", phoneno);
cmd.Parameters.AddWithValue("@mobileno", mobileno);
cmd.Parameters.AddWithValue("@mailid", mailid);
cmd.Parameters.AddWithValue("@website", website);
cmd.Parameters.AddWithValue("@occupation", occupation);
cmd.Parameters.AddWithValue("@status", status);
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
// You can catch it with an error, or Just leave it empty
string msg = "Error";
msg += ex.Message;
throw new Exception(msg);
}
//Close the connection (Not Necessary)
GetConnection().Close();
}
精彩评论