|
-
Dec 19th, 2005, 12:23 PM
#1
[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.
-
Dec 19th, 2005, 05:33 PM
#2
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.
-
Dec 20th, 2005, 08:15 AM
#3
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:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then 'Load Defaults (first open)
Me.ViewState.Add("dsActionItem", Me.dsActionItem)
Call LoadLoadEmployees() 'Populate employee comboboxes/listboxes
Call LoadLocations() 'Populate location combobox
Call LoadIncidentClass() 'Populate parent incident combobox
Call LoadUserDefault() 'Adjust default to session values
Else
End If
End Sub
Add Item to datagrid:
VB Code:
Private Sub btnAddActionItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddActionItem.Click
If valActionItems() Then
'Add Item to datagrid
Try
Dim drHold As DataRow
drHold = dsActionItem.Tables(0).NewRow
drHold.Item(0) = Me.txtAITitle.Text
drHold.Item(1) = Me.txtAiDesc.Text
drHold.Item(2) = Me.wcoAIEmployee.Value
drHold.Item(3) = Me.txtAICompleteBy.Text
'drHold.Item(4) is the IDNumber. Database handles value, in dataset for query purposes
drHold.Item(5) = Me.wcoAIEmployee.Text
dsActionItem.Tables(0).Rows.Add(drHold)
Me.dtgActionItems.DataBind()
Catch ex As Exception
Response.Write(ex.Message & ex.StackTrace) 'XXX DEBUG XXX
End Try
Else
Response.Write("FAIL") 'XXX DEBUG XXX
End If
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.
-
Dec 20th, 2005, 09:40 AM
#4
Re: Datasets and Temporary Objects
Where do you bind your datagrid to the dataset?
-
Dec 20th, 2005, 09:45 AM
#5
Re: Datasets and Temporary Objects
Right before the catch in the second example.
I specify the datasource in design view.
-
Dec 20th, 2005, 09:49 AM
#6
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.
-
Dec 20th, 2005, 10:18 AM
#7
Re: Datasets and Temporary Objects
Pure gold mendhak, thanks a ton!
Here's how it ended up being used:
Initialize:
VB Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then 'Load Defaults (first open)
Me.ViewState.Add("dsActionItem", Me.dsActionItem) 'Add to viewstate
Call LoadLoadEmployees() 'Populate employee comboboxes/listboxes
Call LoadLocations() 'Populate location combobox
Call LoadIncidentClass() 'Populate parent incident combobox
Call LoadUserDefault() 'Adjust default to session values
Else
End If
End Sub
Add Item to the Datagrid:
VB Code:
Private Sub btnAddActionItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddActionItem.Click
If valActionItems() Then
'Add Item to datagrid
Try
Dim drHold As DataRow
drHold = CType(ViewState.Item("dsActionItem"), DataSet).Tables(0).NewRow
drHold.Item(0) = Me.txtAITitle.Text
drHold.Item(1) = Me.txtAiDesc.Text
drHold.Item(2) = Me.wcoAIEmployee.Value
drHold.Item(3) = Me.txtAICompleteBy.Text
'drHold.Item(4) is the IDNumber. Database handles value, in dataset for query purposes
drHold.Item(5) = Me.wcoAIEmployee.Text
CType(ViewState.Item("dsActionItem"), DataSet).Tables(0).Rows.Add(drHold)
Me.dtgActionItems.DataSource = CType(ViewState.Item("dsActionItem"), DataSet) 'Specify source of data
Me.dtgActionItems.DataBind()
Catch ex As Exception
Response.Write(ex.Message & ex.StackTrace) 'XXX DEBUG XXX
End Try
Else
Response.Write("FAIL") 'XXX DEBUG XXX
End If
End Sub
-
Dec 20th, 2005, 10:56 AM
#8
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
-
Dec 20th, 2005, 10:58 AM
#9
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.
-
Dec 20th, 2005, 11:05 AM
#10
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
-
Dec 20th, 2005, 11:11 AM
#11
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.
-
Dec 20th, 2005, 11:58 AM
#12
Re: [RESOLVED] Datasets and Temporary Objects
 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.
-
Dec 20th, 2005, 12:00 PM
#13
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
-
Dec 20th, 2005, 09:33 PM
#14
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
-
Dec 22nd, 2005, 08:07 AM
#15
Re: [RESOLVED] Datasets and Temporary Objects
Woka's right, she's a dumba$$ tutor. 
Use SPs instead.
-
Dec 22nd, 2005, 08:09 AM
#16
Re: [RESOLVED] Datasets and Temporary Objects
-
Dec 22nd, 2005, 08:11 AM
#17
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.
-
Dec 22nd, 2005, 08:16 AM
#18
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|