Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

What are the column names returned by the query

Select * From Suppliers
Avatar of Murray Brown

ASKER

SupplierID, SupplierName, RefID etc
The code works fine in my windows forms app so there is something going on with the variable
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
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
One option is to save the DataTable in ViewState. But that would take toll on performance of the page.
>One option is to save the DataTable in ViewState.

Session would be a more suitable choice in my opinion.
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
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent. Thanks very much