ASP.net manipulating data table bound to GridView

Hi. I am running the following code through a button click. It uses the xSQLTable_Suppliers data table declared at the top of the code and bound using the code further down.
For some reason the data table isn't there. It looks like the variable isn't working. The error that I get is that there is no column called "SupplierName"

    Sub Get_Defaults_for_Supplier(ByVal oSupplierName As String)

        xSQLTable_Suppliers.DefaultView.RowFilter = "SupplierName='" & SQLConvert(oSupplierName) & "'"
...


    Dim xSQLAdapter_Suppliers As SqlDataAdapter 'declared at top of webform code
    Dim xSQLTable_Suppliers As New DataTable

    Function Fill_DGV_Suppliers() As Boolean
        Try
            Dim sSQL As String

            sSQL = "Select * From Suppliers"

            Dim connection As New SqlConnection(Session("MyVar"))
            xSQLAdapter_Suppliers = New SqlDataAdapter(sSQL, connection)
            xSQLAdapter_Suppliers.Fill(xSQLTable_Suppliers)
            Me.dgvSuppliers.DataSource = xSQLTable_Suppliers
            Me.dgvSuppliers.DataBind()

            Fill_DGV_Suppliers = True
        Catch ex As Exception
            Me.lblGeneralError.Text = "Problem loading Suppliers table from SQL " & ex.Message
            Fill_DGV_Suppliers = False
        End Try
    End Function
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
 
CodeCruiserCommented:
Session can hold anything you throw at it (as far as I know)

Session("dTable") = MyDataTable
...
MyDataTable = Session("dTable")

One thing to be aware of is the null reference exception. If dTable does not exist in session, it will return null and then if you try accessing anything on this null, you will get an exception. So the safe bet is

If Not IsNothing(Session("dTable")) Then
     ...
End If

OR

MyDataTable = Session("dTable")
If Not IsNothing(MyDataTable) Then
    ...
End If
0
 
Obadiah ChristopherCommented:
What are the column names returned by the query

Select * From Suppliers
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
SupplierID, SupplierName, RefID etc
The code works fine in my windows forms app so there is something going on with the variable
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Obadiah ChristopherCommented:
Can be a sequencing issue.

Since it is a global variable. It will get recreated each time a postback occurs.

So first the datatable will get created

then
if

Get_Defaults_for_Supplier gets executed before Fill_DGV_Suppliers() it should give you that error
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
I thought as much. Is there a way to create a data table from the Grid after a post back or some other easy way around this
0
 
Obadiah ChristopherCommented:
One option is to save the DataTable in ViewState. But that would take toll on performance of the page.
0
 
CodeCruiserCommented:
>One option is to save the DataTable in ViewState.

Session would be a more suitable choice in my opinion.
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi CodeCruiser. I assumed that session only held strings etc. Sounds like the perfect way to do this. How does session declare and represent a data table
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Excellent. Thanks very much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.