Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do i pull values from a dataset placed in a session?

Status
Not open for further replies.

citizenzen

Programmer
Jun 28, 2007
102
US
Hello.
Earlier I posted a question about using sessions, but I am still having issues.

I have 2 pages:
page1.aspx and page2.aspx

on page1.aspx, the user locates media. The located media needs to be passed to page2.aspx for checkout. this media will be placed in a new datagrid with the session values and it is extracted from a dataset. So, it seemed like a great idea to place the dataset into a session on page1.aspx. But the sessions is not being recognized at all. I also tried to place on the values from the session:

Code:
page1.aspx;
//also tried the following
                Session.Clear();
Session.Add("MyData", locmedia);
                Session.Clear();
                Session.Add("Video", strVideoout);
                Session.Add("TapeID", intTapeIDout);

page2.aspx;
//testing
        string video = Session["Video"].ToString();
        string tapenum = Session["TapeID"].ToString();
        DataSet dstData = (DataSet)(Session["MyData"]);

//placed here for testing only to determine if sessions really exist

        Response.Write(Session.Count + "<br>");
        for (int i = 0; i < Session.Count; i++)
        {
            Response.Write(Session.Keys[i].ToString() + "<br>");
        }

Error message from page2.aspx:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Code:
web.config
  <system.web>
    <sessionState mode="InProc" />

Important Issues:
1) How do I resolve the issue with the sessions? Is it my syntax?
2) Why are the sessions not being recognized? I have seen examples where a dataset is placed in a session, but I can't figure out how to grab the values on the following page.
3) Should the extracted sessions not be placed in the page load event?
4) How do i successfully extract values from the dataset that is in the session?

 
at what line on page2 does the error occur. I suspect it is
Code:
DataSet dstData = (DataSet)(Session["MyData"]);
since you clear the session on page1 after you add it.
Code:
Session.Add("MyData", locmedia);
                Session.Clear();
 
i have this, but how do i extract the values from a sessioned dataTable? I changed the dataset to a datatable on page2.aspx. but how would I read from it so that I can bind my datagrid or do other things?

I have:

protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<strong>Last Updated:</strong> " + DateTime.Now + "<br>");

DataTable dstData = (DataTable)Session["MyData"];

Response.Write("There are " + Session.Count + " Sessions<br>");

for (int i = 0; i < Session.Count; i++)
{
Response.Write("There are " + Session.Keys.ToString() + " keys.<br>");
Response.Write(dstData.Rows);

}
}

and I get this as the output:

Last Updated: 8/26/2007 2:11:23 PM
There is 1 Session.
There are MyData keys. //this is correct
System.Data.DataRowCollection //this is incorrect.
 
I was able to bind my sessioned dataset to a gridview. however, the gridview repeats the data to the right, even though my checker says there are only 2 rows. why does this happen?

I am binding the datatable to the gridview, is there a control i need to turn off? i have auto-generate fields checked off.

Code:
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("<strong>Last Updated:</strong> " + DateTime.Now + "<br>");
                
        DataTable dstData = (DataTable)Session["MyData"];

        Response.Write("There are " + Session.Count + " Sessions<br>");

        for (int i = 0; i < Session.Count; i++)
        {
            Response.Write("There are " + Session.Keys[i].ToString() + " keys.<br>");             
        }
        Response.Write("There are " + dstData.Rows.Count + " rows in the datatable");

            matchedGrid.DataSource = dstData;
            matchedGrid.AutoGenerateColumns = false;
            matchedGrid.DataKeyNames = new string[] { "ItemID" };         

            BoundField mr1 = new BoundField();
            BoundField mr2 = new BoundField();
            BoundField mr3 = new BoundField();

            mr1.DataField = "ItemID";
            mr1.HeaderText = "Tape#";
            mr1.ReadOnly = true;

            mr2.DataField = "Barcode";
            mr2.HeaderText = "Barcode";
            mr2.ReadOnly = true;

            mr3.DataField = "Title";
            mr3.HeaderText = "Video";
            mr3.ReadOnly = true;

            matchedGrid.Columns.Add(mr1);
            matchedGrid.Columns.Add(mr2);
            matchedGrid.Columns.Add(mr3);
            matchedGrid.DataBind();

    }

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top