.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to Remove Empty Item(s) From The DropDownList?

Posted By:Ghanashyam Nayak       Posted Date: September 12, 2010    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

Remove Empty Item(s) From The DropDownList At Run Time...

This Article About Drop Down List.

When You Bind Data in The Drop Down List At That Time May Be Some Values In That Field Are Empty, So At The Time Of Binding It Will Also Bind The Empty Values In The Drop Down List.

What The Solution Of This?

A : After Binding The Data in Drop Down List, Check One By One Values Of The Drop Down List & If Find Any Item As Empty Then Remove That Item From The Drop Down List.

Here It May Be Possible That Some Student Don't Have Mobile Number So At That Time In The Database It Is Empty....

01.private void button1_Click(object sender, EventArgs e)
03.    OleDbConnection Conn = newOleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=<>");
04.    try
05.    {
06.        Conn.Open();
07.        OleDbDataReader dr;
08.        string str = "SELECT MobileNo FROM Student";
09.        OleDbCommand cmd = new OleDbCommand(str, Conn);
10.        dr = cmd.ExecuteReader();
11.        while (dr.Read())
12.        {
13.            //It Will Add MobileNo To The DropDownList
14.            DropDownList1.Items.Add(dr.GetValue(0).ToString());
15.        }
16.        Conn.Close();
17.    }
18.    catch (Exception ex)
19.    {
20.        MessageBox.Show(ex.ToString());
21.    }
24.    //Now Check For Empty Value From The DropDownList...
25.    for (int i = 0; i < DropDownList1.Items.Count;i++ )
26.    {
27.        //Here It Will Check Fro the Empty value...
28.        if (DropDownList1.Items[i].Value == "")
29.        {
30.            //If Found Then Remove That Value From The DropdoenList...
31.            DropDownList1.Items.Remove("");
32.        }
33.    }

 Subscribe to Articles


Further Readings:

Author: Deco         Company URL: http://www.dotnetspark.com
Posted Date: September 13, 2010

It's not advisable to load empty values

in a dropdown list, it should be removed

before getting populated in a dropdownlist.

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend