Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7569511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:11:01+00:00 2026-05-30T15:11:01+00:00

I have two combobox in my form, using a mysql connection to a remote

  • 0

I have two combobox in my form, using a mysql connection to a remote server. The first combobox populated nicely. However, I need the indexid since that is a foreign key to populate a second combobox. Based on the selection, it will change the data in the second combo (for the xample, i the first combo is for car makes, then the models of each make gets filled, so if I choose Nissan, the models will then have Altima, Maxima, Sentra, … but if I chose Toyota, the combo will then show Corolla, Camry, Prius, …)

My foreign key is always -1 for some reason. I am using the selectindex change method, but it keeps crashing/ bc the value is always -1.

I am very new to MySQL in C# and eager to learn. Any help is appreciated. The code is below.

    private void cboMake_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cboMake.SelectedIndex >= 0)
            cboModel.Enabled = true;
        else
            cboModel.Enabled = false;

        // get the foreign key value of the model id to get the make for each brand to populate here            
        // MessageBox.Show(cboModel.ValueMember);
        // right now selectindex always shows -1.  why?
        // if it changes, you need to then enable the right cbo, else, disable.
        // but also, you need the mid, fk, so you can then do a new sql statement with the where clause to populate it.
        int fk = cboModel.SelectedIndex;            
        string connStr = "server=123.456.7.8;user=root;database=car;port=3306; password=nowayjose";
        MySqlConnection conn = new MySqlConnection(connStr);
        try
        {
            string sql = "SELECT * FROM cs_model WHERE mid='fk'";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            DataTable dt = new DataTable();
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            da.Fill(dt);
            cboMake.DataSource = dt;
            cboMake.DisplayMember = "model";
            cboMake.ValueMember = "mmid";
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "MySQL Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        conn.Close();
    }

    private void frmMain_Load(object sender, EventArgs e)
    {
        string connStr = "server=123.456.7.8;user=root;database=car;port=3306; password=nowayjose";
        MySqlConnection conn = new MySqlConnection(connStr);
        try
        {
            string sql = "SELECT * FROM cs_make";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            DataTable dt = new DataTable();
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            da.Fill(dt);
            cboMake.DataSource = dt;
            cboMake.DisplayMember = "make";
            cboMake.ValueMember = "mid";               
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "MySQL Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        conn.Close();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:11:03+00:00Added an answer on May 30, 2026 at 3:11 pm
        private void cboMake_SelectedIndexChanged(object sender, EventArgs e)
        {
    
    
            int foreignKey = (int)cboMake.SelectedIndex +1;   // i forgot to cast the int from string since the database was tinyint, it still returns string in c#
            string fk = foreignKey.ToString();   // since i need the string for the query, back it goes, but i needed to do arithmetic 
            string connStr = "server=10.18.30.1;user=notroot;database=car;port=3306;password=password";
            MySqlConnection conn = new MySqlConnection(connStr);
            try
            {
                string sql = "SELECT * FROM cs_model WHERE mid=" + fk;   // forgot to escape out of the string (used to php $var style within double quotes)
                MySqlCommand cmd = new MySqlCommand(sql, conn);                    
                DataTable dt = new DataTable();
                MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                da.Fill(dt);
                cboModel.DataSource = dt;           // i had cboModel by a silly mistake
                cboModel.DisplayMember = "model";
                cboModel.ValueMember = "mmid";
    
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "MySQL Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            conn.Close();            
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two combobox. I need get some value from first combobox1 after combobox1
I currently have two comboboxes on a form. The first combobox contains a list
i have a form that consist of two textfield and one combobox. i want
I have a form with two ComboBoxes. The second ComboBox holds a list of
In my windows form i have two combobox and one text box when a
I have two combobox in my Access form named CombDate and CombDat1. Now I
I have two forms, a combobox is populated on both forms with this code
I have two combo boxes on a form. The first allows the user to
I'm a newbie using Extjs 4.07. I have created a combobox (remote) queryMode. The
I have a form that has two buttons, textbox, listbox, and a combobox. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.