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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataTable in Session Bonbs out (sometimes)

Status
Not open for further replies.
Jan 9, 2003
147
US
Hi,

I have a datatable stored in the session that is bound to a datagrid with paging enabled. When you first load the page you can click through the datagrid pages fine, but eventually (maybe after 9 clicks) you'll find that the datatable in the session suddenly has zero rows (and the datagrid bind fails because there are no pages to display). The variable is still there in the session, and it's still a DataTable type, there're just no rows.

However, if you click to view another page on the datagrid it will work. Click again...no. It continues on this pattern forever, back and forth, working and not working. All the other session variables (which are all strings) are fine.

I searched through the code. There is only one place that the session variable is set. Everything else is a read.

This behaviour is exhibited both on my development machine (WinXP Pro / 1GB RAM) and the development server (W2003 / dual processors, tons of RAM, etc).

Very confused and frustrated...

Thanks,
-FD

 
Can you post the code of where you set the session variable and the code where you grab the session variable?

Jim
 
Sure,

I've been thinking about this all last night, and I'm still stumped...

Note: ShowError is a procedure that writes errors to a nice panel on every page


Two procedures to enable paging on the datagrid and getting the datatable out of the session to bind to the datagrid:
Code:
   Private Sub ShowData()
      Try

         If CType(Session("ValidatedDataTable"), DataTable).Rows.Count < 1 Then
            ShowError("The data table contains zero rows???")
            Exit Sub
         End If


         dgTest.DataSource = CType(Session("ValidatedDataTable"), DataTable)
         dgTest.DataBind()
      Catch ex As Exception
         ShowError("Error binding to data...<br><hr>" & "dgTest.Pages:" & dgTest.PageCount & "<br>PageIndex: " & dgTest.CurrentPageIndex & "<br><hr>" & ex.ToString)
      End Try
   End Sub



   Private Sub dgTest_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgTest.PageIndexChanged
      dgTest.CurrentPageIndex = e.NewPageIndex
      ShowData()
   End Sub


Here's where the session variable is set. The code is contained within the click procedure for a button. This is actually the end of a longer procedure that uploads a csv file and validates it against a custom business object (A datatable is returned if validation succeeds):

Code:
            'put the validated table into the session so that the
            'upload procedure can see it.
            Session("ValidatedDataTable") = CType(ValidatedDataTable, DataTable)

            'call the procedure that populates the datagrid from the session
            ShowData()
 
More info:

The problem seems to be with the business object that creates the datatable. If I populate the session with a test like:

Code:
Dim MyDa As New SqlDataAdapter("SELECT TOP 500 * FROM A_Table;", SqlConnection1)
Dim MyDt As New DataTable

MyDa.Fill(MyDt)
Session("ValidatedDataTable") = MyDt

I can access it all day long with no problems.

So now the question is, what could be wrong with the returned datatable object that would cause it to die in the session?

The CBO (that validates a text file to make sure the database will accept it) essentially builds a datatable a column at a time by converting passed in SQLDataType parameters to system datatypes. It uses a CASE statement to figure out what kind of column to add to the datatable.. Example:
Code:
 Select Case ItemRule.SqlType
    .
    .
    .
            Case SqlDbType.Int
               .DataType = System.Type.GetType("System.Int32")

            Case SqlDbType.Decimal
               .DataType = System.Type.GetType("System.Decimal")
    .
    .
    .
 End Select


  _dtReturnDataTable.Columns.Add(MyColumn)


Later, the CBO attempts to convert each value from the text file to the dataType of the associated column from the DataTable that was built above. If it cannot, an exception is thrown.

I hope I explained that ok.

But...if .NET doesn't have a problem with the datatable, why would the Session?

-FD
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top