1

I'm storing multiple values in session by retrieve & assign values from the session like this :

var imageSessList = (List<string>)Session["ImagesNames"];

if (imageSessList != null)
{
    string image1 = imageSessList[0];
    string image2 = imageSessList[1];
    string image3 = imageSessList[2];
    string image4 = imageSessList[3];
}

but what if the session contain only 3 values, so while assigning string image4 = imageSessList[3]; it throws null error.

how to handle null in such situation.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
ace
  • 225
  • 4
  • 18
  • 2
    Check the length of your list, first. It will also throw an IndexOutOfRange exception, not a null exception. Tempted to close as a duplicate of https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f – Rob May 30 '17 at 07:01

7 Answers7

2

The value in the session may be null so you have to check for null before casting them. so the initial condition would be if (Session["ImagesNames"] != null) Now it is safe to cast them and assign to imageSessList. So the variable imageSessList will contains items in the session variable, and you needed to get those items based on their index, before accessing them its better to check for existence of that array index. So the complete code would be like this:

if (Session["ImagesNames"] != null)
{
    var imageSessList = (List<string>)Session["ImagesNames"];
    string image1 = imageSessList.Count>0? imageSessList[0]:"";
    string image2 = imageSessList.Count>1? imageSessList[1]:"";
    string image3 = imageSessList.Count>2? imageSessList[2]:"";
    string image4 = imageSessList.Count>3? imageSessList[3]:"";
    // Continue the job with these image variables
    // Variables will be "" if those values are not found in the list
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • it worked after putting var imageSessList = (List)Session["ImagesNames"]; at the begininig – ace May 30 '17 at 09:25
  • @ace: The value in the session may be null, So when you try to cast `null` to `List` cause errors, so you should check for null before performing Cast – sujith karivelil May 30 '17 at 10:03
  • 1st i need to retrieve session wethet null or not by var imageSessList = (List)Session["ImagesNames"]; only then i can compare wether it's null or not. – ace May 30 '17 at 10:24
  • @ace: Ok then try your code before assigning values to `Session["ImagesNames"]`; and check how it comes – sujith karivelil May 30 '17 at 10:26
  • Could it also be expressed as var imageSessionList = Session["ImagesNames"] as List; and then check if imageSessionList is not null? – Steven Brookes May 30 '17 at 10:30
0

You can do this for all

var imageSessList = (List<string>)Session["ImagesNames"];


                    if (imageSessList != null)
                    {
                       string img1= imageSessList.Count>0? imageSessList[0]:"";
                    }
too_cool
  • 1,164
  • 8
  • 24
0

if you need to process each file individually, you can use for or foreach loop as below

var imageSessList = Session["ImagesNames"] as List<string>;
foreach(string file in imageSessList)
{
   //do something with file
}

if you need 4 files to process something, check the item Count and proceed

if(imageSessList!=null && imageSessList.Count ==4)
{
    string image1 = imageSessList[0];
    string image2 = imageSessList[1];
    string image3 = imageSessList[2];
    string image4 = imageSessList[3];
}else
{
    // show error message 
}
Damith
  • 62,401
  • 13
  • 102
  • 153
0

Check the below lines of code. By this way, you will not get any error if your Session doesn't contain 4th item, 3rd item or 2nd item. It will bind the item values if the items are there in session list.

var imageSessList = (List<string>)Session["ImagesNames"];
if (imageSessList != null)
{
    for (int i = 0; i < imageSessList.Count; i++)
    {
        if (imageSessList[i] != null && i == 0)                     
        {
            string image1 = imageSessList[i];
        }
        if (imageSessList[i] != null && i == 1)
        {
            string image2 = imageSessList[i];
        }
        if (imageSessList[i] != null && i == 2)
        {
            string image3 = imageSessList[i];
        }
        if (imageSessList[i] != null && i == 3)
        {
            string image4 = imageSessList[i];
        }
    }
}
  • it will then give an error saying image1 doesn't exists in current context, as i'll be using image1 in next steps. it won't be accessible – ace May 30 '17 at 09:11
  • You can define the string variable before this declaration then you would be able to access all of them. – Manoj Deshwal May 30 '17 at 10:50
-1

Make sure you check for existance of that session value first...

if(Session["ImagesNames"] != null){...}

or even better use the as operator...

var imageSessList = Session["ImagesNames"] as List<string>;


            if (imageSessList != null)
            {
                string image1 = imageSessList[0];
                string image2 = imageSessList[1];
                string image3 = imageSessList[2];
                string image4 = imageSessList[3];
            }

then check the desired keys in the list index...make sure you don't try to access a key that doesn't exist in the index...

if(imageSessList.Count > 0) image1 = imageSessList[0];
if(imageSessList.Count > 1) image2 = imageSessList[1];
if(imageSessList.Count > 2) image3 = imageSessList[2];
Leo
  • 14,625
  • 2
  • 37
  • 55
-1

Why not check if the value is null before passing it in your string variable?

string image1 = imageSessList[3] ?? string.Empty;
Willy David Jr
  • 8,604
  • 6
  • 46
  • 57
-2

try this.

 if (imageSessList != null)
                {
                    string image1 = Convert.Tostring(imageSessList[0]);
                    string image2 =  Convert.Tostring(imageSessList[1]);
                    string image3 =  Convert.Tostring(imageSessList[2]);
                    string image4 =  Convert.Tostring(imageSessList[3]);
                }
SynozeN Technologies
  • 1,337
  • 1
  • 14
  • 19