I created a registration page and I have a table with a Primary key of ID,but I get this error: enter image description here
Cannot insert the value NULL into column 'Id', table 'D:\课程学习\动态网站设计\课程设计1\APP_DATA\REGISTRATION.MDF.dbo.Table'; column does not allow nulls. INSERT fails. The statement has been terminated.
I am just started to learn C# and really don't know how to fix this problem.
This is my code.
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());
}
}
}