0

I am trying to pass multiple session variables to multiple asp pages. However, only the last ImageID and Extention values pass into the asp pages. I need to op

    int key = Convert.ToInt32(StockSummary.SelectedRow.Cells[6].Text);
    int index = 1;
    try
    {

        transportFbConn.Open();

        if (transportFbConn.State == ConnectionState.Closed)
        {
            transportFbConn.Open();
        }
        var sqlquerry = String.Format("select image_key,File_EXT from IMAGE_LIST where SOURCE_PK = {0}", key);
        transportFbCommand = new FbCommand(sqlquerry, transportFbConn);
        transportFbReader = transportFbCommand.ExecuteReader();
        if (transportFbReader.HasRows)
        {
            while (transportFbReader.Read())
            {
                ImageID = transportFbReader.GetString(0);
                extention = transportFbReader.GetString(1);
                //Open PDF:
                if (ImageID != "")
                {
                    Session.Add("IMGID", ImageID);
                    Session.Add("Ext", extention);
                    Response.Write(string.Format("<script>window.open('{0}','_blank');</script>", "Trace"+index+".aspx"));
                }
                else
                {
                    this.ErrorLabel.Text = "No Trace Information found for Part Number : " + this.TextTextBox.Text;
                }
                index++;
            }
        }

I tried the arraylist but I get the object null reference:

 int key = Convert.ToInt32(StockSummary.SelectedRow.Cells[6].Text);
        int index = 1;
        int arrayindex = 0;
        ArrayList imageid = new ArrayList();
        ArrayList extention = new ArrayList();
        try
        {

            transportFbConn.Open();

            if (transportFbConn.State == ConnectionState.Closed)
            {
                transportFbConn.Open();
            }
            var sqlquerry = String.Format("select image_key,File_EXT from IMAGE_LIST where SOURCE_PK = {0}", key);
            transportFbCommand = new FbCommand(sqlquerry, transportFbConn);
            transportFbReader = transportFbCommand.ExecuteReader();
            if (transportFbReader.HasRows)
            {
                while (transportFbReader.Read())
                {
                    imageid.Insert(arrayindex, transportFbReader.GetString(0));
                    extention.Insert(arrayindex, transportFbReader.GetString(1));
                    //Open PDF:
                    if (ImageID[arrayindex].ToString() != "")
                    {
                        Session.Add("IMGID", ImageID[arrayindex]);
                        Session.Add("Ext", extention[arrayindex]);
                        Response.Write(string.Format("<script>window.open('{0}','_blank');</script>", "Trace"+index+".aspx"));
                    }
                    else
                    {
                        this.ErrorLabel.Text = "No Trace Information found for Part Number : " + this.TextTextBox.Text;
                    }
                    index++;
                    arrayindex++;
                }
            }
1
  • The session is a key value store. Are you trying to store more than one value under the same key? Commented Jun 10, 2014 at 16:23

3 Answers 3

1

it's better to create an array that store all keys, values in a temp variable then to store it on the session. because using the same key will overwrite the value

ArrayList ar_IMGID = new ArrayList();
ArrayList ar_Ext = new ArrayList();

while (transportFbReader.Read())
            {
                ImageID = transportFbReader.GetString(0);
                extention = transportFbReader.GetString(1);
                //Open PDF:
                if (ImageID != "")
                {

                    ar_IMGID.Add(ImageID);
                    ar_Ext.Add(extention);
                 }
             }
Session.Add("IMGID", ar_ImageID);
Session.Add("Ext", ar_Ext);
Sign up to request clarification or add additional context in comments.

9 Comments

I tried the arraylist but I get the "Object null reference":
may be you have an error in code.. please send me the code
I get the error on line: if (ImageID[arrayindex].ToString() != "")
No the code is not correct.. you should insert the value to the array list only if it's not null like the code in my answer if it's not clear I can update ur code
or simply add this if (ImageID[arrayindex] != null && ImageID[arrayindex].ToString() != "")
|
1

If there is a session variable with the same key value it is overwritten. So you always update the same key with the newest value.

1 Comment

Right but Im calling the new page each time and it doesnt open the new page until the loop is finished. Thats why the session variables get overwritten: Response.Write(string.Format("<script>window.open('{0}','_blank');</script>", "Trace" + index + ".aspx"));
1

You should put a list of ImageIds in session rather than individual images since you can only have one object represented by the same key. It basically gets overwritten every time through your loop.

1 Comment

How can I execute the opening of a new browser first and then loop through the rest of code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.