0

I have this code in my btnLogin in Form1 to get data from MS Access database and I want it to display in Form2using label but I don't know how to pass it to Form2

private void btnLogin_Click(object sender, EventArgs e)
    {
        connection.Open();
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        if ((this.txtUser.Text == "admin") && (this.txtPass.Text == "admin"))
        {
            Form1 main = new Form1();
            main.txtHere.Text = txtUser.Text;
            main.Show();
            this.Hide();
        }
        else {
            command.CommandText = "select * from StudentsTBL where LastName='" + txtUser.Text + "' and FirstName='" + txtPass.Text + "'";

            OleDbDataReader reader = command.ExecuteReader();
            int count = 0;
            while (reader.Read()) {
                count = count + 1;
                //count++;
            }
            if (count == 1) {
                MessageBox.Show("Login Successful!");
                List<String> stdNo = new List<String>();
                List<String> middleName = new List<String>();
                List<String> section = new List<String>();
                List<String> year = new List<String>();
                List<String> sem = new List<String>();
                List<String> address = new List<String>();
                List<String> dob = new List<String>();
                List<String> gender = new List<String>();
                List<String> age = new List<String>();
                List<String> contact = new List<String>();
                List<String> desc = new List<String>();

                command = new OleDbCommand("select * from StudentsTBL", connection);
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    stdNo.Add(reader["StudentNo"].ToString());
                    middleName.Add(reader["Middle"].ToString());
                    section.Add(reader["stdSection"].ToString());
                    year.Add(reader["stdYear"].ToString());
                    sem.Add(reader["stdSem"].ToString());
                    address.Add(reader["stdAddress"].ToString());
                    dob.Add(reader["stdDob"].ToString());
                    gender.Add(reader["stdGender"].ToString());
                    age.Add(reader["stdAge"].ToString());
                    contact.Add(reader["ContactNo"].ToString());
                    desc.Add(reader["stdDesc"].ToString());
                }
                StudentProfile stdProfile = new StudentProfile(txtUser.Text, txtPass.Text);
                stdProfile.Show();
                this.Hide();
            }
            else if (count > 1)
            {
                MessageBox.Show("Duplicate username and password!!");
            }
            else {
                MessageBox.Show("Login Failed!");
            }
        }
        connection.Close();
    }

source: Getting data from MS Access database and display it in a listbox

4
  • Um, what Form2? What's the name of your Label field? Commented Feb 24, 2015 at 0:00
  • I want to display the data to form2 Commented Feb 24, 2015 at 0:04
  • Forms are just classes Commented Feb 24, 2015 at 0:09
  • If you are trying to get Form1 talk to Form2, you can use delegates or constructor injection. Refer stackoverflow.com/a/27179975/1862333 Commented Feb 24, 2015 at 0:26

1 Answer 1

1

Why you are not using structures ? It will make your like easy yo can handle a lot of information in a single variable.

And why you not make a global variable ? It will be available from any part of you application.

I have project like this on GitHub you can downloaded as example reference

    structure UserInfo{
  public string stdNo;
  public string middleName;
  public string section;
  public string year;
  public string sem;
  public string address;
  public string dob;
  public string gender;
  public string age;
  public string contact;
  public string desc;
}

//===>Define a global variable to handle
public static UserInfo LoggedUsrInfo  = new UserInfo();

private void btnLogin_Click(object sender, EventArgs e)
    {
        connection.Open();
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        if ((this.txtUser.Text == "admin") && (this.txtPass.Text == "admin"))
        {
            Form1 main = new Form1();
            main.txtHere.Text = txtUser.Text;
            main.Show();
            this.Hide();
        }
        else {
            command.CommandText = "select * from StudentsTBL where LastName='" + txtUser.Text + "' and FirstName='" + txtPass.Text + "'";
            DataTable Tbl = new DataTable();
            OleDbDataReader reader = command.ExecuteReader();
            Tbl.Load(reader,LoadOption.OverwriteChanges);//===>Retrieve data and load to datatble
            reader.Close();//Close the reader
            if (Tbl.Rows.Count > 0) {//Count if the data table retrieve some info
                DataTable TblInfo = new DataTable();
                MessageBox.Show("Login Successful!");


                command = new OleDbCommand("select * from StudentsTBL", connection);
                reader = command.ExecuteReader();
                TblInfo.Load(reader,LoadOption.OverwriteChanges);
                reader.Close();//Close the reader
                    LoggedUsrInfo.stdNo = TblInfo.Rows[0]["StudentNo"].ToString();
                    LoggedUsrInfo.middleName = TblInfo.Rows[0]["Middle"].ToString();
                    LoggedUsrInfo.section = TblInfo.Rows[0]["stdSection"].ToString();
                    LoggedUsrInfo.year = TblInfo.Rows[0]["stdYear"].ToString();
                    LoggedUsrInfo.sem =TblInfo.Rows[0]["stdSem"].ToString();
                    LoggedUsrInfo.address = TblInfo.Rows[0]["stdAddress"].ToString();
                    LoggedUsrInfo.dob = TblInfo.Rows[0]["stdDob"].ToString();
                    LoggedUsrInfo.gender = TblInfo.Rows[0]["stdGender"].ToString();
                    LoggedUsrInfo.age = TblInfo.Rows[0]["stdAge"].ToString();
                    LoggedUsrInfo.contact = TblInfo.Rows[0]["ContactNo"].ToString();
                    LoggedUsrInfo.desc = TblInfo.Rows[0]["stdDesc"].ToString();

                StudentProfile stdProfile = new StudentProfile(txtUser.Text, txtPass.Text);
                stdProfile.Show();
                this.Hide();
            }
            else if (count > 1)
            {
                MessageBox.Show("Duplicate username and password!!");
            }
            else {
                MessageBox.Show("Login Failed!");
            }
        }
        connection.Close();
    }


/*
you can access to UserInfo calling in this way from your Form2
Load Event

Label1.Text = Form1.LoggedUsrInfo.middleName;

*/
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.