Data Binding with ADO.NET and ASP.NET

L

Lloyd Sheen

I have several apps that use a roll your own approach, but I am embarking on
a project which will not allow that with the time constraints.

I have gened up a little app to try the databinding approach. Simply it is
a page to maintain the Territories table in Northwind (SQL Server).

I have two SqlDataAdaptors - one for the list of Territories
- on for the individual
Territories row (uses parameter to select)

I created the two datasets. So far so good. I can open the page bind to a
dropdownlist, and get the details into a set of textboxes for each field.

Now I also have a button which simply outputs the text of the update
command. It uses the
UpdateCommand.CommandText from the dataset.

How does databinding help with updates. All parameter values are nothing
when I click the button?

The code follows:

Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
Me.TerritoryList.Fill(Me.DSTerritoryList)
Me.DropDownList1.DataBind()
End If
End Sub

Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
DropDownList1.SelectedIndexChanged
Me.OneTerritory.SelectCommand.Parameters(0).Value =
DropDownList1.SelectedItem.Value
Me.OneTerritory.Fill(Me.DsOneTerritory1)
Page.DataBind()
End Sub

Private Sub btnUpdateCommand_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnUpdateCommand.Click
Me.txtUpdateCommand.Text = Me.OneTerritory.UpdateCommand.CommandText
End Sub
End Class
 
C

Cowboy \(Gregory A. Beamer\) [MVP]

If you follow the ADO .NET model to the nth, you open the DataAdapter to
fill DataTables and bind. When you update, you re-initiate the DataAdapter
and fire the Update command and it is handled for you. Rather convenient.

The world you are describing is partially this model and partially roll your
own, which is where you are having a disconnect.

If you want to take full advantage of the Framework methodology, drag and
drop data goo onto your page in the designer. Set attributes and write a bit
of code to instantiate. Everything automagically binds based on your
declarations. Then, examine the quickstarts and samples and you will see how
MS suggests you use the automagic methods to hook up.

Most likely, after building a page and understanding this method, you will
s##tcan the VS .NET code and create your own DataAdapters. This is primarily
to clean up the large amount of goo in the "hidden" sections of your
CodeBehind file, rather than the MS way is wrong.

Dino Esposito has written the best material on the MS way of using the data
objects. I suggest looking at his articles on MSDN or even buy his ASP
..NET/ADO .NET book. Once you get the system down, it flows nicely. It just
requires a mental paradigm shift.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top