0
votes

I am having trouble passing an entire Gridview to another page using a session. The way I am currently doing it, is by storing the datasource in a datatable and passing the datatable through a session.

Here is my code:

1st Page:

Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click

Dim tbl As DataTable = CType(gvSessionParts.DataSource, DataTable)

Session("dtCart") = tbl

If Request.QueryString("Customer") = "" Then

    Response.Redirect("Orderreview.aspx"

Else

    Response.Redirect("Orderreview.aspx?Customer=" & Request.QueryString("Customer") & "")

End If

End Sub

2nd Page (To grab Session and populate gridview)

        If Not IsPostBack Then

            Dim dt2 As DataTable = CType(Session("dtCart"), DataTable)

            dt2.Columns.Add(New DataColumn("PartNumber", GetType(System.String)))
            dt2.Columns.Add(New DataColumn("Line", GetType(System.String)))
            dt2.Columns.Add(New DataColumn("Description", GetType(System.String)))
            dt2.Columns.Add(New DataColumn("Description2", GetType(System.String)))
            dt2.Columns.Add(New DataColumn("Qty", GetType(System.String)))
            dt2.Columns.Add(New DataColumn("Price", GetType(System.String)))

            gvSessionParts.DataSource = dt2
            gvSessionParts.DataBind()

End If

What am I doing wrong? Why isn't this working?

I get an error:

Object reference not set to an instance of an object.

Line 21: dt2.Columns.Add(New DataColumn("PartNumber", GetType(System.String)))

I feel like I am passing this the wrong way. I do use a ViewState to allow me to keep adding parts to the Gridview on the 1st Page.

1
I'd say: NOT gridview! You should pass data instead of gridview.Maciej Los
@MaciejLos I do not know how to do this? I am trying to pass all the data in the Gridview. Not just one row.Carl Roe

1 Answers

0
votes

You must pass its values, not the control itself.

It's not possible...

Or redraw again all data in a new gridview control, using a new dataset.