Entered Password is incorrect getting an error using asp.net

Posted by Mandlaa under ASP.NET on 11/8/2013 | Points: 10 | Views : 1947 | Status : [Member] | Replies : 8
I doing this code for login,

For suppose i am enterd incorrect Email and password getting an error like below

ERROR:Object reference not set to an instance of an object.

string strCon = ConfigurationManager.ConnectionStrings["stgdbConnectionString"].ConnectionString;
string strSelect = "SELECT ClientId FROM CLIENT WHERE Email = @Email AND Password = @Password";
//string strSelect1 = "select ClientId from CLIENT where Email = @Email AND Password = @Password";

SqlConnection con = new SqlConnection(strCon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSelect;


SqlParameter email = new SqlParameter("@Email", SqlDbType.VarChar, 50);
email.Value = txtEmail.Text.Trim().ToString();
cmd.Parameters.Add(email);

SqlParameter password = new SqlParameter("@Password", SqlDbType.VarChar, 50);
password.Value = txtPwd.Text.Trim().ToString();
cmd.Parameters.Add(password);

con.Open();
//vali();

clientid = cmd.ExecuteScalar().ToString();


con.Close();

if (clientid != null)
{
//vvv();
Session["Emailid"] = txtEmail.Text.ToString().Trim();
Session["ClientId"] = clientid;

//Session["ClientId"] = ClientId.TypeName.Trim().ToString();
////Session["ClientId"] = strSelect1;
Response.Redirect("WebForm1.aspx");
}
else
lblMsg.Text = "Incorrect EmailId or Password";
}




Responses

Posted by: Allemahesh on: 11/8/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Mandlaa,

I have see your code. Please replace the below statement with new one

clientid = cmd.ExecuteScalar().ToString(); 

Replace with below one:-
clientid = Convert.ToString(cmd.ExecuteScalar());


The region is if you try to enter invalied useid and pwd, the cmd.ExecuteScalar() will return null value and .ToString() will not handle null values. So use Convert.ToString(cmd.ExecuteScalar());.

Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 11/8/2013 [Member] Starter | Points: 25

Up
0
Down
I am writing this code Without enter email and password also its working


Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 11/8/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
If it helps you or directs U towards the solution, Please click on MARK IT AS ANSWER

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 11/8/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi,

Convert.toString() and String()
Convert.Tostring handles null if result has an null value, then it does not throw an error but
Tostring() throws an error if result has an null value.

so always cast as convert.tostring("your value");

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 11/8/2013 [Member] Starter | Points: 25

Up
0
Down
it's not working,
With out enter email&password also the code working,
I want enter email&password is incorrect display error message

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/8/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
That is happended because of ToString().. Don't call ToString() directly on ExecuteScalar().

NOTE: Just remove .ToString() method....
con.Open();

object clientid = cmd.ExecuteScalar();

con.Close();

if (clientid != null)
{


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 11/8/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Mandlaa,

Just change the below one with new one.

if (clientid != null)


to

if (clientid != null && clientid != "")


Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 11/8/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi, do the following

Object clientid;
clientid = cmd.ExecuteScaler();

//check
if(clientid!=null)
clientid = convert.toint32(clientid);

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response