DropDownList index

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
Hello,

I have some code which does a select statement on a database and then binds to a dropdownlist. The sql returns two values, Time and SlotID. I would like the time to show as an item in the list(this is easy) but I would also like to store the slotID in the list, maybe as an index. It needs to be related to the Time item so that when a user clicks the text in the list the underlying slotid is shown. Here is my current code
VB.NET:
[size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] cmdSelect [/size][size=2][color=#0000ff]As[/color][/size][size=2] SqlCommand

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] reader [/size][size=2][color=#0000ff]As[/color][/size][size=2] SqlDataReader

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] conn [/size][size=2][color=#0000ff]As[/color][/size][size=2] SqlConnection = [/size][size=2][color=#0000ff]New[/color][/size][size=2] SqlConnection([/size][size=2][color=#800000]"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Procedures.mdf;Integrated Security=True;User Instance=True"[/color][/size][size=2])

conn.Open()

cmdSelect = [/size][size=2][color=#0000ff]New[/color][/size][size=2] SqlCommand([/size][size=2][color=#800000]"sp_selectZoneTimes "[/color][/size][size=2], conn)

cmdSelect.CommandType = CommandType.StoredProcedure

cmdSelect.Parameters.Add([/size][size=2][color=#0000ff]New[/color][/size][size=2] SqlParameter([/size][size=2][color=#800000]"@ZoneID"[/color][/size][size=2], SqlDbType.Int, 4))

cmdSelect.Parameters(0).Value = GetZoneId([/size][size=2][color=#0000ff]CInt[/color][/size][size=2](TextBoxCustNo.Text)) [/size][size=2][color=#008000]'calls GetZoneID function to return the customers zone

[/color][/size][size=2]reader = cmdSelect.ExecuteReader()

[/size][size=2][color=#0000ff]While[/color][/size][size=2] reader.Read

DropDownListSlotID.Items.Add(reader([/size][size=2][color=#800000]"Time"[/color][/size][size=2]))

[/size][size=2][color=#008000]'Me.DropDownListSlotID.DataValueField = reader("SlotID")

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]While

[/color][/size][size=2]reader.Close()

conn.Close()

[/size]

As you can see I tried using the .dataveluefield but this didn't help
Any ideas?
 
Personally I read into a table then bind that to the dropdown using the .displaymember for the visable items and .valuemember for the value.
 
Back
Top