views:

243

answers:

5

I have a dropdownlist that I want to populate with a specific value and specific text. I'm not using a datasource but I am manually making a connection and retrieving data in code. How can I populate this dropdownlist? If I read the data with a datareader and increment an array I only get either the value or the text. This is what I have so far, but it is completely wrong:

//connection string etc goes here
Dbcmd2.CommandText = "select dept,deptname from table"

Dim dr As SqlClient.SqlDataReader
    dr = Dbcmd2.ExecuteReader
    Dim i As Integer
    Dim arydept As New arraylist
    While dr.Read
       arydept.Add(dr1("dept"))
    End While

    ddldept.datasource = arydept
    ddldept.DataTextField = ????????
    ddldept.DataValueField = dr("dept")
    ddldept.DataBind()

How can I get this to work without having to create a class object for Department? Is there anyway or should I create the class object?

+4  A: 

Is there any particular reason you can do it the normal way?

Dbcmd2.CommandText = "select dept,deptname from table"
Dim dr As SqlClient.SqlDataReader = Dbcmd2.ExecuteReader
While dr.Read() 
  ddldept.Items.Add(new ListItem(dr("deptname"),dr("dept"))
End While
' ddldept is now populated with all items from the query
Welbog
UPS, dataaccess code and view code in the same context, this is only demo code - don't do this at home kids!!!
khebbie
@khebbie: Well, obviously. But I think Eric is more concerned with populating the combobox than the data access. If he wants to wrap it into another object for encapsulation that's up to him. He should do it, though. You hear me, Eric?
Welbog
@Welbog I thought you'd know this, it's just that some kid might fly by and see this and think: "Oh!, that's the way to do it" - so that's why I added the warning...No offend!
khebbie
IMO he and his employer would deserve the subseqeuent pain, but good form adding the warning anyway.
belgariontheking
This worked fine, however it's ListItem rather than Item. Thank you. I've used this before...just forgot. I was looking for a dictionary type of answer but this will do just fine.
Eric
@Eric: Updated.
Welbog
+1  A: 

Instead of using an ArrayList if you use a dictionary object then you can store the name and the value.

Something like this should do it

Dim All As New Dictionary(Of String, String)
All.Add("Test", 1)
All.Add("Test2", 2)
test.DataSource = All
test.DataTextField = "Key"
test.DataValueField = "Value"
test.DataBind()
Gavin Draper
I like this idea and will use this in the future. I wasn't sure how to use a dictionary. +1
Eric
+1  A: 

Instead of using the datasource/databind() approach, you could simply create the list items and add them to your dropdownlist.

 dr = Dbcmd2.ExecuteReader
    Dim i As Integer
    Dim arydept As New arraylist

    ddldept.Items.Clear()
    While dr.Read
       ddldept.Items.Add(new ListItem(dr1("dept"), dr1("dept")))
    End While
womp
+1  A: 

How about this one?

ddldept.Items.Clear()

While dr.Read
   ListItem item = New ListItem()
   item.Text = dr("deptname").ToString()
   item.Value = dr("dept").ToString()

   ddldept.Items.Add(item)
End While
Greco
Great answer! +1
Eric
+1  A: 

You are already using the reader so just pop that into a DataTable and bind:

//connection string etc goes here
Dbcmd2.CommandText = "select dept,deptname from table"

Dim dr As SqlClient.SqlDataReader
dr = Dbcmd2.ExecuteReader
Dim myData as DataTable
If dr.HasRows Then
    myData.Load(dr)
End If

ddldept.datasource = myData
ddldept.DataTextField = myData("myTextField")
ddldept.DataValueField = myData("dept")
ddldept.DataBind()
Rob Allen
I really like this idea! +1. Never even thought about this one
Eric