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.

Slides:



Advertisements
Similar presentations
Coding ADO.Net DataSet Objects ISYS 512. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set.
Advertisements

Cookies. Data in Cookies Which web site set the cookie Expiration date –DateTime data type –TimeSpan data type One or more pieces of data Keys Define.
ASP.NET Data Binding. Slide 2 Lecture Overview Understanding the ASP.NET data binding model.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Creating Custom Controls ISYS 512/812. Inheritance The process in which a new class can be based on an existing class, and will inherit that class’s interface.
Concurrency Control. R/RR/W W/W User 2 ReadWrite User 1 Read Write R/W: Inconsistent Read problem. W/W: Lost Update problem.
Authenticating Users in an ASP.NET Application. Web Site Administration Tool From VS 2008, click Website/ ASP.Net Configuration to open Web Site Administration.
Introduction to ADO.NET Programming. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB)
Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart.
Concurrency Control. R/RR/W W/W User 2 ReadWrite User 1 Read Write R/W: Inconsistent Read problem. W/W: Lost Update problem.
ASP.NET and ADO.NET. ASP.NET Server Controls Intrinsic Controls: These controls correspond to their HTML counterparts. –Ex. Textbox, listbox, button,
VB.NET Database Tools ISYS Net Applications OLE DB Provider OLE DB Data Source OLE DB Provider ODBC Data Source SQL Server Data Source SQL Server.Net.
Coding ADO.NET Objects: Connection, Command, DataReader.
Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user. Web.config file – node Options: –Windows.
ASP.Net AJAX. AJAX Asynchronous JavaScript and XML: – JavaScript, Document Object Model, Cascade Style Sheet, XML, server-side script such as.Net, etc.
Shopping Cart. ASP.NET State Management Session: User state Application: Application state Cache Static variables: –Public Shared interestRate as Single.
Introduction to Data Adapter. A Simplified View of ADO.Net Objects Ado.Net Data Provider Connection Adapter Command Reader Dataset Data Consumer WinForm.
ASP.NET and ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
Introduction to ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
Shopping Cart. ASP.NET State Management Session: User state Application: Application state Cache Static variables: –Public Shared interestRate as Single.
Introduction to ASP.Net
Coding ADO.Net DataSet Objects. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set of disconnedted.
Web Site Security ISYS 512/812. Authentication Authentication is the process that determines the identity of a user. Web.config file – node Options: –Windows:
Coding ADO.NET Objects: Connection, Command, DataReader.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Working with Session and Application Objects. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and.
Concurrency Control. R/RR/W W/W User 2 ReadWrite User 1 Read Write R/W: Inconsistent Read problem. W/W: Lost Update problem.
VB.NET Database Access ISYS 812. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Coding ADO.Net DataSet Objects. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set of disconnedted.
VB.NET Database Tools ISYS 573. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Introduction to Web Application Development with.Net and Web Service ISYS 350.
Introduction to ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
Coding ADO.NET Objects: Connection, Command, DataReader.
VB.NET Classes ISYS 812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Working with Session. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and lose their values.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Cookies. Data in Cookies Which web site set the cookie Expiration date –DateTime data type –TimeSpan data type One or more pieces of data Keys: A collection.
VB.NET Classes ISYS 546. Classes A class is program structure that defines a user-defined data type that are used to create objects. An instance of a.
Website Security ISYS 512. Cookies Data in Cookies System.Web Which web site set the cookie Expiration date –DateTime data type –TimeSpan data type One.
Shopping Cart. Search and display product information Add item to cart View cart contents Check out.
Introduction to Web Service ISYS 512. Web Service XML Web Service Web services are classes that are stored on the web which can instantiate and use in.
Introduction to ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
ASP and HTML. Anchor Tag testAnchorTag Demo: ASPNet/ASPNETProdListSelf.ASPX.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS546.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
ASP.Net Web Applications. Characteristics of a typical data driven web application Web Server HTML Graphics Active-X Java Applets HTTP Request ADO / JDBC.
© Minder Chen, ASP.NET 2.0: Introduction - 1 ASP.NET 2.0 Minder Chen, Ph.D. Framework Base Class Library ADO.NET: Data & XML.
ASP.Net and HTML. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data.
Command Object’s ExecuteNonQuery Method ISYS 512.
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
ASP.NET Binding and an Introduction to Database Queries Please use speaker notes for additional information!
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
Coding ADO.NET Objects: Connection, Command, DataReader.
ASP.NET Data Binding. Slide 2 Lecture Overview Understanding the ASP.NET data binding model.
ADO.NET Objects Data Adapters Dr. Ron Eaglin. Agenda Builds on Information in Part I Should have working knowledge of creating a database connection Continuation.
C# Classes ISYS 512. Introduction to Classes A class is the blueprint for an object. –It describes a particular type of object. –It specifies the properties.
C# Classes ISYS 512. Introduction to Classes A class is the blueprint for an object. –It describes a particular type of object. –It specifies the properties.
Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out.
Coding ADO.NET Objects: Connection, Command, DataReader.
Coding ADO.NET Objects: Connection, Command, DataReader
ASP.NET and ADO.NET.
Coding ADO.NET Objects: Connection, Command, DataReader
Database Handling Class and Service
ASP.NET and ADO.NET.
VB Classes ISYS 512.
Working with Session and Application Objects
Introduction to ADO.NET
Command Object’s ExecuteNonQuery Method
Presentation transcript:

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 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

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

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

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

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

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

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

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

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.

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.

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

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

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

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

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

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

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

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

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

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