Drop down for states

M

Me LK

I have a drop down menu with hard coded states. When the page is
viewed the top state automatically shows (Alabama). The state I need
to show is the one that is in the database not the top on the list.
Since I have hard coded the states to the datalist instead of pull
them from the database I am a bit confused as to how to display the
correct item.

My code behind (vb)

______________________________________________________________________
Dim connection As New SqlConnection(connectionString)
Dim Command As New SqlCommand("GetCustomer", Connection)
Command.CommandType = CommandType.StoredProcedure
Command.Parameters.Add("@CustomerID",
Context.Session("CustomerID"))

' extract existing customer details

connection.Open()
Dim customerReader As SqlDataReader = command.ExecuteReader()
customerReader.Read()
If Not customerReader("homeaddress").GetType() Is
GetType(DBNull) Then
txtFirstName.Text = customerReader("first")
txtLastName.Text = customerReader("Last")
txtStreetAddress.Text = customerReader("homeaddress")
txtStreetAddress2.Text = customerReader("homeaddress2")
txtCity.Text = customerReader("City")

State.DataTextField = customerReader("state") ********THIS
IS THE DROP DOWN MENU

________________________________________________________________
I am hard coding the drop down menu in this manner.

<asp:dropdownlist id="State" runat="server">
<asp:ListItem Value="AL">Alabama</asp:ListItem>
<asp:ListItem Value="AK">Alaska</asp:ListItem>
<asp:ListItem Value="AZ">Arizona</asp:ListItem>
<asp:ListItem Value="AR">Arkansas</asp:ListItem>
<asp:ListItem Value="CA">California</asp:ListItem>
<asp:ListItem Value="CO">Colorado</asp:ListItem>

Etc.......
____________________________________________________________________


The text boxes work find and pull the data from the database. The
dropdown list does not. It only shows the hard coded data. This is
probably something stupid and trivial but my brain is just not
working. Any suggestions appreciated.

LK
 
R

Roland Dick

Hi LK,

Me said:
I have a drop down menu with hard coded states. When the page is
viewed the top state automatically shows (Alabama). The state I need
to show is the one that is in the database not the top on the list.
Since I have hard coded the states to the datalist instead of pull
them from the database I am a bit confused as to how to display the
correct item.
State.DataTextField = customerReader("state") ********THIS
IS THE DROP DOWN MENU

DataTextField is only required when you bind the (possible) entries of
the dropdownlist to a datasource. It then tells ASP.NET which field to
use for the description (that is displayed). Since you have hardcoded
those values, you don't want that.

Try setting the SelectedValue property of the dropdownlist instead:
State.SelectedValue = customerReader("state")

Hope this helps,

Roland
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top