locked
error RRS feed

  • Question

  • User736276958 posted

    Hi,

    Through url i am getting uname i my fisrst.aspx as

    uname = Request.QueryString["username"];


    if (uname == null)
    {
    Session["Username"] = "Guest";
    }
    else
    {
    Session["Username"] = uname;
    }

    then i am storing that uname i session and using that uname in multiple pages in my project.

    second.aspx

    string uname = Session["Username"].ToString();

    But some times i am getting object reference error near  string uname = Session["Username"].ToString();

    Please tell me how to currect this.

    also tell me how to use try catch for this.

    Friday, October 30, 2015 1:32 AM

Answers

  • User1428246847 posted

    Your code will handle the error when session == null.

    You can leave the finally out; your code will continue anyway after the catch.

    try
    {
        ...
        ...
    }
    catch (Exception ex)
    {
        ...
    }
    
    if (!IsPostBack)
    {
        if (uname == null )
        {
            //****something**///
        }
        else
        {
            string dname = uname.First().ToString().ToUpper() + uname.Substring(1);
    
            //****something**///
        }
    }
    

    Or possibly better as it will catch any unexpected errors

    string uname = "Guest";
    try { if (Session != null && Session["Username"] != null) { uname = Session["Username"].ToString(); } if (!IsPostBack) { string dname = uname.First().ToString().ToUpper() + uname.Substring(1); //****something**/// } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { // cleanup a possible mess (e.g. an open file handler) // not needed in this example }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 30, 2015 5:56 AM

All replies

  • User1428246847 posted

    In second.aspx, you need to check if 'things' are null. From the head

    string uname = "";
    if (Session["Username"] != null) { uname = Session["Username"].ToString(); }

    In my opinion a try/catch is only a last resort to fix this type of issues; once you have figured out that it can go wrong, you should modify the code to the above. try/catch example below.

    string uname = "";
    try { uname = Session["Username"].ToString(); } catch (Exception ex) { // log error ... // inform user ... }

    Friday, October 30, 2015 2:41 AM
  • User-1716253493 posted

    sometime session can be null because timeout, you can check it not null first before convert it to string.

    Its similar with your Request.QueryString codes

    Friday, October 30, 2015 3:28 AM
  • User1428246847 posted

    sometime session can be null because timeout,
    Thanks for that; I was not sure if that could be the case so left it out.

    Friday, October 30, 2015 3:32 AM
  • User736276958 posted

    try
    {
    if (Session["Username"] != null)
    {
    uname = Session["Username"].ToString();
    }
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.ToString());
    }
    finally
    {
    if (!IsPostBack)
    {

    if (uname == null )
    {
    //****something**///
    }
    else
    {

    string dname = uname.First().ToString().ToUpper() + uname.Substring(1);

    //****something**///
    }
    }
    }

    please tell me weather the code is correct or not?

    and also please tell me how to handle the timeout error

    Friday, October 30, 2015 5:05 AM
  • User1428246847 posted

    Your code will handle the error when session == null.

    You can leave the finally out; your code will continue anyway after the catch.

    try
    {
        ...
        ...
    }
    catch (Exception ex)
    {
        ...
    }
    
    if (!IsPostBack)
    {
        if (uname == null )
        {
            //****something**///
        }
        else
        {
            string dname = uname.First().ToString().ToUpper() + uname.Substring(1);
    
            //****something**///
        }
    }
    

    Or possibly better as it will catch any unexpected errors

    string uname = "Guest";
    try { if (Session != null && Session["Username"] != null) { uname = Session["Username"].ToString(); } if (!IsPostBack) { string dname = uname.First().ToString().ToUpper() + uname.Substring(1); //****something**/// } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { // cleanup a possible mess (e.g. an open file handler) // not needed in this example }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 30, 2015 5:56 AM