[英]C# Registration
我的项目C#遇到问题,我想注册用户,但不起作用! 有人知道我该如何解决这个问题,谢谢。
errorSystem.Data.SqlClient.SqlException(0x80131904):无法将值NULL插入表'D:\\ ASP_WORKSPACES \\ LOGIN \\ LOGIN \\ APP_DATA \\ DATABASE.MDF.dbo.User'的列'Id'中; 列不允许为空。 INSERT失败。 该语句已终止。 àSystem.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔值breakConnection,操作
1 wrapCloseInAction) à System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction)àSystem.Data.SqlClient.TdsParser .ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)àSystem.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObjectState.à FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,字符串resetOptionsString,布尔值isInternal,布尔型forDescribeParameterEncryption) Re 尝试,SqlDataReader ds,布尔describeParameterEncryptionRequest)àSystem.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔returnStream,字符串方法,TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) à System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource
1完成,字符串methodName,布尔sendToPipe,Int32超时,布尔&usedCache,布尔asyncWrite,布尔inRetry)àSystem.Data.SqlClient.SqlCommand.ExecuteNonQuery()àRegistration.Button1_Click(对象发送方,EventArgs e)dans D:\\ asp_workspaces \\ login \\ login \\ Registration.aspx.cs:ligne 49 ClientConnectionId:b0d4edc1-2ade-4417-8d9d-7f6e8befc17e错误编号:515,状态:2,类:16
Register.aspx.cs的代码
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();
string insertQuery = ("insert into [User](username,email,password,country)values (@username,@email,@password,@country)");
SqlCommand cmd = new SqlCommand(insertQuery, conn);
cmd.Parameters.AddWithValue("@username", username.Text);
cmd.Parameters.AddWithValue("@email", email.Text);
cmd.Parameters.AddWithValue("@password", password.Text);
cmd.Parameters.AddWithValue("@country", country.SelectedItem.ToString());
cmd.ExecuteNonQuery();
Response.Write("User Successfully Registered");
conn.Close();
}
catch (Exception ex)
{
Response.Write("error" + ex.ToString());
}
}
Registration.aspx
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td class="auto-style3">User name</td>
<td class="auto-style4">
<asp:TextBox ID="username" runat="server" Height="22px" Width="180px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="username" ErrorMessage="Username is required." ForeColor="#FF3300"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style3">E-mail</td>
<td class="auto-style4">
<asp:TextBox ID="email" runat="server" Width="180px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="email" ErrorMessage="Email is required." ForeColor="#FF3300"></asp:RequiredFieldValidator>
<br />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="email" ErrorMessage="You must enter the valid email." ForeColor="#FF3300" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="auto-style3">Password</td>
<td class="auto-style4">
<asp:TextBox ID="password" runat="server" TextMode="Password" Width="180px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="password" ErrorMessage="Password is required." ForeColor="#FF3300"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style3">Confirm Password</td>
<td class="auto-style4">
<asp:TextBox ID="confpassword" runat="server" TextMode="Password" Width="180px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="confpassword" ErrorMessage="Confirm Password is required." ForeColor="#FF3300"></asp:RequiredFieldValidator>
<br />
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="password" ControlToValidate="confpassword" ErrorMessage="Password must be some" ForeColor="#FF3300"></asp:CompareValidator>
</td>
</tr>
<tr>
<td class="auto-style3">Country</td>
<td class="auto-style4">
<asp:DropDownList ID="country" runat="server" Width="180px">
<asp:ListItem>Select country</asp:ListItem>
<asp:ListItem>USA</asp:ListItem>
<asp:ListItem>UK</asp:ListItem>
<asp:ListItem>Germany</asp:ListItem>
<asp:ListItem>France</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="country" ErrorMessage="Select the country name." ForeColor="#FF3300" InitialValue="Select country"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style5">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
<input id="Reset1" type="reset" value="reset" /></td>
<td> </td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style5"> </td>
<td> </td>
</tr>
</table>
</div>
</form>
在Visual Studio 2017中查询创建数据库SQL
CREATE TABLE [dbo].[User] (
[Id] INT NOT NULL DEFAULT 1,
[username] NCHAR (20) NULL DEFAULT u,
[email] NCHAR (20) NULL DEFAULT e,
[password] NCHAR (20) NULL DEFAULT p,
[country] NCHAR (20) NULL DEFAULT c,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
错误非常明显,您不能在列Id
插入null
。 最简单的解决方案是更改表定义,以使该列为IDENTITY
列,当您插入新行时,该列会自动递增值。 因此,将您的定义更改为:
CREATE TABLE [dbo].[User] (
[Id] INT NOT NULL IDENTITY(1, 1),
[username] NCHAR (20) NULL,
[email] NCHAR (20) NULL,
[password] NCHAR (20) NULL,
[country] NCHAR (20) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
您有一个表Users,其ID列不能为空值(如Error所解释)。
您要么需要通过插入查询为ID列发送一些值,例如
Insert into [User] ([Id], [username], …) values (@id, @username……)
并以与添加其他用户值相同的方式添加参数。 或使数据库列ID为ID,增量Seed value =1
。
假设Id是您的主键列,则分配default =1
并不是一个很好的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.