Pages

Tuesday, 3 May 2011

ASP.NET: How to display data in individual textboxes

Introduction
This question came up on VBCity recently where someone wanted to use individual textboxes to display bound data.  They specifically didn’t want to use one of the complex bound controls like the GridView.  So I did a bit of research and couldn’t find much by way of helpful answers for VB developers.  In the end, I found some useful C# examples by author Bill Hamilton in his ADO.NET Cookbook and I re-worked one of those.  Here’s the result :

The aspx page
For simplicity, I’ve used two labels, two textboxes and a button.  The labels contain the ‘heading’ of what will be contained in its corresponding textbox.  The button will allow the user to page through the records one at a time.

http://www.w3.org/1999/xhtml">

Northwind Company Contacts




>











Without any data, it looks like this:

image1

The code behind – Imports and Variables
The VB code behind file first needs Imports statements for System.Data and System.Data.SqlClient. Then, I created an integer variable that will be used to keep track of which row of data the user is currently viewing.

Here's the first few lines:

Imports System.Data
Imports System.Data.SqlClient

Partial Class _Default
Inherits System.Web.UI.Page

  Shared itemCount As Integer

The code behind – Page Load
When the page first loads, the application needs to get the data from the database and store it in a DataTable.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim NWconnection As String = "Data Source=localhost\sqlexpress;Initial Catalog=Northwind;Integrated Security=True"
Dim strSelect = "SELECT CompanyName, ContactName FROM Customers"
Dim daNWind As New SqlDataAdapter(strSelect, NWconnection)
Dim dtNWind As New DataTable
daNWind.Fill(dtNWind)
.
.
End If

End Sub

This is fairly standard ADO.NET code:

The connection string is created.  In this case I used the Northwind database. The Select statement identifies the table and the fields to be selected.  Here, a couple of fields from the Customers table. A DataAdapter to organise the transfer of the data into the DataTable named dtNWind.

The next step is to  create a DataView and store a reference to that DataView in a Session object:

Dim dvCustomers As DataView = dtNWind.DefaultView
Session("NWindDataView") = dvCustomers

Although I don’t need to do so in this particular example, I could use the DataView at this point to filter or sort the data.
As you can see from the code snippet above, I’ve assigned the name ‘NWindDataView’ to the Session object.

The final instruction in the Page Load event calls a method that will display the first company name and contact name in the DataTable.  I’ll describe that method next, but just for completeness, here’s the finished Page Load event code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim NWconnection As String = "Data Source=localhost\sqlexpress;Initial Catalog=Northwind;Integrated Security=True"
Dim strSelect = "SELECT CompanyName, ContactName FROM Customers"
Dim daNWind As New SqlDataAdapter(strSelect, NWconnection)
Dim dtNWind As New DataTable
daNWind.Fill(dtNWind)

    Dim dvNWind As DataView = dtNWind.DefaultView
Session("NWindDataView") = dvNWind

    ShowInfo()
End If
End Sub

The code behind – The ShowInfo Method
Now that the application has the data to hand, this method will display the individual parts of it.  Here’s the code:

Sub ShowInfo()

  Dim dvCustomers As DataView = CType(Session("NWindDataView"), DataView)
Dim drvCustomer As DataRowView = dvCustomers(itemCount)
txtCompanyName.Text = drvCustomer("CompanyName").ToString()
txtContactname.Text = drvCustomer("ContactName").ToString()
itemCount = itemCount + 1
If itemCount > dvCustomers.Count - 1 Then itemCount = 0

End Sub

First, it creates a DataView by casting the stored Session object NWindDataView to a DataView type. Then, it creates a DataRowView, that zeroes in on an individual row of data. The specific row will be the one that has the index number that equates to the current value of that shared Integer variable I created at the start. Next, the values of the named columns for that row are assigned to the two textboxes.Importantly, the value of the itemCount variable is then incremented by 1, so that the next row will be shown next time this method is called.Equally importantly, the final line checks that we haven’t reached the end of the table. If we have, then the value is reset to zero.

The code behind – The Button Click
All that happens here is that the ShowInfo method is called to move the data display on to the next row:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
ShowInfo()
End Sub

Summary
When the application first runs, the first row’s data is automatically displayed in the two textboxes.  Then, each click of the button will move the data along until the end of the table is reached.  At that point, the row counter is reset to zero and the process starts over.  At run time, after a few clicks of the button, the display looks like this:

image2

I’m not an ASP.NET developer, so I’ve no idea if this the best or most efficient way of achieving the required result.  I suspect probably not.  One alternative I looked at (even though the original requirement was not to use complex bound controls) was to use a FormView and fiddle with the templates in that control - “Fiddle” being the operative word, because it surely is a pain if you have a lot of fields and multiple templates to tweak.  Bearing in mind that my simple example above doesn’t include any edit, insert or delete capabilities, I think FormView would probably be the only realistic option if those features were needed.  But for a simple, read only display of data, the simple binding approach I’ve used here seems to work fine.

Posted Mar 28 2011, 10:08 PM by Ged Mead Filed under: , , , , , , ,

View the original article here

0 comments:

Post a Comment

 
Powered by Blogger