Results 1 to 18 of 18

Thread: [RESOLVED] Datasets and Temporary Objects

  1. #1

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Resolved [RESOLVED] Datasets and Temporary Objects

    I think that's a good way to describe my problem?

    I have a datagrid on my webpage that is bound to a typed dataset. Each time I add a row to the dataset, it adds to the datagrid. The problem is, the next time I add a row to the dataset; the original is gone and the new one is in it's place.

    I (now) realize there isn't viewstates with datasets and I need to store the values somewhere between page refreshes. Since this is a dataset that needs to be filled before the page is submitted, I would like to have an intermediary xml file I could populate each time a new row is added and only talk to my database twice (once for page load, once for page submit).

    The problem is, I can't figure this out. I would like to make a hardcoded xml document that can temporarily store the items until the page is submitted, but can't find a decent resource. Most of them are for permanant data store and not on a "per user/per page" basis.

    I won't put this on your shoulders to explain all this to me. It's too much and I'll probably have quite a few problems. All I'm asking for is a good resource that exaplains how to make XML templates and use them for temporary data storing.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Datasets and Temporary Objects

    Your data may not be stored, but you do have the option of adding your dataset to the viewstate, and in each postback, rebinding the datagrid to the dataset in the viewstate object.

  3. #3

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Datasets and Temporary Objects

    Thanks for the reply.

    I gave this a shot, but maybe I'm not quite sure how. Here's the code I used to attempt this:

    Initialize webform:
    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         If Not Page.IsPostBack Then         'Load Defaults (first open)
    3.             Me.ViewState.Add("dsActionItem", Me.dsActionItem)
    4.             Call LoadLoadEmployees()        'Populate employee comboboxes/listboxes
    5.             Call LoadLocations()            'Populate location combobox
    6.             Call LoadIncidentClass()        'Populate parent incident combobox
    7.             Call LoadUserDefault()          'Adjust default to session values
    8.         Else                                
    9.            
    10.         End If
    11.     End Sub
    Add Item to datagrid:
    VB Code:
    1. Private Sub btnAddActionItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddActionItem.Click
    2.         If valActionItems() Then
    3.             'Add Item to datagrid
    4.             Try
    5.                 Dim drHold As DataRow
    6.                 drHold = dsActionItem.Tables(0).NewRow
    7.  
    8.                 drHold.Item(0) = Me.txtAITitle.Text
    9.                 drHold.Item(1) = Me.txtAiDesc.Text
    10.                 drHold.Item(2) = Me.wcoAIEmployee.Value
    11.                 drHold.Item(3) = Me.txtAICompleteBy.Text
    12.                 'drHold.Item(4) is the IDNumber.  Database handles value, in dataset for query purposes
    13.                 drHold.Item(5) = Me.wcoAIEmployee.Text
    14.  
    15.                 dsActionItem.Tables(0).Rows.Add(drHold)
    16.                 Me.dtgActionItems.DataBind()
    17.             Catch ex As Exception
    18.                 Response.Write(ex.Message & ex.StackTrace)  'XXX DEBUG XXX
    19.             End Try
    20.         Else
    21.             Response.Write("FAIL")                          'XXX DEBUG XXX
    22.         End If
    23.     End Sub

    dtgActionItems is the datagrid on the webform
    dsActionItems is the dataset (built in design view)
    --Contains 1 table: tblActionItems

    Thanks for your help^^

    -------edit---------------
    I think it has something to do with the key when I add it to the viewstate. I'll do some research and see if I can figure out how to get the key, but if you know right off; please let me know. Thanks!
    Last edited by sevenhalo; Dec 20th, 2005 at 08:19 AM.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Datasets and Temporary Objects

    Where do you bind your datagrid to the dataset?

  5. #5

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Datasets and Temporary Objects

    Right before the catch in the second example.

    I specify the datasource in design view.

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Datasets and Temporary Objects

    Ok, this might sound a little confusing, but you need to change the datasource property of the datagrid. It has to be something like this:

    DataGrid1.DataSource = CType(ViewState("mydataset"), DataSet)

    Although you may be happy with working in design view, I have no experience of working with datagrids in design view (nor do I plan to, ever), so you will have to experiment a bit with the placement of the above line.

  7. #7

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Datasets and Temporary Objects

    Pure gold mendhak, thanks a ton!

    Here's how it ended up being used:

    Initialize:
    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         If Not Page.IsPostBack Then         'Load Defaults (first open)
    3.  
    4.             Me.ViewState.Add("dsActionItem", Me.dsActionItem)                               'Add to viewstate
    5.  
    6.             Call LoadLoadEmployees()        'Populate employee comboboxes/listboxes
    7.             Call LoadLocations()            'Populate location combobox
    8.             Call LoadIncidentClass()        'Populate parent incident combobox
    9.             Call LoadUserDefault()          'Adjust default to session values
    10.         Else
    11.  
    12.         End If
    13.     End Sub
    Add Item to the Datagrid:
    VB Code:
    1. Private Sub btnAddActionItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddActionItem.Click
    2.         If valActionItems() Then
    3.             'Add Item to datagrid
    4.             Try
    5.                 Dim drHold As DataRow
    6.                 drHold = CType(ViewState.Item("dsActionItem"), DataSet).Tables(0).NewRow
    7.  
    8.                 drHold.Item(0) = Me.txtAITitle.Text
    9.                 drHold.Item(1) = Me.txtAiDesc.Text
    10.                 drHold.Item(2) = Me.wcoAIEmployee.Value
    11.                 drHold.Item(3) = Me.txtAICompleteBy.Text
    12.                 'drHold.Item(4) is the IDNumber.  Database handles value, in dataset for query purposes
    13.                 drHold.Item(5) = Me.wcoAIEmployee.Text
    14.  
    15.                 CType(ViewState.Item("dsActionItem"), DataSet).Tables(0).Rows.Add(drHold)
    16.             Me.dtgActionItems.DataSource = CType(ViewState.Item("dsActionItem"), DataSet)   'Specify source of data
    17.                 Me.dtgActionItems.DataBind()
    18.             Catch ex As Exception
    19.                 Response.Write(ex.Message & ex.StackTrace)  'XXX DEBUG XXX
    20.             End Try
    21.         Else
    22.             Response.Write("FAIL")                          'XXX DEBUG XXX
    23.         End If
    24.     End Sub

  8. #8
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: [RESOLVED] Datasets and Temporary Objects

    If dsActionItem is in design view, why are you adding it to viewstate???
    If it's a dataset dropped onto the page, then it should handle it's own viewstate automatically.

    Woka

  9. #9

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [RESOLVED] Datasets and Temporary Objects

    I have no idea woka. It was retaining only single values. Each time I added a new row to the table, only that row was saved.

    I didn't see any properties in the dataset that alowed me to set EnableViewState, so I ended up dong it this way. If you have an alternate solution, I'm all ears.

    FYI: The dataset is populated from the webform, it starts off blank initially. I realize it would hold data between posts if it was querying, but I'm not updating the db on each "add" event.

  10. #10
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: [RESOLVED] Datasets and Temporary Objects

    Hmm...will test this out tonight, or tomorrow, but I am positive that their viewstate is handled, although I may be wrong.
    Datasets etc that are dropped onto the form are concidered very bad practice, and app design, if you're a pro developer. Even so, MS training courses tell you to do this. Absolutely disgraceful!!!

    Then again, this was exactly the same official MS course that said:

    Never ever add where clauses to your SQL.
    Instead of doing:
    Code:
    SELECT UserID, Username, Password FROM Users WHERE Admin = 1
    MS tells you to do:
    Code:
    SELECT UserID, Username, Password FROM Users
    Then once EVERY, and I mean EVERY, record from that table has been loaded into the dataset then you take a dataview of the dataset and specify the clause from there.

    I choose my words very carefuly here....UTTER RUBBISH!!!
    Yea, that's clever, loading a million records across a network, only for the results to be shortened "client" side instead of SQL Svr side.

    Sorry, good ranting.

    Woka

  11. #11

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [RESOLVED] Datasets and Temporary Objects

    I think they meant, never add where clauses that aren't indexed fields (admin bit typically wouldn't be). Which isn't always possible.

    I agree with you the viewstate is handled when I query the db. I've used datasets before and never had to do it this way. The issue here is, it's not attached to the db. I built it in design time (mainly for formatting reasons) and the webform only adds rows to it. After the user submits the report, then I will take the dataset and write it to the db once.

  12. #12
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: [RESOLVED] Datasets and Temporary Objects

    Quote Originally Posted by sevenhalo
    I think they meant, never add where clauses that aren't indexed fields (admin bit typically wouldn't be).
    No, no. She catagorically said "NEVER add a where clause"

    This is coming from the same tutor who said "Stongly typed datasets are faster than datasets", how can this possibly be, since a stroingly typed dataset inherits a normal dataset!

    Look what you've made me do....rant.

  13. #13

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [RESOLVED] Datasets and Temporary Objects

    Wow, thats nuts.

    You can't put that on me! You've been foaming at the mouth in every thread you've been in today

  14. #14
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: [RESOLVED] Datasets and Temporary Objects

    It's absolutely true...that's why I cancelled the other courses.


    Dunno why. I know's there's a lot of talk about Badgers and TB in the news lately. I think I've had a good rant today

    Woof

  15. #15
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] Datasets and Temporary Objects

    Woka's right, she's a dumba$$ tutor.

    Use SPs instead.

  16. #16

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [RESOLVED] Datasets and Temporary Objects

    sprocs?

  17. #17
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] Datasets and Temporary Objects

    If that's what you call them. I call them SPs because chicks like it when you use acronyms.

  18. #18

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [RESOLVED] Datasets and Temporary Objects

    Ohh, I thought you were taling to me about the dataset; I'm sorry. I haven't had my coffee yet.

Posting Permissions

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



Click Here to Expand Forum to Full Width