21
Working with Session

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

  • View
    226

  • Download
    0

Embed Size (px)

Citation preview

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

Working with Session

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

Postback and Variables

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

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

Persistence of Data between Page Postback

• We can store variables and ADO objects in Session or Application.

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

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

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

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

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

Example: Save DataReader in Sesson

• Create a data reader

• Use button to display record one at a time

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

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

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

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

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

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

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

Selecting a Subset of Records with DataView’s RowFilter Property

• objDataView.RowFilter = criteria

• Note: The criteria can be a simple or complex condition.

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

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

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

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

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

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.

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

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)

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

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.

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

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

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

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

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

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

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

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

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

Share Variables between Pages

• First page: – Session("CustomerName") =

objDataReader("cname")

• Second page:– Response.Write("Welcome, " +

Session("CustomerName"))

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

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