Gopal
Gopal

Reputation: 11972

Combobox first values should be null

Using C# & MYSQL

Using Combobox in my webpage, in a combobox i want to display a null values first, then it should display all values..

For Example

Combobox.item = null values
combobox.item = 1
Combobox.item = 2
....,

Code

cmd = new OdbcCommand("Select vehicleno as vehicleno from tb_car", con);
        ada = new OdbcDataAdapter(cmd);
        ada.Fill(data1);
        cmbvnoview.DataValueField = "vehicleno";
        cmbvnoview.DataSource = data1;
        cmbvnoview.DataBind();

Above code is working, but is displaying all the values, first it should display a null value, then it should display all the values.

How to modify my code....

Need Code Help

Upvotes: 0

Views: 1596

Answers (3)

akapoor
akapoor

Reputation: 46

To keep the logic at one place, just add a null record to your sql statement

cmd = new OdbcCommand("Select null as vehicleno Union Select vehicleno as vehicleno from tb_car", con);

Upvotes: 0

Rippo
Rippo

Reputation: 22424

Try this after you data bind

...
cmbvnoview.DataBind();
cmbvnoview.Items.Insert(0, new ListItem("Null Values", "-1"));

Change -1 with whatever you feel confortable with. You will have to change your sql to filter out nulls.

OR

try this

   cmd = new OdbcCommand("Select IFNULL(vehicleno, 'Null Values') 
    as vehicleno from tb_car", con);

Your question does not quite make sense so this may not be the right answer.

Upvotes: 2

leppie
leppie

Reputation: 117220

Just prefix an empty/dummy entry to you datasource.

Upvotes: 1

Related Questions