Saturday, 28 November 2015

Check Login details from Database using Asp.Net C#



protected void btnLogin_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=.;database=DemoDB;integrated security=true");
            SqlCommand cmd = new SqlCommand("select * from tblLogin where Username=@UName and Password=@Pwd", con);
            cmd.Parameters.Add(new SqlParameter("@UName", txtUsername.Text));
            cmd.Parameters.Add(new SqlParameter("@Pwd", txtPassword.Text));
            con.Open();

            int NoOfRowsAffected = (int)cmd.ExecuteScalar();
            con.Close();

            if (NoOfRowsAffected > 0)
            {
                Response.Redirect("Homepage.aspx");
                //Server.Transfer("Homepage.aspx");
            }
            else
            {
                lblResult.ForeColor = System.Drawing.Color.Red;
                lblResult.Text = "Invalid User Name or password";
            }
        }

No comments:

Post a Comment