Solved

Best practices using ADO in web forms

Posted on 2011-02-17
3
416 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to error 4 43
Exit the loop 4 34
Code works but it's slow 24 30
Gridview alignment 1 8
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now