Solved

Best practices using ADO in web forms

Posted on 2011-02-17
3
417 Views
Last Modified: 2012-05-11
Experts,

newbie question.

I am pulling data from a database and populating some text boxes and combo boxes. The users can edit/delete any of these fields. They can enter new data in a control that may have not contained data to begin with.  The data is populated on the Load event. The user can do what they want and then click a "save" button.

What is a good practice to accomplish this?  If the data adapter/data set are created in the Load, then they aren't available in the button_click, correct? Do you create "global" adapters/data sets?
Or
Would you populate the controls using a data reader and then use a command to update the database from the button click?

Any guidance is appreciated.

Thanks in advance.
0
Comment
Question by:jvoconnell
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 400 total points
ID: 34924156
Personally I always use the latter of the two approaches. Mainly because, as you noted, the only way to make use of a DataSet/DataAdapter is to store the huge thing in memory between calls, which is just a waste of resources.

So, the quickest and most convenient option is to populate from a datareader and the write back using a simple command (either a stored procedure or at least parameterised SQL).
0
 
LVL 2

Assisted Solution

by:jondow
jondow earned 100 total points
ID: 34924810
I like to take an OOP approach wherever possible, although if you are only displaying a small amount of data this can be overkill.
Create a class(s) that represents the objects on you page.

Public Class MyObject
    Private _Username As String
    Public Property Username() As String
        Get
            Return _Username
        End Get
        Set(ByVal value As String)
            _Username = value
        End Set
    End Property
End Class


Instance and load the object(s) with your database call (sqldatareader/adapter) prior to displaying on the page:
TBName.Text = MyObject.Username

When posting you page, re-instance your object (either with the existing values if any validation or comparison is needed, or as an empty object) then load your object with the user data:

MyObject.Username= TBName.Text

Then have a update method on your class which you call you update the database

MyObject.Update

This approach lets you apply any business logic (ie validation) from within the property or within the update method.

Hope that helps

Rick
0
 
LVL 1

Author Closing Comment

by:jvoconnell
ID: 34925338
Thank you both for the input. The response by carl_tawn gets me on track for the type of project I'm working on. I wanted to give some credit to jondow as well. I will keep this approach for reference when I do have to work with a large volume of data. Once again, thank you both for your time.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question