Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have two window forms. first is login form and second one is main form in .net.
once i enter userid and password so second form opens but first one (login form does not hide. it remains visible )
i have applied both method and property many times but it does not work in my codes. please give me a right solution

What I have tried:

C#
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                con = new SqlConnection("Data Source=.;Initial Catalog=DCID;Integrated Security=True");
                string query = "select * from login where username='" + textBox1.Text + "' and password='" + textBox2.Text + "'";
                cmd = new SqlCommand(query, con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
               

                if (dt.Rows[0]["username"].ToString() == textBox1.Text && dt.Rows[0]["password"].ToString() == textBox2.Text)
                {

                   
                    Form6 obj = new Form6();
                    Form1 objform1 = new Form1();

                 
                  
                    obj.Show();
                    objform1.Visible = false;
                    objform1.Hide();
                    visiblemethod();
                   
                    textBox2.Text = "";
                    textBox1.Text = "";
                    


                }
                else if (dt.Rows[1]["username"].ToString() == textBox1.Text && dt.Rows[1]["password"].ToString() == textBox2.Text)
                {
                    //Form4 obj4 = new Form4();
                    //obj4.Show();
                    clearall();
                  
                }
                else
                {
                    MessageBox.Show("Match does not same");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

           
        }
Posted
v2

For starters, don't do it like that!
There are so many things wrong here it's difficult to know where to start!
Let's go with the really big one: SQL Injection.
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. See here: xkcd: Exploits of a Mom[^]

The second one is nearly as big: Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^].
See here: http://www.commitstrip.com/wp-content/uploads/2013/09/Strips-Erreur-au-pilori-001-650-finalenglish.jpg[^]

Next, don't hardcode connection strings. They make it very difficult to test your code before you release it to production! Use settings or configuration files instead.

Now we get to the problem you have noticed.
C#
Form6 obj = new Form6();
Form1 objform1 = new Form1();
obj.Show();
objform1.Visible = false;
objform1.Hide();
visiblemethod();
Why are you creating a new instance of a form in order to hide it, when it is never displayed?
Try this instead:
C#
Form6 obj = new Form6();
obj.Show();
Hide();
visiblemethod();
That will hide the current instance of the form - i.e. the one the user typed in - rather than a brand new one...

BTW: Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
 
Share this answer
 
I think you need to define your Forms variables outside of the button1_Click() method.
Preferably right after the Class definition as
C#
Public Form6 obj = new Form6();
 
Share this answer
 
You are doing wrong in second line. No need to create object of form1.
C#
Form6 obj = new Form6();
Form1 objform1 = new Form1();

obj.Show();
objform1.Visible = false;
objform1.Hide();
visiblemethod();

textBox2.Text = "";
textBox1.Text = "";

Here is suggested code
C#
Form6 obj = new Form6();
obj.Show();
this.Hide();
visiblemethod();
 
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