Click here to Skip to main content
16,017,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Okay so i have a combo box which is being filled with Names from a MySql table,


here is the code I'm using for that

private void cbox()
   {
       myConnection.GetConnection();
       MySqlDataAdapter sqlgender = new MySqlDataAdapter("Select * from users", myConnection.GetConnection());
       DataTable dtgen = new DataTable("users");
       sqlgender.Fill(dtgen);
       cbxnames.DataSource = dtgen;
       cbxnames.ValueMember = dtgen.Columns[0].ColumnName;
       cbxnames.DisplayMember = dtgen.Columns[1].ColumnName;

   }


the table looks like this

ID NAME GROUP BLAH BLAH BLAH
1 name admin blah blah blah


What I'm trying to do, is based of the combo-box selection populate a few text-boxes with the rest of the table data for that persons id or name
i have no clue how to go about doing this, I've been looking for the last while so any help would be appreciated
Posted
Comments
Mohd Wasif 27-Oct-10 1:25am    
Elaborate ur question to be answer.
Toli Cuturicu 27-Oct-10 9:45am    
Why do all the last 3 columns contain the same data (blah)?

1 solution

First of all while populating your combobox, don't do a "Select * From...". Instead be specific and do "Select ID, Name From..."

Now, based on combobox selection you can always get what ID is selected. Pass that ID to your database and create a Select query Select * From ABC Where ID =@ID

Get back the data from this select query. It should be a single row with all the needed data. Map it to textbox or other controls to display it.

Try!
 
Share this answer
 
Comments
Tunacha 27-Oct-10 2:25am    
Okay well this is what i have tried so far

<pre> private void cbxnames_SelectedIndexChanged(object sender, EventArgs e)
{
string id;
id = cbxnames.ValueMember;
myConnection.GetConnection();
MySqlCommand txtfill = new MySqlCommand("SELECT * FROM users WHERE ID = '"+id+"'", myConnection.GetConnection());
txtfill.ExecuteNonQuery();
MySqlDataReader myReader = txtfill.ExecuteReader();
while (myReader.Read() != false)
{
string name = myReader.GetString("name").ToString();
uname.Text = name;
}


</pre>

But im having no luck with this, could anyone help
Sandeep Mewara 27-Oct-10 2:50am    
What do you mean by doing first ExecuteNonQuery and then ExecuteReader?

Have a look at how ADO.NET works: http://msdn.microsoft.com/en-us/library/e80y5yhx(VS.71).aspx
Sandeep Mewara 27-Oct-10 2:51am    
Learn about ADO.NET first... your code has lots of issues.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900