PuchuKing33
PuchuKing33

Reputation: 381

C# Mysql Link Combobox with Database values in textboxes

I have a form that has a ComboBox that has to display names and the when the name is select is has to show the different values from my mysql database in different textboxes.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Dark_Heresy
{
    public partial class Required_Attributes : Form
    {
        public Required_Attributes()
        {
            InitializeComponent();
        }

        private void cb_Talents_SelectedIndexChanged(object sender, EventArgs e)
        {
            string constring = "datasource = localhost; port = 3306; username = root; password = Mypass;";
            string Query = "SELECT * FROM dark_heresy.talents WHERE TalentName='" + cb_Talents.Text + "' ;";
            MySqlConnection conDataBase = new MySqlConnection(constring);
            MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
            MySqlDataReader myReader;

            try
            {
                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader();

                while (myReader.Read())
                {

                    string description = myReader.GetString("Description");
                    string strength = myReader.GetInt32("R_Str").ToString();
                    string weaponskill = myReader.GetInt32("R_WS").ToString();
                    string ballisticskill = myReader.GetInt32("R_BS").ToString();
                    string fellowship = myReader.GetInt32("R_Fel").ToString();
                    string perception = myReader.GetInt32("R_Per").ToString();
                    string intelligence = myReader.GetInt32("R_Int").ToString();
                    string agility = myReader.GetInt32("R_Agi").ToString();
                    string willpower = myReader.GetInt32("R_WP").ToString();
                    string toughness = myReader.GetInt32("R_Tough").ToString();
                    string talentrequired = myReader.GetString("Talent_required");
                    string skillrequired = myReader.GetString("Skill_required");
                    string classrequired = myReader.GetString("Class_required");

                    TextDescription.Text = description;
                    TextStrengh.Text = strength;
                    TextWeaponskill.Text = weaponskill;
                    TextBallisticskill.Text = ballisticskill;
                    TextFellowship.Text = fellowship;
                    TextPerception.Text = perception;
                    TextIntelligence.Text = intelligence;
                    TextAgility.Text = agility;
                    TextWillpower.Text = willpower;
                    TextToughness.Text = toughness;
                    TextTalent.Text = talentrequired;
                    TextSkill.Text = skillrequired;
                    TextClass.Text = classrequired;



                }
            }

                catch (Exception ex)
                {
                    MessageBox.Show("Error: \r\n" + ex);
                }

            }
        }
    }

However when i open the form and press the ComboBox, nothing happens, and nothing is shown, no syntax error occurs, what is wrong with the code?

UPDATE even if i change the code to:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Dark_Heresy
{
    public partial class Talents : Form
    {
        public Talents()
        {
            InitializeComponent();
        }

        private void cb_Talents_SelectedIndexChanged(object sender, EventArgs e)
        {

            MessageBox.Show("Test");
            //string constring = "datasource = localhost; port = 3306; username = root; password = Lorena89;";
            //string Query = "SELECT * FROM dark_heresy.talents WHERE TalentName='" + cb_Talents.Text + "' ;";
            //MySqlConnection conDataBase = new MySqlConnection(constring);
            //MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
            //MySqlDataReader myReader;

            //try
            //{
            //    conDataBase.Open();
            //    myReader = cmdDataBase.ExecuteReader();

            //    while (myReader.Read())
            //    {

            //        string description = myReader.GetString("Description");
            //        string strength = myReader.GetInt32("R_Str").ToString();
            //        string weaponskill = myReader.GetInt32("R_WS").ToString();
            //        string ballisticskill = myReader.GetInt32("R_BS").ToString();
            //        string fellowship = myReader.GetInt32("R_Fel").ToString();
            //        string perception = myReader.GetInt32("R_Per").ToString();
            //        string intelligence = myReader.GetInt32("R_Int").ToString();
            //        string agility = myReader.GetInt32("R_Agi").ToString();
            //        string willpower = myReader.GetInt32("R_WP").ToString();
            //        string toughness = myReader.GetInt32("R_Tough").ToString();
            //        string talentrequired = myReader.GetString("Talent_required");
            //        string skillrequired = myReader.GetString("Skill_required");
            //        string classrequired = myReader.GetString("Class_required");

            //        TextDescription.Text = description;
            //        TextStrengh.Text = strength;
            //        TextWeaponskill.Text = weaponskill;
            //        TextBallisticskill.Text = ballisticskill;
            //        TextFellowship.Text = fellowship;
            //        TextPerception.Text = perception;
            //        TextIntelligence.Text = intelligence;
            //        TextAgility.Text = agility;
            //        TextWillpower.Text = willpower;
            //        TextToughness.Text = toughness;
            //        TextTalent.Text = talentrequired;
            //        TextSkill.Text = skillrequired;
            //        TextClass.Text = classrequired;

            //    }
            //}

            //    catch (Exception ex)
            //    {
            //        MessageBox.Show("Error: \r\n" + ex);
            //    }

            }

        private void Talents_Load(object sender, EventArgs e)
        {

        }

        }
    }

Nothing happens, the combobox is never fired up, how can i make it work.

Upvotes: 0

Views: 1154

Answers (1)

Ntellect13
Ntellect13

Reputation: 331

I would set a static value that you know exists in the database inside the query and see if it returns anything. This will tell you if your query is incorrect.

 string Query = "SELECT * FROM dark_heresy.talents WHERE TalentName='This.Guy';";

As per your second question, you can create static global variables inside your Required_Attributes form like:

public static string username;
public static string password;

And on the closing event of your login page, set the variables' values like:

private void Authenticate_Closing(object sender, FormClosingEventArgs e)
    {
        Required_Attributes.username = username.text;
        Required_Attributes.password = password.text; 
    }

Your connection string would look like this:

string constring = "datasource = localhost; port = 3306; username =" + username + "; password = " + password + ";";    

Upvotes: 1

Related Questions