HEEN
HEEN

Reputation: 4727

Session conditions are not working properly

I have a scenario where I am inserting values in Gridview row. So when I insert the row for the first time. Acutally the session value is ""

But while checking like this

if (Convert.ToString(Session["RecdInfo"]) != null || Convert.ToString(Session["RecdInfo"]) != "")
    {
        if (strMode == "M")
        {
            FunFillGrid();
            DtRecdInfo = (DataTable)Session["RecdInfo"];
        }
        DtRecdInfo = (DataTable)Session["RecdInfo"];
    }
    else
    {
        BindDataTable();
    }

The condition is getting true as Instead it should get false and go inside the else part.

I tried with Session["RecdInfo"]) but still it is going inside the IF condition. any reasons why it is happening like this ?

Upvotes: 0

Views: 46

Answers (1)

Mairaj Ahmad
Mairaj Ahmad

Reputation: 14624

I think your condition is not correct because if your session is "" than it is not null so condition will be true.

You can try this first check if session is not null than check if it is not empty string

if (Session["RecdInfo"] != null)
{
   if (!string.IsNullOrEmpty(Session["RecdInfo"] as string))
   {

   }
}

Upvotes: 2

Related Questions