[英]ExecuteNonQuery C# & SQL Server
Here is my error: 这是我的错误:
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code System.Data.dll中出现“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理
Additional information: The parameterized query '(@username nvarchar(3),@password nvarchar 附加信息:参数化查询'(@ username nvarchar(3),@ password nvarchar
This is my table: 这是我的表:
CREATE TABLE [dbo].[User]
(
[Username] VARCHAR (250) NOT NULL,
[Pasword] VARCHAR (MAX) NOT NULL,
[FName] VARCHAR (MAX) NOT NULL,
[LName] VARCHAR (MAX) NOT NULL,
[Location] VARCHAR (MAX) NOT NULL,
[Profesion] VARCHAR (MAX) NOT NULL,
[email] VARCHAR (MAX) NOT NULL,
[gender] VARCHAR (MAX) NOT NULL,
[money] INT NOT NULL,
[property] VARCHAR (MAX) NULL,
PRIMARY KEY CLUSTERED ([Username] ASC)
);
And this is the C# code: 这是C#代码:
if (Page.IsValid)
{
SqlCommand NewUser = new SqlCommand("INSERT INTO [User] Values (@username,@password,@name,@lastname,@location,@profesion,@email,@gender,@money,@pro);", c);
NewUser.Connection = c;
NewUser.Parameters.AddWithValue("@username", txtuser.Text);
NewUser.Parameters.AddWithValue("@password", txtpass.Text);
NewUser.Parameters.AddWithValue("@name", txtFName.Text);
NewUser.Parameters.AddWithValue("@lastname", txtLName.Text);
NewUser.Parameters.AddWithValue("@location", ddlcountry.SelectedItem.Text);
NewUser.Parameters.AddWithValue("@profesion", txtprofession.Text);
NewUser.Parameters.AddWithValue("@email", txtemail.Text);
NewUser.Parameters.AddWithValue("@gender", rbgendere.SelectedItem.Text);
NewUser.Parameters.AddWithValue("@money", 0);
NewUser.Parameters.AddWithValue("@pro", null);
Session["CurentUserid"] = txtuser.Text;
c.Open();
NewUser.ExecuteNonQuery();
c.Close();
Session["Conect"] = (bool)true;
Response.Redirect("Finish Had Member.aspx", true);
}
Please help 请帮忙
I suspect the problem is that you're specifying null
as the value for @pro
. 我怀疑问题是你指定null
作为@pro
的值。 From the docs for AddWithValue
: 从AddWithValue
的文档:
Use
DBNull.Value
instead ofnull
, to indicate a null value. 使用DBNull.Value
而不是null
来指示空值。
However, I would also suggest avoiding using AddWithValue
to start with - call Add
and specify the parameter type, then set the Value
property. 但是,我还建议避免使用AddWithValue
开始 - 调用Add
并指定参数类型,然后设置Value
属性。 For example: 例如:
NewUser.Parameters.Add("@money", SqlDbType.Int).Value = 0;
See this blog post for detailed reasoning behind doing this. 有关执行此操作的详细原因,请参阅此博客文章 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.