Presentation is loading. Please wait.

Presentation is loading. Please wait.

ASP.NET and ADO.NET. Bind the DataReader to a DataGrid Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn.

Similar presentations


Presentation on theme: "ASP.NET and ADO.NET. Bind the DataReader to a DataGrid Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn."— Presentation transcript:

1 ASP.NET and ADO.NET

2 Bind the DataReader to a DataGrid Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select * from customer;" Dim objComm As New OleDbCommand(strSQL, objConn) Dim Results As String objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() DataGrid1.DataSource = objDataReader DataGrid1.DataBind() Demo: VS to FrontPage – PageLoad Page.IsPostback

3 Select Rating from a RadiobuttonList Private Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select * from customer where rating = '" & RadioButtonList1.SelectedItem.Value & "'" Dim objComm As New OleDbCommand(strSQL, objConn) Dim Results As String objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() DataGrid1.DataSource = objDataReader DataGrid1.DataBind() End Sub

4 ListControl Base Class DropDownList, ListBox, CheckBoxList, RadioButtonList Properties: –AutoPostBack –DataSource –DataTextField: The field in the datasource that provides the text to be used for the list items. –DataValueField –SelectedItem, SelectedValue, SelectedIndex Note: Value of the selected item: SelectedItem.Value Event: OnSelectedIndexChanged

5 Some Possible Causes of Error The procedure in the Page_Load event is executed everytime the web page is loaded. –1. The AutoPostBack property must set to True for the OnSelectedIndexChanged event to work. –2. If the ListItems are created by the Items.Add method, items may be added again. –3. The ADO.Net objects and binding do not need to be recreated every time. If not page.isPostBack then … Demo: ASPNET/ListBoxPostback.aspx

6 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then ListBox1.Items.Add("Apple") ListBox1.Items.Add("Orange") ListBox1.Items.Add("Banana") End If End Sub

7 Binding a DataReader to a ListBox DataSource: –ListBox1.DataSource = objDataReader DataTextField: –ListBox1.DataTextField = "CID" DataValueField: –ListBox1.DataValueField = "CNAME"

8 Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged Response.Write(ListBox1.SelectedItem) Response.Write(ListBox1.SelectedValue) End Sub Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select * from customer;" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() ListBox1.DataSource = objDataReader ListBox1.DataTextField = "CID“ ListBox1.DataValueField = "CNAME" ListBox1.DataBind() End If End Sub

9 Display Selected Customer in dataGrid Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select * from customer where cid = '" & ListBox1.SelectedItem.Value & "'" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() DataGrid1.DataSource = objDataReader DataGrid1.DataBind() End Sub

10 The Effects of PostBack on Bound Controls If the databinding is done in a PageLoad event procedure without checking postback, the databinding is repeated and the control is initialized to its original state. For list controls, such as ListBox, CheckboxList, and Radiobuttonlist, the selected item is no longer selected. Note: Demo previous example without checking postback.

11 Demo:ReaderUpdate.Aspx Use a DataReader to create a dropwdownList with customer CIDs.. Display selected customer data in textboxes. Update customer’s rating using Command object’s ExecuteNonQuery method.

12 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" Dim objConn As New OledbConnection(strConn) Dim strSQL As String = "select cid from customer;" Dim objComm As New OledbCommand(strSQL, objConn) objConn.open() Dim objDataReader As oledbDataReader objDataReader = objComm.executeReader() ListBox1.DataSource = objDataReader ListBox1.DataTextField = "CID" ListBox1.DataBind() objConn.close() End If End Sub

13 Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select * from customer where cid= '" & ListBox1.SelectedItem.Text & "'" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() objDataReader.Read() TextBox1.Text = objDataReader("cname") TextBox2.Text = objDataReader("city") TextBox3.Text = objDataReader("rating") objDataReader.Close() objConn.Close() End Sub

14 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQLUpd As String = "Update customer set rating = '" & TextBox3.Text & "'" strSQLUpd = strSQLUpd & " where cid='" & ListBox1.SelectedItem.Text & "'" Dim objCommUpd As New OleDbCommand(strSQLUpd, objConn) objConn.Open() objCommUpd.ExecuteNonQuery() objConn.Close() End Sub

15 DataSet Techniques Demos Dataform wizard –Parent/child Adapter wizard –DataBind –Demo: Using view to display customers with selected rating. Using code

16 Persistence of Data between Page Postback Each page is entirely new each time we submit information to the same page. There is no persistence of information between pages (other than the ViewState which remembers the data contained in controls). Variables and ADO objects are lost. We can store variables and ADO objects in Session or Application. Demo: StateTestCounter.ASPX, PersistDataSet.Aspx

17 Increase Counter by One. What is wrong? dim counter as integer sub Page_Load() counter=0 end sub sub IncreaseCounter(sender as object, e as EventArgs) counter=counter+1 response.write("The counter's value is:" + cstr(counter)) end sub Demo: StateTestCounter.ASPX

18 Save Counter in Session Demo: StateTestCounter2.ASPX dim counter as integer sub Page_Load() if not page.isPostBack then counter=0 session("MyCounter")=counter else counter=session("MyCounter") end if end sub sub IncreaseCounter(sender as object, e as EventArgs) counter=counter+1 response.write("The counter's value is:" + cstr(counter)) session("MyCounter")=counter End sub

19 Save the Dataset in Session Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then OleDbDataAdapter1.Fill(DataSet11) OleDbDataAdapter2.Fill(DataSet11) Session("myDataSet") = DataSet11 Else DataSet11 = Session("MyDataset") End If End Sub Private Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged Dim myView As DataView Dim Rating As String If RadioButtonList1.SelectedValue = "A" Then Rating = "A" ElseIf RadioButtonList1.SelectedValue = "B" Then Rating = "B" Else Rating = "C" End If myView = DataSet11.Tables("customer").DefaultView myView.RowFilter = "rating='" & Rating & "'" DataGrid1.DataSource = myView DataGrid1.DataBind() End Sub

20 Demo: Binding to an ArrayList Dim fruits As New ArrayList() Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'If Not Page.IsPostBack Then fruits.Add("apple") fruits.Add("orange") fruits.Add("banana") CheckBoxList1.DataSource = fruits CheckBoxList1.DataBind() 'End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer For i = 0 To CheckBoxList1.Items.Count - 1 If CheckBoxList1.Items(i).Selected Then Response.Write("you check" & CheckBoxList1.Items(i).Text) End If Next End Sub

21 Binding to an ArrayList (Collection) of Objects Dim emps As New ArrayList() Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then Dim myEMp1 As New Emp() myEMp1.eid = "e1" myEMp1.ename = "peter" myEMp1.salary = 5000 Dim myEmp2 As New Emp() myEmp2.eid = "e2" myEmp2.ename = "paul" myEmp2.salary = 6000 emps.Add(myEMp1) emps.Add(myEmp2) RadioButtonList1.DataSource = emps RadioButtonList1.DataTextField = "eid" RadioButtonList1.DataValueField = "salary" RadioButtonList1.DataBind() Session("MyEmps") = emps End If End Sub Private Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged emps = Session("MyEmps") Response.Write(emps.Item(RadioButtonList1.SelectedIndex).ename) Response.Write(emps.Item(RadioButtonList1.SelectedIndex).salary.ToString) End Sub


Download ppt "ASP.NET and ADO.NET. Bind the DataReader to a DataGrid Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn."

Similar presentations


Ads by Google