Thursday, 19 September 2013

Login code for c#.net


Login code three ways in c#.net


first method:


private void Loginbutton_Click(object sender, EventArgs e)
        {
            con.Open();
          
            SqlDataAdapter da=new SqlDataAdapter("select * from login where username='"+txtusername .Text .Trim ()+"' and password='"+txtpassword .Text .Trim ()+"'",con);
            DataTable dt=new DataTable ();
            da.Fill (dt);
            if(dt.Rows.Count >0)
            {
                Form1 fr=new Form1 ();
                fr.ShowDialog ();
            }
            else
            {
                MessageBox .Show ("please enter correct formate");
            }
        }


Second method:


private void LoginButton_Click(object sender, EventArgs e)
        {
            try
            {
                SqlDataAdapter da = new SqlDataAdapter("select * from login where username='" + txtuser_name.Text.Trim() + "' and password='" + txt_password.Text.Trim() + "'", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                int count = dt.Rows.Count;
                if (count == 0)
                {
                    MessageBox.Show("Invalid user name and password");
                    txt_password.Text = "";
                    txtuser_name.Text = "";

                }
                else if (count == 1)
                {
                    Form2 fr = new Form2();
                    fr.ShowDialog();
                    txt_password.Text = "";
                    txtuser_name.Text = "";

                }
            }
            catch (Exception er)
            {
                MessageBox.Show(er.Message);
            }
        }

Third Method:


 protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        con.Open();
        if (TextBox3.Text != "" && TextBox4.Text != "")
        {
            SqlCommand cmd = new SqlCommand("select * from AdminLogin where Pwd = '" + TextBox4.Text + "'and  UName = '" + TextBox3.Text + "'", con);
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            if (dr.HasRows)
            {

               Form2 fr = new Form2();
                    fr.ShowDialog();
                    TextBox3.Text = "";
                   TextBox4.Text = "";
            }
            else
            {
                MessageBox.Show("Invalid user name and password");
TextBox3.Text = "";
                   TextBox4.Text = "";

            }
        }
    }


No comments:

Post a Comment