-1

why is the form named (Smart_pharmacy) being closed when I execute this code :

private void LoginBTN_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("Data Source=Abdullah-PC;Initial Catalog=SmartPharmacyDB;Integrated Security=True");
    SqlCommand com = new SqlCommand();
    com.Connection = con;
    com.CommandText = "select userpass from usertab where username = @username";
    com.Parameters.Add("@username", usernametxt.Text);
    con.Open();
    object returnedvalue = com.ExecuteScalar();

    if (returnedvalue != null)
    {
        string returneduserpass = com.ExecuteScalar().ToString();
        con.Close();

        if (returneduserpass == userpasstxt.Text)
        {
            Smart_Pharmacy f = new Smart_Pharmacy();
            f.Show();
            this.Close();
        }
        else
        {
            MessageBox.Show("Incorrect username or password !");
        }
    }
    else
    {
        MessageBox.Show("Incorrect username or password !");
    }
}

I want the current form to be closed and keep the form (Smart_Pharmacy) opened please help.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
alabasi
  • 81
  • 1
  • 2
  • 9
  • One possible solution is to validate with that form in program.cs, then pass an instance of Smart_Pharmacy to Application.Run(). – Idle_Mind Dec 10 '13 at 15:53
  • possible duplicate of [c# open a new form then close the current form?](http://stackoverflow.com/questions/5548746/c-sharp-open-a-new-form-then-close-the-current-form) – Dylan Meeus Dec 10 '13 at 15:53

4 Answers4

4

I assume you have this code in your main form (one which is passed to Application.Run method in your Main method). When main application form is closed, all other forms are closed as well and application terminates.

What you should do is change application main form to Smart_Pharmacy. And close application if login failed. Like this:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    using(LoginForm loginForm = new LoginForm())
    {
        if (loginForm.ShowDialog() != DialogResult.OK)
            return; // exit applicatioin if login failed
    }

    // if login successfully this start main form
    Application.Run(new Smart_Pharmacy());
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • @downvoter can you explain where I went wrong? – Sergey Berezovskiy Dec 10 '13 at 16:25
  • 1
    Your diagnosis of the problem is fine; your proposed solution is a bad idea. He's done with the form; he shouldn't be keeping it around hidden for the duration of the application. The solution is to simply change what the main form is. – Servy Dec 10 '13 at 16:27
  • @Servy I thought OP will need current form later. But after re-reading question again I see that this looks like login form, and `Smart_Pharmacy` is a main form. So, agree with you, hiding is not good for login form – Sergey Berezovskiy Dec 10 '13 at 16:29
1

I would say its because you created the Smart_Pharmacy form within the form that is then closed (ie. destroyed).

So the code does what you are telling it to...kill current form. Since the form is destroyed all objects referenced only within this form get destroyed too.

If you want the other form to stay open then you will have to keep a reference to it somewhere else.

PS: On an unrelated note, this code is horrible. You really shouldn't connect to db from button clicks and such. If you are in a position to revise this I would strongly suggest to separate your business logic from UI code.

Technically this works but you should always try to keep layered and easily maintainable.

Alex
  • 1,110
  • 8
  • 15
  • Not true. Shown form will be added to application opened forms and will stay there when you'll close other (not main) form which created it – Sergey Berezovskiy Dec 10 '13 at 16:12
  • Hm. I'll admit I haven't done anything in winforms in ages so I'll agree with you :) However, if the login form is the main form my answer still stands...albeit slightly tarnished. – Alex Dec 10 '13 at 16:17
0

Change to

 Smart_Pharmacy f = new Smart_Pharmacy();
 f.ShowDialog(this);
 this.Close();

So this.Close() will execute after the Smart_Pharmacy form closed

if you need main form to be hidden, use this.WindowState = FormWindowState.Minimized; before opening Smart_Pharmacy

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
0

I've had the same problem. I'm guessing that your login form is set as your start up form. In which case you would need to Hide it rather than close it.

Justin Adkins
  • 1,214
  • 2
  • 23
  • 41
  • yes it is set as the start up form. – alabasi Dec 10 '13 at 15:58
  • As @lazyberezovsky said, I would simply Hide the form rather than closing it. `this.Hide();` Just make sure that when you close your application you remember that your Login form is still "open" jut hidden. – Justin Adkins Dec 10 '13 at 15:59