Elvis
Elvis

Reputation: 29

Cannot insert the value NULL into column when I try to insert it from text box but works fine when I insert it directly

I am not able to INSERT the data into database when I try to insert the data from the text box with ID="USERNAME" that is by selecting the VALUES as @USERNAME e.g.:InsertCommand="INSERT INTO [Table12] ([USERNAME]) VALUES (@USERNAME)". But the same works absolutely fine if I don't opt to insert the data from the above text box.

I am getting below error if I were to use the above code in for INSERT statement.

"Cannot insert the value NULL into column 'USERNAME', table 'D:\LOGINCONTROL\LONGINCONTROLTEM\LONGINCONTROLTEM\APP_DATA\ASPNET-LONGINCONTROLTEM-20150905150101.MDF.dbo.Table12'; column does not allow nulls. INSERT fails. The statement has been terminated."

Below statement works fine but not the above, as in the text "SOME VALUE" is inserted into the database correctly, once I click the button, which proves that there is no connection issue here.

e.g.:InsertCommand="INSERT INTO [Table12] ([USERNAME]) VALUES ('SOME DATA')".

Below is the complete code that I am using.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="INPUTTODATABASE.aspx.cs" Inherits="LongincontrolTem.INPUTTODATABASE" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="testtest" runat="server">

      User Name:   <asp:TextBox ID="USERNAME" value="" runat="server" ></asp:TextBox>

        <asp:Button ID="SaveData" runat="server" Text="InsertData" OnClick="SaveData_Click" />


        <asp:SqlDataSource ID="InsertInformation" runat="server" DataSourceMode="DataSet"
          ConnectionString="Data Source=DECTO-PC;AttachDbFilename=|DataDirectory|\aspnet-LongincontrolTem-20150905150101.mdf;Integrated Security=True"
         InsertCommand="INSERT INTO [Table12] ([USERNAME]) VALUES (@USERNAME)"
          ProviderName="System.Data.SqlClient" >


<InsertParameters>
        <asp:Parameter Name="USERNAME" Type="Char" />

    </InsertParameters>

        </asp:SqlDataSource>


    </div>
    </form>
</body>
</html>

// **code behind page**
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace LongincontrolTem
{
    public partial class INPUTTODATABASE : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void SaveData_Click(object sender, EventArgs e)
        {
            SqlDataSource DataSourced = (SqlDataSource)FindControl("InsertInformation");



            DataSourced.Insert();
        }
    }
}

Upvotes: 0

Views: 316

Answers (1)

Michael McMullin
Michael McMullin

Reputation: 1460

It looks like there are a couple of minor changes needed to get this working:

First, your InsertParameters should refer to your TextBox, as this is what you need to supply the value. Also, you probably need a type of String rather than Char. Try replacing that block with something like:

<InsertParameters>
    <asp:ControlParameter Name="USERNAME" Type="String" ControlID="USERNAME" />
</InsertParameters>

Next, your SaveData_Click event can be simplified:

protected void SaveData_Click(object sender, EventArgs e)
{
    InsertInformation.Insert();
}

Upvotes: 1

Related Questions