Solved

Best practices using ADO in web forms

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create an MVC5, Razor Widget 2 26
Help with error in Query 2 31
Error message when using PrintSocket.Send 7 18
asp.net mvc 2 23
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

860 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