190 likes | 205 Views
Learn how to work with Session and Application objects in ASP.NET, including storing variables and ADO.Net objects, persistence of data between page postbacks, and manipulating data in a DataSet.
E N D
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 object.
Session • The Session object let you to many objects. • Objects stored in Session may not be the same type of object. • Session resizes dynamically. As elements are added, it grows in capacity to accommodate them. • Numerical data stored in Session needs to be casted.
Working with the Session object • To place a value into the Session simply assign it a key and then assign the value: • Session["count"] = count; • Session["myName"] = myName; • To read values from the Session: • count=(int) Session["count"]; • myName=(string) Session["myName"]; • myName = Session["myName"].ToString(); • To remove an item: • Session.Remove("count"); • To remove all items: • Session.RemoveAll();
Increase Counter by One. What is wrong? int counter = 0; protected void Button1_Click(object sender, EventArgs e) { ++counter; Response.Write("You click " + counter.ToString() + " times"); }
Save Counter in Session int counter=0; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) Session["mYcounter"]=counter; else counter = (int) (Session["mYcounter"]); protected void Button1_Click(object sender, EventArgs e) { counter += 1; Session["mYcounter"] = counter; Response.Write(counter); }
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
DataSet objDataSet = new DataSet(); DataView objDataView = new DataView(); protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string strConn = "Data Source=rkoq6ngwva.database.windows.net;Initial Catalog=CustomerOrders;Persist Security Info=True;User ID=dchaoDB;Password=dchao_Azure1"; SqlConnection objConn = new SqlConnection(strConn); string strSQL = "select * from customer;"; SqlDataAdapter objAdapter = new SqlDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer"); Session["myDataset"] = objDataSet; } else objDataSet = (DataSet)Session["myDataset"]; } protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) { objDataView = objDataSet.Tables["Customer"].DefaultView; objDataView.RowFilter = "rating = '" + RadioButtonList1.SelectedItem.ToString() + "'"; GridView1.DataSource = objDataView; GridView1.DataBind(); }
Binding a ListBox and Display Two other Fields DataSet objDataSet = new DataSet(); protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string strConn = "Data Source=rkoq6ngwva.database.windows.net;Initial Catalog=CustomerOrders;Persist Security Info=True;User ID=dchaoDB;Password=dchao_Azure1"; SqlConnection objConn = new SqlConnection(strConn); string strSQL = "select * from customer;"; SqlDataAdapter objAdapter = new SqlDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer"); Session["myDataset"] = objDataSet; ListBox1.DataSource = objDataSet.Tables["Customer"]; ListBox1.DataTextField = "cid"; ListBox1.DataValueField = "Cname"; ListBox1.DataBind(); } else objDataSet = (DataSet)Session["myDataset"]; } protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { TextBox1.Text = ListBox1.SelectedValue; TextBox2.Text = objDataSet.Tables["Customer"].Rows[ListBox1.SelectedIndex]["rating"].ToString(); }
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.
DataSet objDataSet = new DataSet(); protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string strConn = "Data Source=rkoq6ngwva.database.windows.net;Initial Catalog=CustomerOrders;Persist Security Info=True;User ID=dchaoDB;Password=dchao_Azure1"; SqlConnection objConn = new SqlConnection(strConn); string strSQL = "select * from customer;"; SqlDataAdapter objAdapter = new SqlDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer"); string strSQLOrders = "select * from orders"; objAdapter.SelectCommand.CommandText = strSQLOrders; objAdapter.Fill(objDataSet, "Orders"); Session["myDataset"] = objDataSet; 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"].ToString(); TextBox2.Text = objDataSet.Tables["Customer"].Rows[ListBox1.SelectedIndex]["rating"].ToString(); DataView ObjDataView= new DataView(); ObjDataView = objDataSet.Tables["Orders"].DefaultView; ObjDataView.RowFilter = "CID='" + ListBox1.SelectedValue + "'"; GridView1.DataSource = ObjDataView; GridView1.DataBind(); Session["myDataset"] = objDataSet; } else objDataSet = (DataSet)Session["myDataset"];}
Listbox SelectedIndexChanged event protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { TextBox1.Text = objDataSet.Tables["Customer"].Rows[ListBox1.SelectedIndex]["Cname"].ToString(); TextBox2.Text = objDataSet.Tables["Customer"].Rows[ListBox1.SelectedIndex]["rating"].ToString(); DataView ObjDataView = new DataView(); ObjDataView = objDataSet.Tables["Orders"].DefaultView; ObjDataView.RowFilter = "CID='" + ListBox1.SelectedValue + "'"; GridView1.DataSource = ObjDataView; GridView1.DataBind(); }
Navigate DatasetNote: Webform textbox does not have the databinding property
Page Load event DataSet objDataSet = new DataSet(); int rowIndex = 0; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string strConn = "Data Source=rkoq6ngwva.database.windows.net;Initial Catalog=CustomerOrders;Persist Security Info=True;User ID=dchaoDB;Password=dchao_Azure1"; SqlConnection objConn = new SqlConnection(strConn); string strSQL = "select * from customer;"; SqlDataAdapter objAdapter = new SqlDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer"); Session["myDataset"] = objDataSet; Session["myDataset"] = objDataSet; Session["rowIndex"] = rowIndex; TextBox1.Text = objDataSet.Tables["Customer"].Rows[rowIndex]["CID"].ToString(); TextBox2.Text = objDataSet.Tables["Customer"].Rows[rowIndex]["Cname"].ToString(); } else objDataSet = (DataSet)Session["myDataset"]; }
Button event protected void Button1_Click(object sender, EventArgs e) { rowIndex = (int)Session["rowIndex"]; ++rowIndex; if (rowIndex >= objDataSet.Tables["Customer"].Rows.Count) rowIndex = objDataSet.Tables["Customer"].Rows.Count-1; TextBox1.Text = objDataSet.Tables["Customer"].Rows[rowIndex]["CID"].ToString(); TextBox2.Text = objDataSet.Tables["Customer"].Rows[rowIndex]["Cname"].ToString(); Session["rowIndex"] = rowIndex; } protected void Button2_Click(object sender, EventArgs e) { rowIndex = (int)Session["rowIndex"]; --rowIndex; if (rowIndex < 0) rowIndex = 0; TextBox1.Text = objDataSet.Tables["Customer"].Rows[rowIndex]["CID"].ToString(); TextBox2.Text = objDataSet.Tables["Customer"].Rows[rowIndex]["Cname"].ToString(); Session["rowIndex"] = rowIndex; }
Share Variables between Pages • First page: • Session[“CustName”]=“Smith”; • Second page: • Response.Write("Welcome, " + Session["CustName“].ToString();)
Sharing DataSet DataSet objDataSet = new DataSet(); protected void Page_Load(object sender, EventArgs e) { objDataSet = (DataSet)Session["myDataset"]; GridView1.DataSource = objDataSet.Tables["Customer"]; GridView1.DataBind(); }