Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...These forums are an excellent source and example of the way people can help each other..."

Geography

Where in the world do Tek-Tips members come from?

Combo Box and a Table Binding to Variables

rdrunner400 (Programmer)
7 May 12 12:10
Hi All,

I have a combo box that I am populating as per the following code.

It is coming from a table that has 8 fields per record and in the combo box i am making the first field of the record appear in the Combo Box.  

My question is as follows.  If I have 50 records in the underlying table I want to be able to select any of the names in the first column of the table and get the contents of the other 7 fields in the row from the table and store them in global variables.

So if I have have selected Name 1 from the table which is showing in the combo box I would like to be able to assign the other 7 fields contents to constant variables to be used furhter in the program.


Here is the code I am using .......


this.cboCombo.Visible = true;

SqlConnection cs = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename='C:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Data\\Names.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True");

// Create the data apadter
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Names ORDER by Full_Name", cs);

// Create the Data Table
DataTable dt = new DataTable();

// Fill Data Table with Records
da.Fill(dt);

// Loop around the table and populate the combo box with records....

for (int i = 0; i < dt.Rows.Count; i++)
{
   cboCombo.Items.Add(dt.Rows[i]["Full_Name"]);
}

So at this point i have got the Full Names from the Table Names and i have loaded them into the Combobox and when i click on the combo box i would like to retreive the contents of the other 7 fields in the underlying table and place them into variables.

Is this possible ???

regards in advance

rdrunner
Qik3Coder (Programmer)
9 May 12 16:25
Instead of adding a string, you need to add a ListItem.
You will need to set the Value member of the List item to the Primary Key of your table.

Then in your selectedIndexChanged event you can use the SelectedValue to get the rest of the fields.

Lodlaiden

You've got questions and source code. We want both!
Oh? That? That's not an important password. - IT Security Admin (pw on whiteboard)

rdrunner400 (Programmer)
10 May 12 2:52
Hi,

Thanks for the idea - it works a treat ....

Regards

Rdrunner

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close