Skip to content
Loading
  • +1
    string constr1 = ConfigurationManager.ConnectionStrings["JPConstr"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(constr1))
    {
    conn.Open();
    SqlCommand cmd = new SqlCommand("select UserId,Password from Sign_Up", conn);
    SqlDataReader rdr = cmd.ExecuteReader();
    while (rdr.Read())
    {
    string User = rdr["UserId"].ToString();
    string Pass = rdr["Password"].ToString();
    if (txtlogin.Text!=User)
    {
    Label3.Text = "It seems you are not registered to us";
    Label3.ForeColor = System.Drawing.Color.Blue;
    }
    else if(txtpassword.Text != Pass)
    {
    Label3.Text = "Invalid user Id Or Password";
    Label3.ForeColor = System.Drawing.Color.Purple;
    }
    }
    conn.Close();
    }
     
    this is the simplest code which should work but not working according to if block unauthorized user should warn and according to else if block if user enters incorrect password but his/her id is right this code block should work i also place above true condition in else if block but not worked.
     
    i don't know what the hack is this.could you suggest karthi what should i do 
    +1
  • +1
    @umair,
     
       Try this,
     
     
    string constr = ConfigurationManager.ConnectionStrings["JPConstr"].ConnectionString;
     
    Public void checklogin()
    {
        DataTable dt1 = filldata();
     
        if(dt1.Rows.Count() > 0)
        {
              //use this if you allow many user can have the same username but diff passwords. so use foreach.
             
                int i = 0; 
                foreach(DataRow dr in dt1.Rows)
                {
                   if(txtpass.Text = dr["password"].ToString()) 
                   {
                       i++;
                   } 
                } 
                
                if(i > 0)
                   //do what you want if password match.
                else
                {
                    //invalid password message box.
                } 
     
             //if you not allow same user name with diff password. I mean the username should not match with any other user name.  use this.
                 
              string pass = " ";
             foreach(DataRow dr in dt1.Rows)
             {
                   pass = dr["Password"].ToString();
             } 
     
             if(txtpass.Text == pass)
             {
                  //do what you want if password match.
             } 
             else
                {
                    //invalid password message box.
                } 
        } 
        else
         {
             //if the user not Registered the dt1.Rows must be 0.   
     
              Label3.Text = "It seems you are not registered or you Entered the Wrong User Name";
              Label3.ForeColor = System.Drawing.Color.Blue;
          }
    } 
     
    Public DataTable filldata()
    { 
    SqlConnection conn = new SqlConnection(constr) 
    SqlCommand cmd = new SqlCommand("select UserId,Password from Sign_Up where UserId=@Id", conn);
    cmd.Parameters.AddWithValue("@Id", txtlogin.Text);
    conn.Open();
    DataTable dt = new DataTable(); 
    SqlDataAdapter da =new SqlDataAdapter(cmd);
    da.Fill(dt);
    conn.Close(); 
    return dt; 
    } 
     
     
    i hope this will help you. 
    +1