我创建了一个注册页面,我有一个带有 ID 主键的表,但我收到此错误: 在此处输入图像描述
无法将值NULL插入'Id'列,表'D:\课程学习\动态网站设计\课程设计1\APP_DATA\REGISTRATION.MDF.dbo.Table';列不允许空值。插入失败。该语句已终止。
我刚开始学习 C#,真的不知道如何解决这个问题。
这是我的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class Registration : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();
string chekuser = "select count(*) from [Table] where UserName='" + TextBoxUN.Text + "'";
SqlCommand com = new SqlCommand(chekuser, conn);
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
if (temp == 1)
{
Response.Write("The username is exsist");
}
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();
string insertQuery = "insert into [Table] (UserName,Email,Password) values (@Uname,@email,@password);";
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.AddWithValue("@Uname",TextBoxUN.Text);
com.Parameters.AddWithValue("@email", TextBoxEmail.Text);
com.Parameters.AddWithValue("@password", TextBoxPass.Text);
com.ExecuteNonQuery();
Response.Redirect("Manager.aspx");
Response.Write("Your registration is successful!");
conn.Close();
}
catch (Exception ex)
{
Response.Write("Error:"+ex.ToString());
}
}
}