Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试/捕获方法错误ASP.NET_C#_Asp.net_Try Catch - Fatal编程技术网

C# 尝试/捕获方法错误ASP.NET

C# 尝试/捕获方法错误ASP.NET,c#,asp.net,try-catch,C#,Asp.net,Try Catch,我正在努力使这一切顺利进行。我想让它检查插入后是否存在记录,但它总是返回一个错误:第1行:“nvarchar”附近的语法不正确。有人能指出我的申报有什么问题吗?另外,如果你有一个更好的尝试捕捉方法,请启发我更多。刚刚开始在ASP.NET中编程 提前谢谢 protected void Page_Load(object sender, EventArgs e) { string connString_LibrarySystem = "Server=DEVSER

我正在努力使这一切顺利进行。我想让它检查插入后是否存在记录,但它总是返回一个错误:第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";
        }

编辑:刚刚删除了数据类型

您不必在insert语句中包含数据类型。跳过它们

试一试


去掉“值”列表中的数据类型。您不需要它们。

不要将类型放入您的值中

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 )";

为了实现这一点,您需要从sqlstring变量中删除数据类型

我可能会切换到使用存储过程,然后加载参数,因为这基本上就是您在这里使用addwithvalue命令所做的

另外,
cmd.ExecuteNonQuery()
将返回一个int,告诉您已成功添加了它。如果它返回一个1,你就知道它完成了。

首先,删除SQL中的类型,您不需要它们(正如其他答案所建议的那样)

其次,通过以下方式向查询中添加参数:

cmd.Parameters.AddWithValue("@bookid", Request.Form["bookid"]);

您不确定该请求。表单[“bookid”]不为空,这将导致您当前的问题

好吧,现在我有一个错误:准备好的语句“(@bookid nvarchar(4000),@booktitle nvarchar(4000),@lastname nvar”需要参数@bookid,但未提供该参数。将检查我是否遗漏了一些内容。。谢谢。我想我就快到了。
cmd.Parameters.AddWithValue("@bookid", Request.Form["bookid"]);