尝试/ Catch方法错误ASP.NET(Try/Catch method error ASP.NET)
我正在努力完成这项工作。 我希望它检查插入后是否存在记录,但它总是返回一个错误:第1行:'nvarchar'附近的语法不正确。 有人能指出我的声明有什么不对吗? 此外,如果你有更好的尝试捕捉方法,请更多地启发我。 刚刚在ASP.NET中编程
提前致谢。
protected void Page_Load(object sender, EventArgs e) { string connString_LibrarySystem = "Server=DEVSERVER;User ID=sa;Password=Sup3r-Us3r;Database=LibrarySystem"; string strSQL = "INSERT INTO TblBooks (bookid, booktitle, lastname, firstname, description, categoryid, dateadded, statusid, quantity, isdeleted) VALUES (@bookid, @booktitle, @lastname, @firstname, @description, @categoryid, @dateadded, @statusid, @quantity, @isdeleted)"; SqlConnection conn = new SqlConnection(connString_LibrarySystem); conn.Open(); SqlCommand cmd = new SqlCommand(); cmd = new SqlCommand(strSQL, conn); cmd.Parameters.AddWithValue("@bookid", Request.Form["bookid"]); cmd.Parameters.AddWithValue("@booktitle", Request.Form["booktitle"]); cmd.Parameters.AddWithValue("@lastname", Request.Form["lastname"]); cmd.Parameters.AddWithValue("@firstname", Request.Form["firstname"]); cmd.Parameters.AddWithValue("@description", Request.Form["description"]); cmd.Parameters.AddWithValue("@categoryid", Request.Form["categoryid"]); cmd.Parameters.AddWithValue("@dateadded", Request.Form["dateadded"]); cmd.Parameters.AddWithValue("@statusid", Request.Form["statusid"]); cmd.Parameters.AddWithValue("@quantity", Request.Form["quantity"]); cmd.Parameters.AddWithValue("@isdeleted", Request.Form["isdeleted"]); cmd.ExecuteNonQuery(); { conn.Close(); } statuslabel.Text = "Insert successful"; }编辑:刚才删除了数据类型。
I'm trying to make this work. I want it to check if a record exist after inserting but it always return an error: Line 1: Incorrect syntax near 'nvarchar'. Can someone point out to me whats wrong in my declaration? Also if you have a better try catch method please enlighten me more. Just new to programming in ASP.NET
Thanks in advance.
protected void Page_Load(object sender, EventArgs e) { string connString_LibrarySystem = "Server=DEVSERVER;User ID=sa;Password=Sup3r-Us3r;Database=LibrarySystem"; string strSQL = "INSERT INTO TblBooks (bookid, booktitle, lastname, firstname, description, categoryid, dateadded, statusid, quantity, isdeleted) VALUES (@bookid, @booktitle, @lastname, @firstname, @description, @categoryid, @dateadded, @statusid, @quantity, @isdeleted)"; SqlConnection conn = new SqlConnection(connString_LibrarySystem); conn.Open(); SqlCommand cmd = new SqlCommand(); cmd = new SqlCommand(strSQL, conn); cmd.Parameters.AddWithValue("@bookid", Request.Form["bookid"]); cmd.Parameters.AddWithValue("@booktitle", Request.Form["booktitle"]); cmd.Parameters.AddWithValue("@lastname", Request.Form["lastname"]); cmd.Parameters.AddWithValue("@firstname", Request.Form["firstname"]); cmd.Parameters.AddWithValue("@description", Request.Form["description"]); cmd.Parameters.AddWithValue("@categoryid", Request.Form["categoryid"]); cmd.Parameters.AddWithValue("@dateadded", Request.Form["dateadded"]); cmd.Parameters.AddWithValue("@statusid", Request.Form["statusid"]); cmd.Parameters.AddWithValue("@quantity", Request.Form["quantity"]); cmd.Parameters.AddWithValue("@isdeleted", Request.Form["isdeleted"]); cmd.ExecuteNonQuery(); { conn.Close(); } statuslabel.Text = "Insert successful"; }EDIT: There just removed the datatypes.
最满意答案
您不必在插入语句中包含数据类型。 跳过它们。
尝试
string strSQL = "INSERT INTO TblBooks (bookid, booktitle, lastname, firstname, description, categoryid, dateadded, statusid, quantity, isdeleted) VALUES (@bookid, @booktitle , @lastname, @firstname, @description, @categoryid, @dateadded , @statusid , @quantity, @isdeleted)";You don't have to include data type in insert statements. Skip them.
Try
string strSQL = "INSERT INTO TblBooks (bookid, booktitle, lastname, firstname, description, categoryid, dateadded, statusid, quantity, isdeleted) VALUES (@bookid, @booktitle , @lastname, @firstname, @description, @categoryid, @dateadded , @statusid , @quantity, @isdeleted)";更多推荐
发布评论