Click here to Skip to main content
14,241,320 members
Rate this:
Please Sign up or sign in to vote.
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...
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
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

you can use .

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

but also maybe there is some error on loading your mdiform
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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)
//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;
  }
}
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

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
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100