简体   繁体   中英

Error passing values between forms

while passing username and password from form2 to form3 , for the first time when i run the application , the value does not get passed for the first time . however when i re - run the application , it works everytime and the addition is successful , im really new to c# and windows programming so please forgive any poor executions . flow is form1->form2->form3

UPDATE : this is the definition for form 3

  public partial class Form3 : Form
    {
        MySqlConnection connection;
        public string username;
        public string password;
        public Form3(string user,string pass)
        {
            username = user;
            password = pass;
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(username, password);
            string connstring = "server=localhost;user=" + username + ";database=testdb;port=3306;password=" + password + ";";
            connection = new MySqlConnection(connstring);
            connection.Open();
            string query = "INSERT INTO user_books values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "');";
            MySqlCommand newcommand = new MySqlCommand(query,connection);
            try
            {
                newcommand.ExecuteNonQuery();
                MessageBox.Show("DONE");
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.ToString());
            }
            connection.Close();

        }

        private void Form3_Load(object sender, EventArgs e)
        {
            MessageBox.Show(username, password);
        }
    } 

and this is form2 :

 public partial class Form2 : Form
    {
        public static MySqlConnection connection;

        public string username;
        public string password;

        public Form2(string user,string pass)
        {
            InitializeComponent();
            this.Text = "MAIN MENU";
            username = user;
            password = pass;
        }

        Form3 form3 = new Form3(username,password);
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(username,password);//this lines  no longer gives an empty message box with title Error
            form3.Show();
        }
    }

this is form 1 :

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Text = "LOGIN";
        }

        MySqlConnection connection;
        Form2 form2;
        private void button1_Click(object sender, EventArgs e)
        {
            string username = textBox1.Text;
            string password = textBox2.Text;

            string connstring = "server=localhost;user=" + username + ";database=testdb;port=3306;password=" + password + ";";

            try
            {
                connection = new MySqlConnection(connstring);
                connection.Open();

                form2 = new Form2(username,password);
                form2.Show();
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.ToString(), "Exception");
            }
            connection.Close();
        }
    }

using mysql and visual studio 2012

Please give proper names to your forms and drop all static variables.

Move the creation of Form3 into the event handler, so it will get the username/password that are set at the time of the click, not when the class is constructed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM