Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    May 2005
    Location
    The Lone Star State
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    DataTable gone when page re-posts??

    Hello All!
    I'm having trouble figuring out a work around for a little problem: First I dimmed the DataAdatpter and DataSet:

    Code:
    Dim DA as ODBCDataAdapter = New ODBCDataAdapter
    Dim ds As DataSet = New DataSet
    I have the DataAdapter filling the DataTable in my Page_Load sub:

    Code:
    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:

    Code:
    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.

  • #2
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,073
    Thanks
    2
    Thanked 22 Times in 22 Posts
    I'm still very much in a learning phase of .NET, but I think this line of code:

    Code:
    If Not(Page.IsPostBack) Then
    should be changed to this instead:

    Code:
    If Not IsPostBack Then
    Try that and see what it does for you.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!

  • #3
    New Coder
    Join Date
    May 2005
    Location
    The Lone Star State
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,073
    Thanks
    2
    Thanked 22 Times in 22 Posts
    You need to post more of your code in order for us to be able to help.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!

  • #5
    New Coder
    Join Date
    May 2005
    Location
    The Lone Star State
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!
    Attached Files Attached Files

  • #6
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    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:
    Code:
    ' do all of your jazz above here..
    Session("ds") = ds
    
    ' now access with 
    Session("ds").Tables("INVOICES")....
    See how that works out...
    Good luck;


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •