我的代码:
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配置:
我是新的c#.net..im无法找到此错误..有人帮助..
答案 0 :(得分:3)
您应该打开连接并关闭它。
string connectionString = "";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
//do your coding
con.Close();
答案 1 :(得分:1)
错误消息说明错了。连接未打开,但已关闭。
确保已打开连接。使用Open()
- 方法打开连接。
答案 2 :(得分:1)
从您的代码中,您需要打开SqlCommand声明中指定的连接(dal.con
)
你应该用
打开它dal.con.Open()
在您执行ExecuteNonQuery之前但请记住在(dal.con.Close()
)之后关闭它。
我建议改变
return Convert.ToBoolean(cmd.ExecuteNonQuery());
到
dal.con.Open();
bool result = Convert.ToBoolean(cmd.ExecuteNonQuery());
dal.con.Close();
return result;
答案 3 :(得分:0)
dal.con
尚未设置或打开或其他任何内容。根据错误消息。
答案 4 :(得分:0)
您可以发布有关连接字符串的更多信息吗?
在Webconfig中尝试:
<connectionStrings>
<add name="connName" connectionString="//ConnectionString here"/>
</connectionStrings>
在班级中:
public static SqlConnection GetConnection()
{
string conString = ConfigurationManager.ConnectionStrings["connName"].ConnectionString;
SqlConnection con = new SqlConnection(conString);
return con;
}
而不是这个:
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;
}
这样做:
//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();
}