How to verifying Login details c#.Net

Verifying username and password:
  In the windows or web applications verifying user login details is an important validation. With this only we can allow the user authentication for entering into the Application. For thal you have to write the code to authenticate the username and password which are allready contains in the database.

Event:Write the code in login button_Click event.

 SqlConnection cn = new SqlConnection(@"Data Source=CENSYS10\SQLEXPRESS;Integrated         Security=true;Initial Catalog=master");
 cn.Open();

            SqlCommand cmd = new SqlCommand("select * from table1 where username = @username      and password = @password", cn);
            cmd.Parameters.AddWithValue("@username", textBox1.Text);
            cmd.Parameters.AddWithValue("@password", textBox2.Text);
            da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
                        if (dt.Rows.Count > 0)
            {

                //Your function here.
               // da = new SqlDataAdapter("select * from new", cn);
                //da.Fill(ds, "new");
                //SqlCommandBuilder build = new SqlCommandBuilder(da);
                //dataGridView1.DataSource = ds.Tables[0];

                MessageBox.Show("Login Success");

            }
            else
            {
                MessageBox.Show("Invalid username or password");
            }


I hope this code will help you.

1 comment: