Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP.NET Item.Insert Null Value

Posted on 2009-04-14
9
Medium Priority
?
584 Views
Last Modified: 2012-05-06
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
0
Comment
Question by:ITHelper80
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 24138526
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.
0
 
LVL 3

Expert Comment

by:BalkisBr
ID: 24138564
You can only use .Add() in this case.

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

[]'s
0
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24138921
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

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24138975
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();


0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24139082
>>>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?
0
 
LVL 6

Author Comment

by:ITHelper80
ID: 24139298
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
0
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24139483
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
0
 
LVL 6

Author Comment

by:ITHelper80
ID: 24144303
I was able to solve this problem by using a different method. Thanks to all who offered a suggestion
0
 
LVL 6

Accepted Solution

by:
ITHelper80 earned 0 total points
ID: 24144308
Found different solution that worked for my purposes
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question