?
Solved

ASP.net manipulating data table bound to GridView

Posted on 2012-08-21
9
Medium Priority
?
720 Views
Last Modified: 2012-08-21
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
0
Comment
Question by:Murray Brown
  • 4
  • 3
  • 2
9 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 38317426
What are the column names returned by the query

Select * From Suppliers
0
 

Author Comment

by:Murray Brown
ID: 38317441
SupplierID, SupplierName, RefID etc
The code works fine in my windows forms app so there is something going on with the variable
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38317499
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Murray Brown
ID: 38317529
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
 
LVL 20

Expert Comment

by:informaniac
ID: 38317632
One option is to save the DataTable in ViewState. But that would take toll on performance of the page.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38317721
>One option is to save the DataTable in ViewState.

Session would be a more suitable choice in my opinion.
0
 

Author Comment

by:Murray Brown
ID: 38317768
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
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 38317794
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
 

Author Closing Comment

by:Murray Brown
ID: 38317815
Excellent. Thanks very much
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question