Presentation is loading. Please wait.

Presentation is loading. Please wait.

Working with Session. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and lose their values.

Similar presentations


Presentation on theme: "Working with Session. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and lose their values."— Presentation transcript:

1 Working with Session

2 Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and lose their values.

3 Persistence of Data between Page Postback We can store variables and ADO objects in Session or Application.

4 Increase Counter by One. What is wrong? Dim counter As Integer Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click counter = counter + 1 Response.Write(counter) End Sub

5 Save Counter in Session Dim counter As Integer Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click counter = counter + 1 Session("MyCounter") = counter Response.Write(counter) End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then Session("MyCounter") = counter Else counter = Session("MyCounter") End If End Sub

6 Example: Save DataReader in Sesson Create a data reader Use button to display record one at a time

7 Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\SalesDB2007.accdb“ Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select * from customer;" Dim objComm As New OleDbCommand(strSQL, objConn) Dim objDataReader As OleDbDataReader Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then objConn.Open() objDataReader = objComm.ExecuteReader() objDataReader.Read() TextBox1.Text = objDataReader("cid") TextBox2.Text = objDataReader("cname") Session("myReader") = objDataReader Else objDataReader = Session("myreader") End If End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) If objDataReader.Read() = True Then TextBox1.Text = objDataReader("cid") TextBox2.Text = objDataReader("cname") Else Label1.Text = "no more record" objConn.Close() End If End Sub

8 DataSet Example Create a web form with a Radiobuttonlist to choose rating and display customers with the selected rating in a data grid. RadioButtonList control: –Items property –SelectedValue property

9 Data Binding with DataView And Save the Dataset in Session If Not Page.IsPostBack Then Response.Write(Session("CurrentDay")) Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\SalesDB2007.accdb“ Dim objConn As New OleDbConnection(strConn) Dim objDataSet As New DataSet() Dim strSQL As String = "select * from customer;" Dim objAdapter As New OleDbDataAdapter(strSQL, objConn) objAdapter.Fill(objDataSet, "Customer") Dim ObjDataView As New DataView() ObjDataView = objDataSet.Tables("Customer").DefaultView GridView1.DataSource = ObjDataView GridView1.DataBind() Session("DS") = objDataSet End If

10 Selecting a Subset of Records with DataView’s RowFilter Property objDataView.RowFilter = criteria Note: The criteria can be a simple or complex condition.

11 DataView Example Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged objDataSet = Session("DS") Dim ObjDataView As New DataView() ObjDataView.Table = objDataSet.Tables("Customer") ObjDataView.RowFilter = "rating='" & RadioButtonList1.SelectedValue & "'" GridView1.Visible = True GridView1.DataSource = ObjDataView GridView1.DataBind() End Sub

12 Binding a ListBox and Display Two Fields Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.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 * from customer;" Dim objAdapter As New OleDbDataAdapter(strSQL, objConn) objAdapter.Fill(objdataset, "Customer") ListBox1.DataSource = objdataset.Tables("customer") ListBox1.DataTextField = "CID" ListBox1.DataValueField = "Cname" ListBox1.DataBind() Session("MyDataset") = objdataset Else objdataset = Session("MyDataset") End If End Sub Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged TextBox1.Text = ListBox1.SelectedValue TextBox2.Text = objdataset.Tables("customer").Rows(ListBox1.SelectedIndex)("Rating") End Sub

13 DataTable’s Rows Property This is a collection of all the records in a table, a collection of DataRow objects. DataRow object’s properties and methods.

14 Accessing a Record with Index objAdapter.Fill(objDataSet, "Customer") TextBox1.Text = objDataSet.Tables("Customer").Rows(rowIndex).Item(0) TextBox2.Text = objDataSet.Tables("Customer").Rows(rowIndex).Item(1)

15 A page with CID Listbox to display customer data in textboxes and orders data in GridView. Use one adapter to create a dataset with two tables. Create bound listbox and automatically select the first customer’s CID: – ListBox1.Items(0).Selected = True GridView control is bound using Orders table’s view.

16 Creating Multiple Tables: One Adapter, Many Tables Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQLCustomer As String = "select * from customer;" Dim strSQLOrders As String = "select * from orders" Dim objAdapter As New OleDbDataAdapter(strSQLCustomer, objConn) Dim objDataset As New DataSet objAdapter.Fill(objDataset, "Customer") objAdapter.SelectCommand.CommandText = strSQLOrders objAdapter.Fill(objDataset, "orders")

17 Bind GridView to Orders table’s View Dim ObjDataView As New DataView() ObjDataView = objDataset.Tables("Orders").DefaultView ObjDataView.RowFilter = "CID='" & ListBox1.SelectedValue & "'" GridView1.DataSource = ObjDataView GridView1.DataBind()

18 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 strSQLCustomer As String = "select * from customer;" Dim strSQLOrders As String = "select * from orders" Dim objAdapter As New OleDbDataAdapter(strSQLCustomer, objConn) objAdapter.Fill(objDataset, "Customer") objAdapter.SelectCommand.CommandText = strSQLOrders objAdapter.Fill(objDataset, "orders") ListBox1.DataSource = objDataset.Tables("customer") ListBox1.DataTextField = "CID" ListBox1.DataValueField = "CID" ListBox1.DataBind() ListBox1.Items(0).Selected = True TextBox1.Text = objDataset.Tables("customer").Rows(ListBox1.SelectedIndex)("Cname") TextBox2.Text = objDataset.Tables("customer").Rows(ListBox1.SelectedIndex)("Rating") Dim ObjDataView As New DataView() ObjDataView = objDataset.Tables("Orders").DefaultView ObjDataView.RowFilter = "CID='" & ListBox1.SelectedValue & "'" GridView1.DataSource = ObjDataView GridView1.DataBind() Session("DS") = objDataset End If

19 Listbox’s SelectedIndexChanged Event Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged objDataset = Session("DS") TextBox1.Text = objDataset.Tables("customer").Rows(ListBox1.SelectedIndex)("Cname") TextBox2.Text = objDataset.Tables("customer").Rows(ListBox1.SelectedIndex)("Rating") Dim ObjDataView As New DataView() ObjDataView = objDataset.Tables("Orders").DefaultView ObjDataView.RowFilter = "CID='" & ListBox1.SelectedValue & "'" GridView1.DataSource = ObjDataView GridView1.DataBind() Session("DS") = objDataset End Sub

20 Share Variables between Pages First page: –Session("CustomerName") = objDataReader("cname") Second page: – Response.Write("Welcome, " + Session("CustomerName"))

21 Working with Session Start Event in Global.ASAX In Global.ASAX – Public CurrentDay As String – Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) – CurrentDay = "Today is " + Now().DayOfWeek.ToString + ", " + Now().ToString – Session("CurrentDay") = CurrentDay –End Sub In a web page: –Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load – response.Write(session("CurrentDay")) –End Sub


Download ppt "Working with Session. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and lose their values."

Similar presentations


Ads by Google