...

DataTable gone when page re-posts??

reverendleo
12-30-2005, 02:43 PM
Hello All!
I'm having trouble figuring out a work around for a little problem: First I dimmed the DataAdatpter and DataSet:

Dim DA as ODBCDataAdapter = New ODBCDataAdapter
Dim ds As DataSet = New DataSet

I have the DataAdapter filling the DataTable in my Page_Load sub:

Sub Page_Load(sender As Object, e As EventArgs)
If Not(Page.IsPostBack) Then
DA.SelectCommand = cmdPerv
DA.Fill(ds, "INVOICES")
conPerv.Close
cmdPerv.Dispose
End If
End Sub

(I use the If Not(Page.IsPostBack) to prevent the table from reloading when other buttons on the form are clicked - 'Clear Fields', for example)

Now, I use this DataTable as the DataSource for a Repeater:

rptInv.DataSource = ds.Tables("INVOICES").SELECT(strSearch)
rptInv.DataBind()

The above 2 lines of code are in a sub that runs on a button click (not the Page_Load sub).

Problem: while debugging, I find that there is no more table called "INVOICES", and hence keep getting a System.NullReference error.

I've tried referencing the table like ds.Tables(0), and get 'No Table 0' error. Also, if I remove the If..Then around the Page_Load sub, it runs fine, but is terribly slow, since it re-fills the DataTable every time a button is clicked.

Any ideas? Thanks in advance.

vinyl-junkie
12-30-2005, 07:10 PM
I'm still very much in a learning phase of .NET, but I think this line of code:

If Not(Page.IsPostBack) Then

should be changed to this instead:

If Not IsPostBack Then

Try that and see what it does for you.

reverendleo
01-03-2006, 06:59 PM
Thanks for the reply!

Changing that syntax seems to have no effect on the problem. I guess both ways of writing that line are valid.

Since my last post, I've also tried Dimming a DataTable, and referring to the dimmed table as the DataSource = INVOICES.Select(strSearch)

This produces the error 'Unable to find column [name of the column used in strSearch]'

Thanks again, hopefully we'll get her hammered out soon.

vinyl-junkie
01-04-2006, 03:38 AM
You need to post more of your code in order for us to be able to help.

reverendleo
01-04-2006, 04:25 PM
Thanks again for the help!

Originally I didn't try to create a DataTable by hand, I just referred to it by the name I gave it when I filled it with the DataAdapter. This latter method was only an attempt to fix the problem from before (DataTable is lost when the page posts back). I have attached a copy of the entire code to this post. The lines in bold and italics (only a few) were not in my original code, and the line that actually causes the error is underlined and in red.

This did work in its entirety before I added the If Not(Page.IsPostBack) Then... around the lines in the Page_Load subroutine. I just can't figure out why the DataTable seemingly ceases to exist.

Much Obliged!

Brandoe85
01-04-2006, 05:55 PM
The page will be recreated on every post back, therefore, your dataset will be gone. You could grab your data each time you post back, as you were doing. Otherwise, you could store the dataset in a session variable maybe even keep it with the viewstate.
When you load in your data, create a session variable to hold the dataset:

' do all of your jazz above here..
Session("ds") = ds

' now access with
Session("ds").Tables("INVOICES")....


See how that works out...
Good luck;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum