ASP.NET无法将值NULL插入列'name'

时间:2017-07-12 08:29:30

标签: c# asp.net sql-server webforms sqldatasource

我有一个Users表,列:idnamesurname。我正在尝试制作简单的插入功能,但是当我提交表单时,我得到无法将值NULL插入列'name',表'database_name.dbo.Users';列不允许空值。 INSERT失败。

我正在关注this教程。

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" 
AutoEventWireup="true" CodeBehind="test.aspx.cs" 
Inherits="Spravochnik.Users.test" %>

<asp:Content ContentPlaceHolderID="BodyPlaceHolder" runat="server">

<script runat="server">
    private void store(object source, EventArgs e) {
        SqlDataSource1.Insert();
    }
</script>

<asp:SqlDataSource 
    ID="SqlDataSource1" 
    runat="server" 
    DataSourceMode="DataReader" 
    ConnectionString="Data Source=DESKTOP-FJR7LSU;Initial Catalog=spravochnik;Integrated Security=True"
    SelectCommand="SELECT * FROM Users"
    InsertCommand="INSERT INTO Users (name, surname) VALUES (@Name, @Surname)">
        <InsertParameters>
            <asp:FormParameter Name="Name" FormField="NameBox" />
            <asp:FormParameter Name="Surname" FormField="SurnameBox" />
        </InsertParameters>
</asp:SqlDataSource>

<asp:TextBox ID="NameBox" runat="server" />
<asp:RequiredFieldValidator
    ID="RequiredFieldValidator1"
    runat="server"
    ControlToValidate="NameBox"
    Display="Static"
    ErrorMessage="Please Enter User's Name." /><br />

<asp:TextBox ID="SurnameBox" runat="server" />
<asp:RequiredFieldValidator
    ID="RequiredFieldValidator2"
    runat="server"
    ControlToValidate="SurnameBox"
    Display="Static"
    ErrorMessage="Please Enter User's Surname." /><br />

<asp:Button ID="Button" runat="server" Text="Add" OnClick="store" />


</asp:Content>

的Site.Master

<%@ Master Language="C#" AutoEventWireup="true" 
CodeBehind="Site.master.cs" Inherits="Spravochnik.Site" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="~/Public/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="~/Public/css/stylesheet.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="head" runat="server" />
</head>
<body>
<form runat="server">
    <div class="container">
        <div style="padding-top:50px">
            <asp:ScriptManager runat="server">
                <Scripts>
                    <asp:ScriptReference Path="~/Public/js/jquery.js" />
                    <asp:ScriptReference Path="~/Public/js/bootstrap.min.js" />
                </Scripts>
            </asp:ScriptManager>
            <asp:ContentPlaceHolder ID="BodyPlaceHolder" runat="server" />
        </div>
    </div>
</form>
</body>
</html>

请提前帮助 谢谢

3 个答案:

答案 0 :(得分:0)

HIII,

1.您可以更改数据库的架构,可以设置Identity值并将标识种子增加到+1。 enter image description here

2.您可以手动为数据列ID分配一些值。

3.您可以删除表格中的select allow null。

答案 1 :(得分:0)

变化:

<asp:TextBox ID="NameBox" runat="server" />

为:

<form id="form1" runat="server">
<asp:TextBox ID="NameBox" runat="server" />

并改变:

<asp:Button ID="Button" runat="server" Text="Add" OnClick="store" />

为:

<asp:Button ID="Button" runat="server" Text="Add" OnClick="store" />
</form>

这是必需的,因为asp:FormParameter需要<form>而您的HTML缺少一个。

答案 2 :(得分:0)

不知道为什么,但是当我将主页和子页合并到一个单独的页面时,它工作正常。谢谢你的帮助!