Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Cookies

2 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 a new cookie: –Dim CookieCID as new HttpCookie(“cid”) Add to: Response.Cookies –Response.cookies.add(cookieCID)

3 Cookie’s Properties System.Web/HttpCookie –Name –Value –Expires To write a cookie: –Response.Cookies.Add(cookieObj)

4 Creating Cookies dim cookieCID as New HttpCookie("cid") dim cookieCNAME as new HttpCookie("cname") dim dt as dateTime=dateTime.now() dim ts as new TimeSpan(30,0,0,0) cookieCID.value=cid.text cookieCname.value=cname.text cookieCID.expires=dt.add(ts) cookieCname.expires=dt.add(ts) response.cookies.add(cookieCID) response.cookies.add(cookieCNAME) Note: The name(or key)of cookieCID is “cid” Demo: ASPNET/CookieForm.aspx

5 sub setCookie(Sender As Object, E As EventArgs) dim cookieCID as New HttpCookie("cid") dim cookieCNAME as new HttpCookie("cname") dim dt as dateTime=dateTime.now() dim ts as new TimeSpan(30,0,0,0) cookieCID.value=cid.text cookieCname.value=cname.text cookieCID.expires=dt.add(ts) cookieCname.expires=dt.add(ts) response.cookies.add(cookieCID) response.cookies.add(cookieCNAME) end sub Please enter customer ID: Please enter your name: This ID and name will be used to set your cookie.

6 Reading Cookies Dim custid as string Dim custName as string custid=request.cookies("cid").value custname=request.cookies("cname").value

7 Demo: CookieRead.ASPX sub page_load() cid.text=request.cookies("cid").value cname.text=request.cookies("cname").value cookieCount.text=cstr(request.cookies.count) Dim ckey As String For Each ckey In Request.Cookies.Keys Response.Write(ckey) Next end sub Note: SessionID

8 Using Cookie with DataReader 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 dim objDataReader as oledbDataReader dim cid as string cid=request.cookies("CID").value strSQL="select * from webcustomer where CustID= '" & cid & "'" dim objComm as new OledbCommand(strSQL,objConn) objDataReader=objComm.executeReader() objDataReader.Read() session("cname")=objDataReader("CustName") response.write (" Welcome:" & objDataReader("CustName") & " ") Demo:ASPNET/CookieGreeting.aspx

9 Demo: CookieGreetingShowOrder.aspx sub page_Load() If not Page.IsPostBack then dim cid as string cid=request.cookies("CID").value objConn.open() strSQL="select * from webcustomer where CustID= '" & cid & "'" dim objComm as new OledbCommand(strSQL,objConn) objDataReader=objComm.executeReader() if notobjDataReader.Read()then response.write("We don't have your record" & " ") else response.write (" Welcome:" & objDataReader("CustName") & " ") end if session("cid")=cid session("cname")=objDataReader("CustName") objConn.close() end if end sub

10 sub showOrders(sender as object, e as EventArgs) dim cid, cname as string cid=session("cid") cname=session("cname") msg1.text=cname & " orders: " objConn.open() strSQL="select * from webOrder where CustID= '" & cid & "'" dim objComm as new OledbCommand(strSQL,objConn) objDataReader=objComm.executeReader() dgOrder.Datasource=objDataReader dgOrder.DataBind() objConn.close() end sub

11 Session System.Web.HttpSessionState Properties: –SessionID –IsCookieLess –TimeOut –Count Methods –Abandon

12 Working with the Session To place a value into the collection simply assign it a key and then assign the value: –Session (“Age”)=25 To read values from the collection: –myAge = Session(“Age”) Session ID: –Session.SessionID Demo: sessionID.aspx To remove a session –Session.abandon

13 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

14 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


Download ppt "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."

Similar presentations


Ads by Google