i am using query string to send pagename in to another page, if pagename is equal to intended page then it would be redirected to mentioned url else if pagename (querystring) is null then page would be redirected to some other page , so first condition is
working but not the second one, when query string is not set, i get error,
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 23: protected void btnsubmit_Click(object sender, EventArgs e) Line 24: { Line 25: String pageName = Request.QueryString["pageName"].ToString();
ASPX code :
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class loginApplicant : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
String pageName = Request.QueryString["pageName"].ToString();
int advId = int.Parse(Request.QueryString["advId"]);
String hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(txtboxPassword.Text, "SHA1");
//Response.Write(hashedPwd);
String conString = "Data Source=COSANOSTRA;Initial Catalog=Waleed_orsfinal;Integrated Security=True";
SqlConnection con = new SqlConnection(conString);
String query = "select email,password from tblUser where email='" + txtboxUserEmail.Text + "' and password= '" + hashedPwd + "' ";
SqlCommand com = new SqlCommand(query, con);
try
{
con.Open();
SqlDataReader dr = com.ExecuteReader();
if (dr.Read() == true && pageName == "page_applyForJob")
{
Session["applicantSession"] = txtboxUserEmail.Text;
Response.Redirect("applyForJob.aspx?advId=" + advId);
}
else if (dr.Read() == true && pageName== null ) { Session["applicantSession"] = txtboxUserEmail.Text; Response.Redirect("~/showAdvertsiement.aspx"); }
else
{
Response.Write("Wrong Combination Of Email and Password");
}
}
catch (Exception ex)
{
Response.Write("error" + ex.Message);
}
finally
{
con.Close();
}
}
}
help please, in situation of null value i want it to redirect to mentioned page
instead of equating it to null, equat it to string.empty, then check for pageName == string.empty in the Else If condition....also make sure your dr.Read() is true or else it will not enter into that condition...
Hunain Hafee...
Member
238 Points
639 Posts
query string Null exception
Nov 07, 2012 08:08 PM|LINK
i am using query string to send pagename in to another page, if pagename is equal to intended page then it would be redirected to mentioned url else if pagename (querystring) is null then page would be redirected to some other page , so first condition is working but not the second one, when query string is not set, i get error,
Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 23: protected void btnsubmit_Click(object sender, EventArgs e) Line 24: { Line 25: String pageName = Request.QueryString["pageName"].ToString();ASPX code :
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; public partial class loginApplicant : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnsubmit_Click(object sender, EventArgs e) { String pageName = Request.QueryString["pageName"].ToString(); int advId = int.Parse(Request.QueryString["advId"]); String hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(txtboxPassword.Text, "SHA1"); //Response.Write(hashedPwd); String conString = "Data Source=COSANOSTRA;Initial Catalog=Waleed_orsfinal;Integrated Security=True"; SqlConnection con = new SqlConnection(conString); String query = "select email,password from tblUser where email='" + txtboxUserEmail.Text + "' and password= '" + hashedPwd + "' "; SqlCommand com = new SqlCommand(query, con); try { con.Open(); SqlDataReader dr = com.ExecuteReader(); if (dr.Read() == true && pageName == "page_applyForJob") { Session["applicantSession"] = txtboxUserEmail.Text; Response.Redirect("applyForJob.aspx?advId=" + advId); } else if (dr.Read() == true && pageName== null ) { Session["applicantSession"] = txtboxUserEmail.Text; Response.Redirect("~/showAdvertsiement.aspx"); } else { Response.Write("Wrong Combination Of Email and Password"); } } catch (Exception ex) { Response.Write("error" + ex.Message); } finally { con.Close(); } } }help please, in situation of null value i want it to redirect to mentioned page
inquisitive_...
Contributor
3421 Points
575 Posts
Re: query string Null exception
Nov 07, 2012 08:46 PM|LINK
Check for null reference
while(1)
Hunain Hafee...
Member
238 Points
639 Posts
Re: query string Null exception
Nov 07, 2012 08:53 PM|LINK
thanks but little problem occurred but Thank God, that error disappeared,
now problem is that only else block is executed when i put pageName equals to null + putting ur code in my code,
but without pagename equal to null, it logins successfully, y ?
now my code is this :
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; public partial class loginApplicant : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnsubmit_Click(object sender, EventArgs e) { // String pageName = Request.QueryString["pageName"].ToString(); // int advId = int.Parse(Request.QueryString["advId"]); String pageName = Request.QueryString["pageName"] == null ? null : Request.QueryString["pageName"].ToString(); int advId = Request.QueryString["advId"] == null ? 0 : int.Parse(Request.QueryString["advId"]); String hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(txtboxPassword.Text, "SHA1"); //Response.Write(hashedPwd); String conString = "Data Source=COSANOSTRA;Initial Catalog=Waleed_orsfinal;Integrated Security=True"; SqlConnection con = new SqlConnection(conString); String query = "select email,password from tblUser where email='" + txtboxUserEmail.Text + "' and password= '" + hashedPwd + "' "; SqlCommand com = new SqlCommand(query, con); try { con.Open(); SqlDataReader dr = com.ExecuteReader(); if (dr.Read() == true && pageName == "page_applyForJob") { Session["applicantSession"] = txtboxUserEmail.Text; Response.Redirect("applyForJob.aspx?advId=" + advId); } else if (dr.Read() == true && pageName== null) { Session["applicantSession"] = txtboxUserEmail.Text; Response.Redirect("~/showAdvertsiement.aspx"); } else { Response.Write("Wrong Combination Of Email and Password"); } } catch (Exception ex) { Response.Write("error" + ex.Message); } finally { con.Close(); } } }chek bold section, it is executed when i place pagename= null, and even no parameter is passing to it
ramiramilu
All-Star
98023 Points
14516 Posts
Re: query string Null exception
Nov 08, 2012 07:46 AM|LINK
instead of equating it to null, equat it to string.empty, then check for pageName == string.empty in the Else If condition....also make sure your dr.Read() is true or else it will not enter into that condition...
i would rather say use String.IsNullOrWhiteSpace to check whether string is empty or not...that is more easier way...http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx
Thanks,
JumpStart
UjjwalMeshra...
Participant
1179 Points
293 Posts
Re: query string Null exception
Nov 08, 2012 08:07 AM|LINK
hi, first check for dr.read() then check for pagename in two different if statements. like below
if (dr.Read() == true) { if (!string.IsNullOrEmpty(pageName)) { if(pageName == "page_applyForJob") { Session["applicantSession"] = txtboxUserEmail.Text; Response.Redirect("applyForJob.aspx?advId=" + advId); } } else { Session["applicantSession"] = txtboxUserEmail.Text; Response.Redirect("~/showAdvertsiement.aspx"); } } else { Response.Write("Wrong Combination Of Email and Password"); }Regards
Hunain Hafee...
Member
238 Points
639 Posts
Re: query string Null exception
Nov 09, 2012 06:06 PM|LINK
ok thanks