ASP.NET – How to Bind Data

Data Binding is binding controls to data from databases.With data binding we can bind a control to a particular column in a table from the database or we can bind the whole table to the data grid. Data binding provides simple, convenient, and powerful way to create a read/write link between the controls on a form and the data in their application. Working with Data Binding in ASP.NET is slightly different to working with it in VB .NET. Generally, Datasets don’t maintain a current record that is displayed in bound controls. In VB .NET, the BindingContext object handles that and it lets us set the record bound control display. In ASP.NET, there is no BindingContext object to handle that. In ASP.NET, we use a DataView to let the user select which record should be displayed in bound controls. We bind the controls using data view and use the RowFilter property of the data view to select the record we want the bound control to display. Simply said, in VB .NET we use the Dataset to bind records to the bound control and in ASP.NET we use a DataView.

Sample Application

Let’s understand data binding in ASP.NET with a sample. To start, create a database in Access, name it as Books.mdb. Create a table, Table1 with three columns, BookName, Publisher and ISBN. Enter some values in Table1, save and close it. On a new Web Forms page add three TextBoxes and four Buttons. The user interface should look like the image below.

From the Data tab of the toolbox (as shown in the image below) drag a OledbDataAdapter object and configure it to Table1 of theBooks database. Click here to read how to configure a Data Adapter. Once you are finished configuring the data adapter, generate the dataset by selecting Data->Generate DataSet from the main menu.

Since we will use a DataView instead of a Dataset to navigate through the record, drag a DataView object on to the Web Forms page and set the Table property of the data view to Dataset11.Table1.

Select TextBox1, click on the ellipse button for it’s DataBindings property to open the DataBinding’s dialog box. The data binding’s dialog looks like the image below.

In the Data Binding’s dialog we need to bind TextBox1 to the BookName field, TextBox2 to Publisher field and TextBox3 to the ISBN field using the DataView (DataView1). You should not use DataSet as you would in a Windows Application. Open the Code designer window and paste the following code for each button.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles MyBase.Load
DataSet41.Clear()
OleDbDataAdapter1.Fill(DataSet41)
TextBox1.DataBind()
TextBox2.DataBind()
TextBox3.DataBind()
‘filling the dataadapter and displaying the first record from the table when the
‘page loads
End Sub 

 

Private Sub First_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles First.Click
Dim abc As String
Dim index As Integer = 0
Me.ViewState(“index”) = index
‘creating a variable named index to keep track of the record that is currently being
‘displayed and saving it’s state across server round trips using the ViewState property
abc = DataSet41.Tables(0).Rows(index).Item(“BookName”)
‘selecting a record in the dataview using it’s RowFilter property and specifying
‘the text value of a field
DataView1.RowFilter = “Bookname='” & abc & ” ‘”
TextBox1.DataBind()
TextBox2.DataBind()
TextBox3.DataBind()
‘using the abc value with the RowFilter property and binding the text boxes
to the newly selected record
End Sub

Private Sub Previous_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Previous.Click
Dim abc As String
Dim index As Integer
index = Me.ViewState(“index”)
index -= 1
‘checking the index value and setting it to a value less than the current one so that
‘it displays the previous record each time the previous button is clicked
< 0 Then
If index index = 0
End If
‘if first record is reached it will display the first record each time the previous
‘button is clicked
Me.ViewState(“index”) = index
abc = DataSet41.Tables(0).Rows(index).Item(“BookName”)
DataView1.RowFilter = “Bookname='” & abc & “‘”
TextBox1.DataBind()
TextBox2.DataBind()
TextBox3.DataBind()
End Sub

Private Sub Next_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Next.Click
Dim abc As String
Dim index As Integer
index = Me.ViewState(“index”)
index += 1
‘checking the index value and setting it to a value one more than the current one
‘so that it displays next record each time the next button is clicked
If index > DataSet41.Tables(0).Rows.Count – 1 Then
index = DataSet41.Tables(0).Rows.Count – 1
index = 0
End If
‘if last record is reached it displays the last record each time the next button is
‘clicked. the code counts for the number of rows in the table and sets the last row to the
‘index variable once the last row is reached
Me.ViewState(“index”) = index
abc = DataSet41.Tables(0).Rows(index).Item(“BookName”)
DataView1.RowFilter = “Bookname='” & abc & “‘”
TextBox1.DataBind()
TextBox2.DataBind()
TextBox3.DataBind()
End Sub

Private Sub Last_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Last.Click
Dim abc As String
Dim index As Integer
index = DataSet41.Tables(0).Rows.Count – 1
‘the code counts for the number of rows in the table and sets the last row to the
‘index variable so that last record is displayed each time the last button is clicked
Me.ViewState(“index”) = index
abc = DataSet41.Tables(0).Rows(index).Item(“BookName”)
DataView1.RowFilter = “Bookname='” & abc & “‘”
TextBox1.DataBind()
TextBox2.DataBind()
TextBox3.DataBind()
End Sub

Once you are finished with the code, run the page. The first record from the table is displayed when the page loads. You can click the button to navigate through the records.

 

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s