473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can you store datasets in the viewstate? Should we?

When I write scripts for deleting records in databases, I tend to do it in
this fashion:

- delete link passes ID to a delete.aspx page
- delete.aspx page queries db using that ID to retrieve the file
information
- page displays file information with a 'are you sure you want to delete'
confirmation button.
- upon clicking said button, I delete the record.

Simple enough.

At times, though, I need to delete multiple records related to one record as
well as actual files in the filesystem related to the record.

In that case, I tend to use this method:

- delete link passes ID to a delete.aspx page
- delete.aspx page queries db using that ID to retrieve the file
information
- page displays file information with a 'are you sure you want to delete'
confirmation button.
- upon clicking said button, I requery the db to get the data again
- I loop through the dataset returned to delete the related content first,
then delete the parent record

In that method, I query the DB twice for essentially the same data.

Would it make more sense to query once, then store the data in the viewstate
(if I can). Or is it not a big deal to just do a double query.

As these are small datasets, it's probably a moot issue at this point, but
for future reference, I'd like to understand the best practices for this
type of situation.

-Darrel
Dec 29 '06 #1
13 2363
"darrel" <no*****@nowhere.comwrote in message
news:ut**************@TK2MSFTNGP02.phx.gbl...
When I write scripts for deleting records in databases, I tend to do it in
this fashion:
<snip>
Simple enough.
Yes, but you're making me wonder if you're using ASP Classic or ASP.NET. For
one thing, when you talk about writing "scripts", do you mean VBScript or
JavaScript? You surely don't refer to VB.NET or C# as "scripts"...???

OK, so you talk about ViewState, so you must be using ASP.NET, but why on
earth do you need a separate page to carry out your database writes? This is
very much the awful ASP Classic kludge which the ASP.NET Postback
functionality was designed to replace...
- delete.aspx page queries db using that ID to retrieve the file
information
Again, in ASP.NET, you would ask that question *before* the postback

<asp:Button ID="cmdDelete" Text="Delete" OnClick="cmdDelete_Click()"
OnClientClick="return confirm('Are you sure you want to delete this
record?');" />
I'd like to understand the best practices for this type of situation.
Use Postback...
Dec 29 '06 #2
Yes, but you're making me wonder if you're using ASP Classic or ASP.NET.
For one thing, when you talk about writing "scripts", do you mean VBScript
or JavaScript? You surely don't refer to VB.NET or C# as "scripts"...???
I do when I don't fully think about it before writing. ;o)

No, this is asp.net, I should have said functions. Not scripts.
Again, in ASP.NET, you would ask that question *before* the postback
Correct:

page loads
button click 'confirms'
postback deletes record
>I'd like to understand the best practices for this type of situation.

Use Postback...
I am. But that wasn't really the question. The question is can I and/or
should I store a dataset in the viewstate to carry it over from page load to
the postback? Or should I just requery the DB upon postback?

-Darrel
Dec 29 '06 #3
"darrel" <no*****@nowhere.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
I am. But that wasn't really the question. The question is can I and/or
should I store a dataset in the viewstate to carry it over from page load
to the postback? Or should I just requery the DB upon postback?
Get rid of the additional delete page, and you won't have to worry about
this...
Dec 29 '06 #4
Get rid of the additional delete page, and you won't have to worry about
this...
I prefer a separate delete page. I tend to use a 'master' delete page to
handle all record deletions in the app.

Is this bad? If so, why?

Regardless, even if it's the same page, I still have the issue I am asking
about. When I have to delete multiple records that requires a database query
to determine said relations, can I store that in viewstate to pass it back
to the page on postback? Should I?

-Darrel
Dec 29 '06 #5
"darrel" <no*****@nowhere.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>Get rid of the additional delete page, and you won't have to worry about
this...

I prefer a separate delete page. I tend to use a 'master' delete page to
handle all record deletions in the app.

Is this bad? If so, why?
I wouldn't call it "bad" per se - just completely unnecessary, IMO...
Regardless, even if it's the same page, I still have the issue I am asking
about. When I have to delete multiple records that requires a database
query to determine said relations, can I store that in viewstate to pass
it back to the page on postback? Should I?
Obviously you *can* do this - you can do anything you like... :-)

As to whether you *should* or not, it's difficult for me to say because,
IMO, it's totally unnecessary anyway...
Dec 29 '06 #6
As to whether you *should* or not, it's difficult for me to say because,
IMO, it's totally unnecessary anyway...
What part of this do you find unecessary?

I need to to data from the database to construct the confirmation to delete
message. Ie, 'do you want do delete file [name of file from database]'

Then I need to get data from the database to find all the related records
that need deleting.

What other way are you proposing?

No matter how I set this up, I ultimately need/want three 'nodes' on the
flow chart:

1) see record with delete option. If clicked on...
2) see record asking to confirm the delete. If clicked on...
3) delete the record and present confirmation message.

-Darrel
Dec 29 '06 #7
"darrel" <no*****@nowhere.comwrote in message
news:u$*************@TK2MSFTNGP06.phx.gbl...
What part of this do you find unecessary?
The "middle" stage.
I need to to data from the database to construct the confirmation to
delete message. Ie, 'do you want do delete file [name of file from
database]'
Obviously. You'd do this as part of your initial databinding process...
Then I need to get data from the database to find all the related records
that need deleting.
??? Of course you don't!
What other way are you proposing?
CREATE PROC DeleteRecords
@intPrimaryKey int

DELETE FROM <RelatedTable1>
WHERE <ForeignKey= @pintPrimaryKey

DELETE FROM <RelatedTable2>
WHERE <ForeignKey= @pintPrimaryKey

DELETE FROM <MainTable>
WHERE <PrimaryKey= @intPrimaryKey

Wrap the individual deletes in a transaction for additional robustness.
No matter how I set this up, I ultimately need/want three 'nodes' on the
flow chart:
Ah, but that's different! If you *want* to go through the superfluous middle
stage, that's perfectly fine - it's your app! All I'm saying is that it's
completely unnecessary...
1) see record with delete option. If clicked on...
2) see record asking to confirm the delete. If clicked on...
Surely they're the same thing, no...?
Dec 29 '06 #8
>What part of this do you find unecessary?
>
The "middle" stage.
Are you suggesting a confirmation isn't necessary? I guess I'd have to
disagree with that.

Or are you suggesting that the specific way I'm doing the confirmation is
unecessary. I'm not quite clear on what the alternative you are prposing is.
>I need to to data from the database to construct the confirmation to
delete message. Ie, 'do you want do delete file [name of file from
database]'

Obviously. You'd do this as part of your initial databinding process...
Right.
>Then I need to get data from the database to find all the related records
that need deleting.

??? Of course you don't!
Oh!? That's my question. ;0)

Your example of the stored procedure works, but that doesn't help me delete
the physical files on the server. I still need to grab that data from the
database, so I can then delete the files from the filesystem.
Ah, but that's different! If you *want* to go through the superfluous
middle stage, that's perfectly fine - it's your app! All I'm saying is
that it's completely unnecessary...
>1) see record with delete option. If clicked on...
2) see record asking to confirm the delete. If clicked on...

Surely they're the same thing, no...?
Yea, I think we're saying the exact same thing there...you're just not
actually writing out the 3rd node (delete and show confirmed deletion)

I think your suggestion makes perfect sense if I didn't have to find out the
filenames I need to delete from the filesystem. That seems to be the catch.

-Darrel
Dec 29 '06 #9
"darrel" <no*****@nowhere.comwrote in message
news:OG**************@TK2MSFTNGP06.phx.gbl...
Are you suggesting a confirmation isn't necessary? I guess I'd have to
disagree with that.
No - I'm merely suggesting that only *one* confirmation is necessary - with
OnClientClick
Or are you suggesting that the specific way I'm doing the confirmation is
unecessary. I'm not quite clear on what the alternative you are prposing
is.
I'm saying that you only need to ask the user *once* if they want to delete
the record, and all its associated records...
Your example of the stored procedure works, but that doesn't help me
delete the physical files on the server. I still need to grab that data
from the database, so I can then delete the files from the filesystem.
??? Why do you need to "grab" them??? Simply tell SQL Server to delete them
as part of your stored procedure...
I think your suggestion makes perfect sense if I didn't have to find out
the filenames I need to delete from the filesystem. That seems to be the
catch.
??? But you already know the filenames that need to be deleted!!! They're
stored in your database.

Or are you saying that your SQL Server box can't actually see the files to
be deleted - it merely stores their filespecs...?
Dec 29 '06 #10
I'm saying that you only need to ask the user *once* if they want to
delete the record, and all its associated records...
Right. I am only asking once.
??? But you already know the filenames that need to be deleted!!! They're
stored in your database.
No, they're stored in the filesystem.
Or are you saying that your SQL Server box can't actually see the files to
be deleted - it merely stores their filespecs...?
Right.

-Darrel
Dec 29 '06 #11
"darrel" <no*****@nowhere.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>I'm saying that you only need to ask the user *once* if they want to
delete the record, and all its associated records...

Right. I am only asking once.
Er, no - once when the user first clicks the Delete button, and then once
more when they've been redirected to your delete page...
>??? But you already know the filenames that need to be deleted!!! They're
stored in your database.

No, they're stored in the filesystem.
??? OK - I'm obviously missing the point here... Are the filespecs of the
files to be deleted stored in your database or not? If not, then what's the
point of your second database query...???
Dec 29 '06 #12
Er, no - once when the user first clicks the Delete button, and then once
more when they've been redirected to your delete page...
The first click doesn't prompt any sort of confirmation dialog on that page.
That's what the deletePage.aspx is for.

I don't want the delete button to immediately delete the file without an
'are you sure' dialog of some sort.
??? OK - I'm obviously missing the point here...
Yea. Sorry. We're going on here, aren't we. ;o)
Are the filespecs of the files to be deleted stored in your database or
not? If not, then what's the point of your second database query...???
The file name is stored in the database. That's the reason for the second
query. To get the filename so I cand delete the file from the filesystem.
The files themselves are NOT in the database.

Obviously, this is one argument for actually putting them in the database.
;o)

-Darrel
Dec 29 '06 #13
"darrel" <no*****@nowhere.comwrote in message
news:On**************@TK2MSFTNGP06.phx.gbl...
>Er, no - once when the user first clicks the Delete button, and then once
more when they've been redirected to your delete page...

The first click doesn't prompt any sort of confirmation dialog on that
page. That's what the deletePage.aspx is for.

I don't want the delete button to immediately delete the file without an
'are you sure' dialog of some sort.
>??? OK - I'm obviously missing the point here...

Yea. Sorry. We're going on here, aren't we. ;o)
>Are the filespecs of the files to be deleted stored in your database or
not? If not, then what's the point of your second database query...???

The file name is stored in the database. That's the reason for the second
query. To get the filename so I cand delete the file from the filesystem.
The files themselves are NOT in the database.

Obviously, this is one argument for actually putting them in the database.
Fair enough - just a difference of opinion, I guess...

If I were doing this, my flow would be the following:

1) User clicks button

2) Button's OnClientClick is something like "return confirm('Are you sure
you want to delete this record and all its associated files?');"

3) If user responds "Yes", page posts back and calls the button's Click()
method server-side

4) Button's click method calls a SQL Server stored procedure which:

a) fetches a recordset of the associated files
b) walks through the recordset deleting each file
c) deletes the records from the recordset
d) deletes the primary record
Dec 29 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: wASP | last post by:
I thought it was something relatively simple: ViewState = SomeObj; Then: SomeObj = ViewState; So, in my own code, I have this on the initial load:
3
by: Martin | last post by:
Dear fellow ASP.NET programmer, I stared using forms authentication and temporarily used a <credentials> tag in web.config. After I got it working I realized this wasn't really practical. I...
5
by: Joakim Westman \(Elicit AB\) | last post by:
Hi! I have a page that generates a lot of HTML, and I am considering different solutions to constrain the amount of code that is sent back to the client. One thing I thought about is the...
2
by: george d lake | last post by:
Hi, I have a structure that I need to save between pages. Can not use sessions. I would love to use ViewState, but, I get this error! The type 'TicketSystemV2.ucTicketList+OrderBy' must be marked...
2
by: Sky | last post by:
Basically, I'm stumped on how to translate something I wrote in PHP to ASP.NET, and I'm having a hard time figuring out what is right way to do it now... The scenario in PHP was as follows: I...
1
by: Pipo | last post by:
Hi, I'm trying to store a delegate into a viewstate but I get the message that it needs to be serialized then. So I put the attribute <Serializable> in the delegate..but this isnt enough... ...
4
by: Robert P. | last post by:
I can easily store a one-dimensional array in viewstate ( see Test1 ) If I try storing a multi-dimensional array in the viewstate it's crapping out on me when it goes to serialize the array (not...
5
by: Tarun Mistry | last post by:
Hi all, im attempting to use the ViewState instead of the hidden values normally used in procedural web development, however i cant seem to get anything to add into it or load correctly from it. ...
7
by: Shadow Lynx | last post by:
I realize that his question has been asked, in many other forms, many times in this group. Even so, my tired eyes have not yet found a sufficient answer, so I've decided to "reask" it even though...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.