In this tutorial i will explain how to create/read cookies in asp.net using both c# and vb.net with example. or create or get cookie value from asp.net website using c#, vb.net with example. or also explain how to check if cookie exists or not in asp.net website using both c# and vb.net with example. By using "Response.Cookies" we can create Cookies in asp.net and by using “Request.Cookies” properties we can get cookie value from asp.net website or using c# and vb.net.
Description:
In previous tutorial I explained How To Get User's IP Address in Asp.Net, Encrypt and Decrypt Querystring Paramiter Values in Asp.net, How to Generate One Time Password (OTP) in Asp.Net, Ways to Fix Unobtrusive Validation Error in Asp.Net 4.5, Now I will explain how to create cookie or how to get values of cookie from asp.net website using c# and vb.net with example.
So let's create one example for demonstration purpose.
Aspx Code:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Cookies Example in Asp.Net</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="lbl_Cookies" Font-Size="Large" runat="server"></asp:Label> <br /> <br /> <asp:Button ID="btn_Create" runat="server" Text="Create Cookies" OnClick="btn_Create_Click"/> <asp:Button ID="btn_GetCookies" runat="server" Text="Get Cookies" OnClick="btn_GetCookies_Click" /> </div> </form> </body> </html>
After completion of aspx page add following namespaces in code behind
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;
C# Code to Create and Read Values of Cookie
protected void btn_Create_Click(object sender, EventArgs e) { Response.Cookies["EmpId"].Value = "144510"; Response.Cookies["EmpId"].Expires = DateTime.Now.AddDays(1); Response.Cookies["Employee_Name"].Value = "Nikunj Satasiya"; Response.Cookies["Employee_Name"].Expires = DateTime.Now.AddDays(1); Response.Cookies["Salary"].Value = "45000/-"; Response.Cookies["Salary"].Expires = DateTime.Now.AddDays(1); lbl_Cookies.Text = "Cookie Created Successfully...!"; } protected void btn_GetCookies_Click(object sender, EventArgs e) { string EmpId = Request.Cookies["EmpId"].Value; string Empname = Request.Cookies["Employee_Name"].Value; string Empsalary = Request.Cookies["Salary"].Value; lbl_Cookies.Text = "<b>Cookie Values are </b> : EmpId: " + EmpId + ", Employee Name: " + Empname + ", Salary:" + Empsalary; }
Vb.Net Code to Create and Read Values of Cookie
Protected Sub btn_Create_Click(sender As Object, e As EventArgs) Handles btn_Create.Click Response.Cookies("EmpId").Value = "144510" Response.Cookies("EmpId").Expires = DateTime.Now.AddDays(1) Response.Cookies("Employee_Name").Value = "Nikunj Satasiya" Response.Cookies("Employee_Name").Expires = DateTime.Now.AddDays(1) Response.Cookies("Salary").Value = "45000/-" Response.Cookies("Salary").Expires = DateTime.Now.AddDays(1) lbl_Cookies.Text = "Cookie Created Successfully...!" End Sub Protected Sub btn_GetCookies_Click(sender As Object, e As EventArgs) Handles btn_GetCookies.Click Dim EmpId As String = Request.Cookies("EmpId").Value Dim Empname As String = Request.Cookies("Employee_Name").Value Dim Empsalary As String = Request.Cookies("Salary").Value lbl_Cookies.Text = "<b>Cookie Values are </b> : EmpId: " + EmpId + ", Employee Name: " + Empname + ", Salary:" + Empsalary End Sub
Demo:
If you have any kind of question about any post, Feel free to ask.You can simply drop a comment below post. Your feedback and suggestions will be highly appreciated. ConversionConversion EmoticonEmoticon