Hi,

i am using c# window application

I have create one Login Form. I want that when user put its username and password first it should check in the database and then it should give the out put. i have even create on signup form first it should be register with that form then he can go to login form.

The code i have written is

private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(@"Server=xxx\SQLEXPRESS;Database=xxx;Trusted_Connection= True");
            conn.Open();
            SqlCommand cmd = new SqlCommand("select * from signin where username='" + txtusername.Text + "'and password='" + txtpassword.Text + "'", conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            if
            {
                MessageBox.Show("You have successfully Login");
                Form2 f1 = new Form2();
                f1.Show();

                {
                    MessageBox.Show("Incorrect Username and Password");
                }


            }
        }

I am stuck in If else statement i am not getting what i write in that and even i am confused in how do i merge this table and give the output as i want i have directly insert the admin and password in sql but other its are not happening and even i have inner join the table but i am not getting the out put which i want i am very confused.


Hope i have am clear with what i want please help me please

hello, Nemo
try this code...

private void button1_Click(object sender, EventArgs e)
        {

            if (textBox1.Text == "")
            {
                label5.Text = "Please Enter Your Login ID!";
                textBox1.Focus();
            }
            else if (textBox2.Text == "")
            {
                label5.Text = "Please Enter Your Password!";
                textBox2.Focus();

            }

            else
            {

                con = new SqlConnection("Data Source=.;Initial Catalog=INOXHOTEL;User ID=sa;Password=niitdelhi ");

                cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "Select * from LoginUser where LoginID='" + textBox1.Text + "' and Password='" + textBox2.Text + "'";
                con.Open();
                result = cmd.ExecuteNonQuery();

                dr = cmd.ExecuteReader();



                while (dr.Read())
                {

                    login = dr[0].ToString();
                    password = dr[1].ToString();

                }


                dr.Close();
                con.Close();



                if (textBox1.Text == login)
                {
                    if (textBox2.Text == password)
                    {
                        userid = textBox1.Text;
                        userpwd = textBox2.Text;
                        // m1.Show();
                        MainForm m = new MainForm();
                        this.Hide();
                        m.Show();
                    }



                }

                else
                {
                    label5.Text = "Please Enter Correct Login ID And Password!";
                }

            }
}

May be help full...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.