ASP.NET 无法将 NULL 值插入列“名称”
ASP.NET Cannot insert the value NULL into column ‘name’
我有一个 Users
table,列:id
、name
、surname
。我正在尝试实现简单的插入功能,但是当我提交表单时,我得到 Cannot insert the value NULL into column ‘name’, table ‘database_name.dbo.Users’;列不允许空值。插入失败。
我正在学习 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>
请帮忙,提前致谢!
嗨,
1.you 可以更改数据库的架构,您可以设置身份值并将身份种子增加到 +1。
2.you可以手动为数据列ID赋值
3.You 可以删除 select 允许 table.
中的空值
变化:
<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 缺少一个。
不知道为什么,但是当我将主页面和子页面合并为一个页面时,它工作正常。感谢您的帮助!
我有一个 Users
table,列:id
、name
、surname
。我正在尝试实现简单的插入功能,但是当我提交表单时,我得到 Cannot insert the value NULL into column ‘name’, table ‘database_name.dbo.Users’;列不允许空值。插入失败。
我正在学习 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>
请帮忙,提前致谢!
嗨,
1.you 可以更改数据库的架构,您可以设置身份值并将身份种子增加到 +1。
2.you可以手动为数据列ID赋值
3.You 可以删除 select 允许 table.
中的空值变化:
<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 缺少一个。
不知道为什么,但是当我将主页面和子页面合并为一个页面时,它工作正常。感谢您的帮助!