简体   繁体   中英

C# WInform ,Passing next value to the next form, HOW

how will I pass data from database to the next Winform, like in php called session)

I wanted to show, a welcome message showing NICKNAME on the 2nd form, instead of USER ID, Please show me in most easiest way, Thanks

Below is my code for Loginpage:

try //***************TRY START****************
{
    OleDbCommand cmd = new OleDbCommand("Select * from Account where User_ID='" + userID.Text + "' and Pass='" + Password.Text + "';", con);
    OleDbDataReader myReader;
    con.Open();
    myReader = cmd.ExecuteReader();
    int count = 0;
    while (myReader.Read())
    {
        count = count + 1;
    }

    if (count == 1)
    {
        string s = userID.Text;
        MessageBox.Show(s);
        this.Hide();
        AdminPanel us = new AdminPanel();
        us.Show();
    }
    else  
    {
        MessageBox.Show("Invalid ");
    }
    con.Close();
} // ******END TRY********
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
    con.Close();
}

you can create constructor in Form2 to accept nickname

public Form2(string nickname)
{
  InitializeComponent(); 
  label1.Text=nickname;
}

in your 1st page, read the nickname from myReader and pass it to Form2

con.Open();
OleDbDataReader myReader= cmd.ExecuteReader();
if(myReader.Read())
{
       string nickname= myReader["Nickname"].ToString(); // give correct column name as your database
       Form2 frm=new Form2(nickname); 
       frm.Show();
}else  
{
    MessageBox.Show("Invalid ");
}

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