Link to home
Start Free TrialLog in
Avatar of ITHelper80
ITHelper80

asked on

ASP.NET Item.Insert Null Value

How can I add a NULL value to a dropdown list that is Dataound?

I have tried
Items.Insert(0, New ListItem("", String.Empty))
Items.Insert(0, New ListItem("", Nothing))

but the records are not being inserted as NULLS
Avatar of Luis Pérez
Luis Pérez
Flag of Spain image

If the dropdown list is databound, try to insert the null/empty value not directly to the dropdownlist, but on the data source. For example, if your data source is a Dataset obtained from database query, insert a new empty file in the dataset itself before to databind to the dropdownlist. That will work for sure.
Avatar of BalkisBr
BalkisBr

You can only use .Add() in this case.

try this:
YourDropDown.Items.Add(new ListItem())

[]'s
Hi,

The following code snippet works.
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Name");
            
            DataRow dr;
 
            dr = dt.NewRow();
            dr[0]= 1;
            dr[1]="mathews";
            dt.Rows.Add(dr);
 
            dr = dt.NewRow();
            dr[0]=2;
            dr[1]="Julie";
            dt.Rows.Add(dr);
 
            dr = dt.NewRow();
            dr[0]=3;
            dr[1]="King";
            dt.Rows.Add(dr);
            
            dr=dt.NewRow();
            dr[0]=4;
            dr[1]="Romeo";
            dt.Rows.Add(dr);
 
            dr=dt.NewRow();
            dr[0]=5;
            dr[1]="Mose";
            dt.Rows.Add(dr);
 
            DropDownList1.DataSource = dt;
            DropDownList1.DataTextField = "Name";
            DropDownList1.DataValueField = "ID";
            DropDownList1.DataBind();
 
            DropDownList1.Items.Insert(0, new ListItem(string.Empty, string.Empty));

Open in new window

But there is a limitation when you try to insert into the database. An empty string will be insert into the database. This is because the SelectedValue of the DropDownList will return a string.

To handle this you have to handle in the code behind.

If you have a typed dataset, automatically Visual Studio will create a function named

DataSet1.DataTable1.SetColumnNull();


>>>but the records are not being inserted as NULLS
Why do you need to insert a NULL value in a DropDownList? You can insert an empty string in DDL, not null. Maybe I misunderstood your problem?
Avatar of ITHelper80

ASKER

Well Im fine with inserted an empty string but the problem is I am running a SELECT query against the database and I dont know how to filter out the empty string....

WHERE [TableName] IS NOT NULL doesnt work
You have to write a stored procedure like this.

Create Procedure dbo.sp_InsertValue(@EName varchar(150) = null)
AS
Begin
             Insert into [TableName] values (@EName)
END

and on the page code behind you have to add the parameter @EName  to the SqlCommand object only when the selected index of the dropdown list  > 0

or other way

Create Procedure dbo.sp_InsertValue(@EName varchar(150))
As
Begin
     IF len(@EName)>0
         Insert into [TableName[ Values (@EName)
    Else
        Insert into [TableName] Values (null)
END
I was able to solve this problem by using a different method. Thanks to all who offered a suggestion
ASKER CERTIFIED SOLUTION
Avatar of ITHelper80
ITHelper80

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial