Solved

Best practices using ADO in web forms

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

15 Experts available now in Live!

Get 1:1 Help Now