1

我创建了一个注册页面,我有一个带有 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());
        }




    }
}
4

3 回答 3

1

Id您的数据库表的属性不是自动生成的,因此它需要一个值,但您在插入期间没有提供它。因此,要么让您的Id属性标识自动生成,要么在插入期间提供一个值。例如给Id1

string insertQuery = "insert into [Table] (Id,UserName,Email,Password) values (@id,@Uname,@email,@password);";
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.AddWithValue("@id",1);
com.Parameters.AddWithValue("@Uname",TextBoxUN.Text);
com.Parameters.AddWithValue("@email", TextBoxEmail.Text);
com.Parameters.AddWithValue("@password", TextBoxPass.Text);
于 2016-12-27T04:33:19.577 回答
1

错误说明了一切,

您正在尝试将数据插入表中,并且您的表中有一个作为“id”的列,它是一个主键。作为主键的列不能包含 null 这就是您收到此错误的原因

从您的代码中插入唯一 id 或为列 id 设置自动生成,设置自动生成将为每条新记录自动插入 is,这将解决您的问题。

于 2016-12-27T04:38:16.887 回答
0

首先,我必须感谢您提供格式良好的第一篇文章,然后感谢您对参数化查询的出色使用。

当我们查看错误时,这意味着您的列中将有一[id][Table]设置为非空但不是自动递增。因此,您应该为该列提供一个值以便插入一行。您可以通过两种方式解决此问题:

  • 通过将该字段更改为自动增量列;您可以使用 alter 命令通过以下查询将 id 字段修改为 autoIncrement:

     ALTER TABLE [Table] MODIFY [ID] INT IDENTITY
    
  • 或者通过在插入查询中为该列提供一个值,在这种情况下,查询将如下所示:

    string insertQuery = "insert into [Table] (Id,UserName,Email,Password) values (id,@Uname,@email,@password);";
    
于 2016-12-27T04:36:36.200 回答