Click here to Skip to main content
15,917,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I create a login form in that frmlogin when i login button click go to another form like MDIparent1 form but it open and exit this is my code plese help...
C#
private void login_Click(object sender, EventArgs e)
{
       try
       {
            System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
            conn.ConnectionString = BillingApplication.Properties.Settings.Default.DB_BillingConnectionString;
            conn.Open();

            string selectString = "SELECT * FROM login WHERE (Username = '" + textBox1.Text + "' AND Password = '" + textBox2.Text + "')";
            OleDbCommand cmd = new OleDbCommand(selectString, conn);
            OleDbDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows == true)
            {
                  MDIParent1 M1 = new MDIParent1();
                  M1.Show();    
            }
            catch (Exception)
            {
                MessageBox.Show("Error with Database Connection");
            }
}
Posted
v4
Comments
pradiprenushe 23-Nov-12 0:43am    
Can u paste code in MDIParent1 form?
Abhishek Pant 23-Nov-12 3:57am    

you can use .

m1.showdialog();
this.hide();

but also maybe there is some error on loading your mdiform
 
Share this answer
 
it's not quite right to start your mdi form from login form. your login form should be called from mdi form. it can be done in several places for example:
In MDI form constructor(user wouldn't see MDIForm before login)
C#
//MDIForm constructor
public MDIForm()
{

  using (AuthorizeForm form = new AuthorizeForm())
                {
                    if (form.ShowDialog() != DialogResult.OK) //login incorrect
                    {
                        System.Environment.Exit(1);
                        return;
                    }
                }
}

//it's your AutorizeForm
public class AutorizeForm()
{
     private void login_Click(object sender, EventArgs e)
     {
       try
       {
            System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
            conn.ConnectionString = BillingApplication.Properties.Settings.Default.DB_BillingConnectionString;
            conn.Open();
 
            string selectString = "SELECT * FROM login WHERE (Username = '" + textBox1.Text + "' AND Password = '" + textBox2.Text + "')";
            OleDbCommand cmd = new OleDbCommand(selectString, conn);
            OleDbDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows == true)
            {
                    DialogResult = DialogResult.OK;
                    return;
            }
        }
        catch (Exception)
        {
            MessageBox.Show("Error with Database Connection");
        }
        finally
        {
            if(conn != null && conn.State != == ConnectionState.Closed)
            {
                conn.Close();
            }
        }
   DialogResult = DialogResult.NO;
  }
}
 
Share this answer
 
why do you need MdI? if it can be done simply by opening another form and hiding Ist form.


private void login_Click(object sender, EventArgs e)
{
       try
       {
            System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
            conn.ConnectionString = BillingApplication.Properties.Settings.Default.DB_BillingConnectionString;
            conn.Open();
 
            string selectString = "SELECT * FROM login WHERE (Username = '" + textBox1.Text + "' AND Password = '" + textBox2.Text + "')";
            OleDbCommand cmd = new OleDbCommand(selectString, conn);
            OleDbDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows == true)
            {
                  
                  Form2 f2 = new Form2();
                  f2.Show();  // Display the new form.
           this.Hide(); //hide current form
            

            }
            catch (Exception)
            {
                MessageBox.Show("Error with Database Connection");
            }
}


and do what you want to do in Form2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900