Shalvin Interests

Sunday, February 9, 2014

ExecuteScalar with Asp .Net


CREATE TABLE Users(
    UserId int IDENTITY primary key,
    UserName varchar(50),
    Password varchar(50))

using System.Data.SqlClient;
using System.Configuration;


SqlConnection cnn;

protected void btnLogin_Click(object sender, EventArgs e)
{
using (cnn = new SqlConnection(
@"Data Source=.\sqlexpress;Initial Catalog=CustomLogin;Integrated Security=True"))
{
  SqlCommand cmd;
  cnn.Open();
  cmd = new SqlCommand(
   "select Count(*) from Users where UserName = @UserName and Password = @Password", 
   cnn);
  

  cmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
 cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
 int intCount = Int32.Parse( cmd.ExecuteScalar().ToString());

 if (intCount == 1)
  Response.Redirect("Welcome.aspx");
 else
  lblMessage.Text = "Invalid credentials";
}

No comments:

Post a Comment