Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filling DropDownList from Dataset

Status
Not open for further replies.

richself

Technical User
Jun 27, 2005
37
US
I'm trying fill a List box from a DataSet. It's for a WebClient. Below is the code I have. I can get it to come up with System.DataRow.DataRow.View in all the selections. If I go to the listbox DataBindings property and select the binding. I come up with my tables contents but each letter of the first column is on each line. IE, "Change" shows up C then H then A and so on as I progress down the listbox.

From Web Form

'Get exsisting reports from Database
Dim ws As New ReportWebClient.localhost.ReportService
ws.Credentials = System.Net.CredentialCache.DefaultCredentials
ReportData.Merge(ws.GetReport())
DataGrid1.DataBind()

'Get downtime codes for list box
CodesData.Merge(ws.GetCodes())
downtimeList.DataBind()

From Web Sevice

<WebMethod()> Public Function GetCodes() As codes1
Dim codes As New codes1
OleDbDataAdapter2.Fill(codes)
Return codes
End Function
 
I belive I got it.

'Get downtime codes for list box
CodesData.Merge(ws.GetCodes())
downtimeList.DataSource = CodesData
downtimeList.DataMember = "Codes"
downtimeList.DataTextField = "Process"
downtimeList.SelectedIndex = -1
DataBind()

Sometimes it takes putting it out here to put the pressure on!
 
My List box fill find but now I can't get the selected item into the dataset. This is the code I'm using. It works for the listboxes I have a "Collection" filled out for.

cRow("Downtime") = downtimeList.items(downtimeList.SelectedIndex).ToString()

Ideas?
 
Hey,

this is what i use to fill a drop-down:

Code:
With Combobox1
  'what value do you want to use
  .ValueMember = "IdField"
  'what do you want to show
  .DisplayMember = "DataField"
  'bind datatable to combobox
  .DataSource = DataTable1
  'display first row
  .SelectedIndex = 0
End With

to retrieve values use this:

Code:
Messagebox.Show("Value Member = " & Combobox1.SelectedValue & vbCrLf & "Display Member = " Combobox1.Text)

Dale
 
I tried using SelectedValue but now what's happening is that the value never changes from 0. During run time the list box populates and I can select an item in the list but adding a watch, the value stays at 0. Not sure what's going on.
 
You could use a reader first!

The code that fills the dropdownlist from a reader could be:
Do until reader.Read = False

combo1.Items.Add(Trim(reader.Getstring(Trim(reader.GetOrdinal("name_of_the_field")))))

loop

in the code of the form

Regards
 
You might want to double check your datatable. Possibly the values aren't what you expect.

Dale
 
Just for the curious. I have finally figured this one out. When I hit the submit button on my form, the page was reloading, thus the listbox was reloading and sending the default index to my database, which was -1. I added If Not IsPostBack to the dataset load and it took care of the problem.

 
For future reference, you may want to ask ASP.Net questions on the ASP.Net forum. Most of the work done here in VB.Net is windows application development, where we don't have to deal with IsPostBack ;)

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
That's where I get a bit confused. When to seperate the ASP / VB / SQL questions. I'm doing it all in Studio 2003 so I assumed it would be a VB.Net question. I'm starting to understand where to seperate the questions though.
Thanks for the feedback.

Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top