在表单之间传递值时出错

时间:2013-12-26 17:26:21

标签: c# mysql windows winforms

同时将用户名和密码从form2传递给form3,这是我第一次运行应用程序时,第一次没有传递该值。然而,当我重新运行应用程序时,它每次都有效,并且添加成功,对于c#和windows编程来说真的很新,所以请原谅任何糟糕的执行情况。 flow是form1-> form2-> form3

更新: 这是表格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);
        }
    } 

这是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();
        }
    }

这是表格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();
        }
    }

使用mysql和visual studio 2012

1 个答案:

答案 0 :(得分:1)

请为表单指定正确的名称并删除所有静态变量。

将Form3的创建移动到事件处理程序中,因此它将获得在单击时设置的用户名/密码,而不是在构造类时。