Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a drop down list that I am filling using linq to sql which will display a list of U.S. states. At the top of the drop down I would like to have a "Select a State" entry with a value of "XX". This being the first time I have really used linq to sql I am a little lost.

Here is my code so far:
C#
var theStates = from states in db.States
                                select new
                                {
                                    StateID = states.StateID,
                                    StateName = states.StateName
                                };

                
                dropPrimaryAddressState.DataSource = theStates;
                dropPrimaryAddressState.DataTextField = "StateName";
                dropPrimaryAddressState.DataValueField = "StateID";
                dropPrimaryAddressState.DataBind();
Posted

1 solution

C#
var theStates = from states in db.States
                                select new
                                {
                                    StateID = states.StateID,
                                    StateName = states.StateName
                                };

dropPrimaryAddressState.DataSource = theStates;
dropPrimaryAddressState.DataTextField = "StateName";
dropPrimaryAddressState.DataValueField = "StateID";
dropPrimaryAddressState.DataBind();

ListItem newItem = new ListItem();
newItem.Text = "Select a State";
newItem.Value = "XX";

dropPrimaryAddressState.Items.Insert(0, newItem);

dropPrimaryAddressState.SelectedIndex = 0;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900