Populate a combo box from a stored proc.

SteveInBeloit

Well-known member
Joined
May 22, 2006
Messages
132
Programming Experience
10+
I am trying to populate a combo box from stored procedure in VB.NET. I am use VS 2003, and MSSQL on the back end. I can fill the combo box with the .add("value1"), and I can create a connection string and call a stored proc with parameters, but can't figure out how to populate it based on the values returned from the SP.
Can some one please give me an example, or point me to one.
Thanks,
Steve
 
Well, this is what I did and it worked, but it is not what I wanted. I wanted to be able to just bind it to the stored proc, and not have to read through each record and .add it to the combo box.

Dim Conn As New SqlClient.SqlConnection(strConn)
Dim datareader As SqlClient.SqlDataReader
Dim cmdLoadData As New SqlClient.SqlCommand("spStartingLoc", Conn)
cboFromTo.Items.Clear()
cmdLoadData.CommandType = CommandType.StoredProcedure
Conn.Open()
datareader = cmdLoadData.ExecuteReader
While datareader.Read
cboFromTo.Items.Add(datareader(0))
End While
datareader.Close()
Conn.Close()

Steve
 
There are different ways to do this... one is using the reader like you did. The other is to return a DataTable or DataSet and set that as the DataSource, then Set the DisplayMember to the field name you want displayed, and ValueMember to the field to actualy return as the value.

There is no way to directly bind it to a stored procedure... you have to bind it to something in between.

-tg
 
Thanks, I will leave it like it is since it is working, but I will read up on how to create a dataset based on a stored proc, and then try what you mentioned.
Thanks
 
combo box

i have used it doing a dataset. Then you set the valuemember and displaymember properties to the fields from the dataset and set teh datasource of the combobox to the dataset.

I have a question if anyone can help. How can you add an unbound item (such as "Select...", or "Add New") to a databound combo box?

Thanks.
 
Back
Top