C# 将行插入数据库时发生异常
我编写了以下代码,以便在数据库中插入一行:C# 将行插入数据库时发生异常,c#,asp.net,C#,Asp.net,我编写了以下代码,以便在数据库中插入一行: SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["earchConnectionString"].ConnectionString); //string insCmd = "Insert into [node] (title, parent_id, oid, display, linkById, customLinks, contentType)
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["earchConnectionString"].ConnectionString);
//string insCmd = "Insert into [node] (title, parent_id, oid, display, linkById, customLinks, contentType) values (@title, @parent_id, @oid, @display, @linkById, @customLinks, @contentType)";
string insCmd = "Insert into [node] (title) values (@title)";
SqlCommand insertUser = new SqlCommand(insCmd, con);
insertUser.Parameters.AddWithValue("@title", TextBoxTitle.Text);
try
{
insertUser.ExecuteNonQuery();
con.Close();
Response.Redirect("addNode.aspx");
Label1.Text = "update success";
}
catch (Exception er)
{
Label1.Text = er.StackTrace;
}
堆栈跟踪:
at System.Data.SqlClient.SqlConnection.GetOpenConnection(String method)
at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Admin_addNode.Button1_Click(Object sender, EventArgs e)
in c:\inetpub\web1\Admin\addNode.aspx.cs:line 52
有人知道问题出在哪里吗?在执行ExecuteNonQuery()之前先调用con.Open()
我们可以得到异常类型和消息以及堆栈跟踪吗?“有人知道问题出在哪里吗?”-是的,您必须在catch
子句中设置断点并查找异常。现在只打印堆栈跟踪,这是相关的,但不显示实际的异常。设置断点,检查异常并在web上搜索它。如果你找不到解决办法,可以问一个如何解决的问题,展示你的尝试。欢迎@hkinterview!顺便说一句,程序员还需要睡觉:D@JohnWoo-真的!我不知道:)@MahmoudGamal现在你知道了:D连电脑都会睡觉。呵呵
con.Open();
insertUser.ExecuteNonQuery();