I am trying to login into the system
I have created 3 pages
1.Registration Page
2.Login Page
3.Secure Page
In registration page I can enter new username and password and that gets registered
well that works fine..but when i try to login with that username and password
I get an error
"Object reference not set to an instance of object"
Here is my code for Login page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionstring"].ConnectionString);
con.Open();
string cmdStr = "select count(*) from Registration where UserName='" + TextBox1.Text + " ' ";
SqlCommand Checkuser = new SqlCommand(cmdStr, con);
int temp = Convert.ToInt32(Checkuser.ExecuteScalar().ToString());
if (temp == 1)
{
string cmdStr2 = "Select password from Registration where UserName=' " + TextBox1.Text + " ' ";
SqlCommand pass = new SqlCommand(cmdStr2, con);
string Password = pass.ExecuteScalar().ToString();
con.Close();
if (Password == TextBox2.Text)
{
Session["New"] = TextBox1.Text;
Response.Redirect("Secure.aspx");
}
else
{
Label1.Visible = true;
Label1.Text = "Invalid Password";
}
}
else
{
Label1.Visible = true;
Label1.Text = "Invalid UserName";
}
}
}
I am getting error at the line
string Password = pass.ExecuteScalar().ToString();
Please help