Search code examples
c#tabpage

How to show a tabpage with button click from other form


private void btnLogin_Click(object sender, EventArgs e)
{
  Form1 frm = new Form1();
  string sql = "SELECT * From Admin WHERE UserName='" + txtUserName.Text + "' And Password='"+txtPassword.Text+"'";

  if (conn2.State != ConnectionState.Open)
  {
    conn2.Open();
  }

  command = new SqlCommand(sql, conn2);
  SqlDataReader reader = command.ExecuteReader();
  reader.Read();

  if (reader.HasRows)
  {
    if(reader[0]==txtUserName.Text   && reader[1]==txtPassword.Text)
    {
      // I want the code in this section 
    }
  }
}

I want to activate the "edit tabpage" when the login suceeds . The "edit page " is in other form.


Solution

  • This might do the trick for you.

    int indexyouwant = 1; // Suppose 1 is your Edit Page Tab. 
    Form1 frm = new Form1();
    // SQL 
    if (reader.HasRows)
    {
        if(reader[0]==txtUserName.Text   && reader[1]==txtPassword.Text)
        {
            frm.YourTabControlName.SelectedIndex = indexyouwant;
        }
    }
    

    It is suggested that you should use Parameterized Query to prevent SQL Injection.