Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# MySQL combobox Help
Hi everyone.
 
I am trying to populate my text boxes with the values in my database so when a user selects a name of a teacher from the combobox, the text-boxes will populate with their contact details. This is the code I have so far. There are no errors however the textboxes are still blank when I select a value from combo box.
 
  private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MySqlConnection cs = new MySqlConnection(connectionSQL);
            cs.Open();
 
            DataSet ds = new DataSet();
 
            MySqlDataAdapter da = new MySqlDataAdapter("Select * from Teacher WHERE name='" + comboBox1.Text + "'", cs);
 
            MySqlCommandBuilder cmd = new MySqlCommandBuilder(da);
 
            da.Fill(ds);
 

            if (comboBox1.SelectedIndex > 0)
            {
 

                NameBox.Text = ds.Tables[0].Rows[0]["name"].ToString();
                AddressBox.Text = ds.Tables[0].Rows[0]["address"].ToString();
 
            }
Any help or advice would be greatly appreciated
 

UPDATED CODE:
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MySqlConnection cs = new MySqlConnection(connectionSQL);
 
string query = "SELECT name FROM Teacher WHERE name= 'Liz Murphy'";
MySqlDataAdapter adapter = new MySqlDataAdapter(query, cs);
DataSet DS = new DataSet();
 
adapter.Fill(DS);
 
NameBox.Text = DS.Tables[0].Rows[0].ToString();
Posted 3-Apr-13 6:55am
Edited 3-Apr-13 10:46am
v4
Comments
richcb at 3-Apr-13 12:01pm
   
I think you want to use "comboBox1.SelectedValue" when you query the database.
Member 9611735 at 3-Apr-13 12:05pm
   
Thank you. I changed my code but the textboxes are still not being populated
richcb at 3-Apr-13 12:07pm
   
Is your dataset getting filled with data?
Member 9611735 at 3-Apr-13 12:15pm
   
I just checked and it's not. Do you think there is something wrong with my query?
richcb at 3-Apr-13 12:23pm
   
Check my solution below and see if you can get that to work.
Member 9611735 at 3-Apr-13 12:51pm
   
Your solution throws up the following error Error 1 'MySql.Data.MySqlClient.MySqlCommand' is a 'type' but is used like a 'variable'. I've tried changing it to different things but dataset is still empty
richcb at 3-Apr-13 13:41pm
   
Look at my update to the solution please.
Member 9611735 at 3-Apr-13 14:20pm
   
There is still nothing in dataset
richcb at 3-Apr-13 14:22pm
   
Maybe your query is the problem. Is "Teacher" the name of the table? Can you post your connection string please?
Member 9611735 at 3-Apr-13 14:26pm
   
Yes teacher is the name of the table. My connection string is
string connectionSQL= "server=sql.byethost27.org;user id=xxxxxxx;password=xxxxxxxxxx;database=xxxxxxxxxx";
richcb at 3-Apr-13 14:29pm
   
Odd, I hate to ask this, but is there data in the table? What type of database is it?
Member 9611735 at 3-Apr-13 14:33pm
   
Yes there is two records in the database. It's mySQL database. I am getting combobox values from the same table in the database and it's working fine
richcb at 3-Apr-13 14:41pm
   
I found another sample you can try. See my solution.
Member 9611735 at 3-Apr-13 14:51pm
   
I added in NameBox.Text = DS.Tables[0].Rows[0].ToString(); after your newly posted solution and this error occurred
There is no row at position 0.
richcb at 3-Apr-13 15:02pm
   
It has to be your query not returning anything. Is "name" the exact column name in "Teacher"? When you debug, what is the value of "comboBox1.SelectedValue"?
Member 9611735 at 3-Apr-13 15:23pm
   
yes name is the column name. Do i need to bind my textbox with the table in xaml or anything? Am i using the correct code to add the result into textbox?
richcb at 3-Apr-13 15:26pm
   
No binding needed. I think it is correct as long as the dataset is getting filled.
Member 9611735 at 3-Apr-13 15:38pm
   
I tried specifying the query by adding a where clause to query and specifying a name. Now when I debug the textbox is populated with System.Data.DataRow
richcb at 3-Apr-13 15:41pm
   
Can you use the "Improve question" widget to add all the code you are using now please? Leave what you have already, just add what you have now to the bottom of it.
Member 9611735 at 3-Apr-13 16:44pm
   
There is definitely something wrong with the query. I played about with the code and by specifying the name of teacher all the textboxes were populated with the correct data from database. Any suggestions?? Thank you so much for your help thus far.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try something like this:
        MySqlConnection cs = new MySqlConnection(connectionSQL);
 
        string query = "SELECT * FROM Teacher WHERE name= '" + comboBox1.SelectedValue + "'";
        adapter = new MySqlDataAdapter(query, cs);
        DataSet DS = new DataSet();
 
        adapter.Fill(DS);
 
You will have to alter how you get your textboxes filled after the query.
  Permalink  
v5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

PROBLEM SOLVED!!! I hadn't assigned combobox selectedValuePath when I was populating combo box. Many thanks to richcb for helping me
  Permalink  
Comments
richcb at 3-Apr-13 17:29pm
   
You are welcome.

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

  Print Answers RSS
0 OriginalGriff 190
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 Afzaal Ahmad Zeeshan 120
4 DamithSL 115
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 3 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100