×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • 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!

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

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Combo Box and a Table Binding to Variables

Combo Box and a Table Binding to Variables

Combo Box and a Table Binding to Variables

(OP)
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

RE: Combo Box and a Table Binding to Variables

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)

RE: Combo Box and a Table Binding to Variables

(OP)
Hi,

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

Regards

Rdrunner

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

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! Already a Member? Login


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