Assiging database fields from a listbox

Johnnh

Member
Joined
May 5, 2006
Messages
6
Programming Experience
Beginner
Hello

I have a windows form with a list box that is poulated with from a dataset containing one table with two fields, name and surname. I need to display both the forename and surname in different text boxes as the user selects the name from the list box.

I can do this with one text box (name) using the following

TextBox3.Text = ListBox1.SelectedValue

But since the listbox is assigned i am unable to fill the second text box with the surname field

I am aware this can be done with the databind option on a web application but are stuck how to do it with a form.I am attached to a access database.

Any help v much appreciated
 
i use two dataset's, the first one gabs only the primary key feild and whatever feild that's displayed in the listbox then in the selectedindexchanged event of the listbox i fill the other dataset grabbing only one record which all the other textbox's and labels and whatnot are bound to
 
Thanks for the reply, if you have time could you demonstrate the code for the method you placed as im very new to this

Many thank
J

simply drag and drop the database feilds from the server explorer for the listbox on the form a DB connection and DataAdapter will be made automatically, rename the DataAdapter to something more understanding/useful, now right click the dataadapter and select configure and make sure you've set everything up as needed when done with that wizard right click the dataadapter and select generate dataset, to bind the listbox to the dataset simply set it's source property to the new dataset and it's displaymember property to the field in the dataset you want diplayed to the user

do the same for making the other dataadapter and dataset

in the listbox's selectedindexchanged event simply fill the 2nd dataset by calling it's Fill() method with the 2nd dataset as an arguement, you'll also need to set the command parameter for it before filling the dataset so it only grabs 1 record, i would use the primary key as the field for only grabbing 1 record

there are many tutorials available on the web for explaining this better
 
Back
Top