0

I have One Users table, columns: id, name, surname. I am trying to make simple insert functionality, but when I submit form I get Cannot insert the value NULL into column ‘name’, table ‘database_name.dbo.Users’; column does not allow nulls. INSERT fails.

I am following this tutorial.

<%@ 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>

Please help, Thanks in advance!

3
  • The value is what i have entered in name input (not empty) Commented Jul 12, 2017 at 9:05
  • Have you disabled viewstate? Commented Jul 12, 2017 at 10:12
  • @mjwills No, I didn't Commented Jul 12, 2017 at 10:25

3 Answers 3

0

Hiii,

1.you can change the schema of your database , you can set Identity value and increase the identity seed to +1. enter image description here

2.you can manually assign some values for the data column ID.

3.You can remove select allow nulls in your table.

Sign up to request clarification or add additional context in comments.

4 Comments

This is one way, I guess it depends if the issues is that it won't allow an insert of NULL, or if it is trying to insert NULL
ID field is not an issue, it is auto-incrementing. The problem is name and surname fields when I call store method it is not getting those values and I don't know why.
I don't want name and surname to be null
Check if the textbox names are correct. Or remove the textbox and put a new one, give a specific name and try again.
0

Change:

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

to:

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

and change:

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

to:

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

This is needed since asp:FormParameter needs a <form> and your HTML lacks one as is.

Comments

0

Don't know why but when I merge Master and child pages to a single one, it works fine. Thanks for your help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.