-3

I am a little stumped at the moment. i have two label controls on my web app that am trying to assign some text values to, the first label works just fine while the second doesn't. the controls are both referenced during the masterpage Load event, see code below

protected void Page_Load(object sender, EventArgs e)
{
    string id = Convert.ToString(Session["id"]);
    //string rtn = Convert.ToString(GetRequestsCount(id));
    //StringBuilder rtn = new StringBuilder();
    //rtn.Append(id);
    int display = GetRequestsCount(id);

    if (!this.IsPostBack) //prevent post back
    {
        if (string.IsNullOrEmpty(Session["id"].ToString()))
        {
            Response.Redirect("welcome.aspx", true);
        }
        else
        {
            lblUser.Text = Session["FName"].ToString() + " " + Convert.ToString(Session["LName"]) + " {" + id + "}";
            Label1.Text = string.Concat(Convert.ToString(display), " New Request(s)");
        }

public int GetRequestsCount(string id)
{
    string query = "SELECT count(*) FROM TableWHERE username='" + id + "' AND (isActive ='False')";
    int count = 0;

    try
    {
        using (SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString))
        {
            using (SqlCommand cmdCount = new SqlCommand(query, thisConnection))
            {
                thisConnection.Open();
                count = (int)cmdCount.ExecuteScalar();
            }
        }
        return count;
    }
    catch (NullReferenceException ex)
    {

        throw;
    }
}
Tim
  • 41,901
  • 18
  • 127
  • 145
wisemikky
  • 11
  • 1
  • 2
    You should write which line triggers the exception. – Kamil T May 12 '14 at 12:56
  • Show ALL relevant code. If you debug, where does it go wrong? Does it crash? – L-Four May 12 '14 at 12:57
  • The actual line where the error occurs is thisLabel1.Text = string.Concat(Convert.ToString(display), " New Request(s)"); – wisemikky May 12 '14 at 13:01
  • What error??? Be more specific – L-Four May 12 '14 at 13:03
  • NullReferenceException, Object reference not set to an instance of an object. – wisemikky May 12 '14 at 13:05
  • 1
    What is NULL? Just DEBUG and see what is NULL and there you are! Probably thisLabel1 or display is NULL. thisLabel1 is not even in the code you mentioned. – L-Four May 12 '14 at 13:06
  • the error is with Label1, in this line Label1.Text = string.Concat(Convert.ToString(display), " New Request(s)"); Although the display variable has value – wisemikky May 12 '14 at 13:16
  • You still didn't post all the relevant code. – L-Four May 12 '14 at 13:19
  • All null reference exceptions are for the same reason, you're trying to do some operation on an object that is null. Therefore, you merely have to run the code in the debugger to see what's null. – mason May 12 '14 at 13:44
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 12 '14 at 16:23

2 Answers2

0

My best guess, without a line number, is this is the problem...

ConfigurationManager.ConnectionStrings["con"].ConnectionString

Check you actually have a connection string with this name in Web.config

Justin Harvey
  • 14,446
  • 2
  • 27
  • 30
0

If the NullReferenceException is being thrown when attempting to access Label1, you need to check whether or not the Label control exists before attempting to change its Text value

if (Label1 != null)
{
    Label1.Text = string.Concat(Convert.ToString(display), " New Request(s)");
}

Or you need to explicitly declare the label in your code behind:

Label lblRequests = (Label) <container holding Label>.FindControl("Label1");