Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
how to open a application which is a form after a login form.i have made a login form which results in a message login successfull in a messagebox.
can i open my application after that form when the login is successfull. and returns to the same login form if the login is not valid
my code for login form is
also my database is sql server 2008 and i am using visual studio
thanks in advance
help me plz

C#
private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\documents and settings\aquib\my documents\visual studio 2010\Projects\login\login\Database1.mdf;Integrated Security=True;User Instance=True";
            try
            {
                con.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Error with the databse connection");
            }
            string qry1 = "Select * from Log007 where Password=@Password and Username=@Username";
            SqlCommand com = new SqlCommand(qry1, con);
            com.Parameters.AddWithValue("@Username", this.textBox1.Text);
            com.Parameters.AddWithValue("@Password", this.textBox2.Text);
            SqlDataReader dr = com.ExecuteReader();
            while (dr.Read())
            {
                if (dr.HasRows == true)
                {
                    MessageBox.Show("Login Successfull", "Login Information");
                }
            }
            if (dr.HasRows == false)
            {
                MessageBox.Show("Access Denied", "Login Information");
                this.Close();
Posted
Updated 18-Aug-12 18:25pm
v2
Comments
ridoy 19-Aug-12 2:52am    
Simple solution...follow solution 2 posted here

Hi,
Try adding this when login is successful:
C#
if (dr.HasRows == true)
{
       MessageBox.Show("Login Successfull", "Login Information");
       this.Hide(); //This will hide your current form
       MyWinForm1 M1 = new MyWinForm1();//Create the object of your second form
       M1.Show(); //Show the second form using the object.
}

While closing the file handle the form closing event as I said in my comments below. Use this:
C#
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
  // Display a MsgBox asking the user to save changes or abort.
  if(MessageBox.Show("Do you want to save changes to your text?", "My Application",
     MessageBoxButtons.YesNo) ==  DialogResult.Yes)
  {
     // Cancel the Closing event from closing the form.
     e.Cancel = true;
     Application.Exit();
     // Call method to save file...
  }
}




All the best.
--Amit
 
Share this answer
 
v2
Comments
[no name] 19-Aug-12 2:51am    
Good Job.
My 5!
_Amy 19-Aug-12 3:02am    
Thanks. :)
shaikh-adil 19-Aug-12 3:26am    
hey when i closes the second form but the program is still running.i persnally hae to stop debugging why so bro
_Amy 19-Aug-12 3:36am    
Use Application.Exit(); to exit the application. :)
shaikh-adil 19-Aug-12 3:58am    
i know that but if i want to close the application by clicking the exit button which is there already there in the frame so how can i do that
use your code at Program class. In this code I assume that Form1 is your Start up WinForm.

C#
class Program
{
   int count = 3;
   do
   {
       // show log in form here 3 times if user get mistake at typing
       if (!dr.HasRows)
           Application.Exit();
       else
           break;

   } while (count != 0);

   Application.Run(new Form1());
}
 
Share this answer
 
Comments
shaikh-adil 19-Aug-12 2:55am    
i have to write the whole button click code in program.cs?
[no name] 19-Aug-12 7:55am    
I said "Show the login dialog at Program", not whole the project.
shaikh-adil 19-Aug-12 8:41am    
bro i understood.
But my login dialog contain database connectivity.. Will it work?
I have my code above. Tell me it will work or not. And directly i have to write the whole code in program.cs file?
[no name] 19-Aug-12 13:59pm    
Sure. I used that before myself.
There is no different where you write that.
shaikh-adil 19-Aug-12 22:50pm    
okay i will give you feedback...
Will try after eid
thanx

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