Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

AWP Practical for Finals

° Use a multiline text box while acquiring with another text box
HTML:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TextBoxes.aspx.cs"
Inherits="TextBoxes" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Text Boxes</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Text Boxes</h1>
<div>
Enter Single Line Text:
<asp:TextBox ID="txtSingleLine" runat="server"></asp:TextBox>
</div>
<div>
Enter Multiple Lines Text:
<asp:TextBox ID="txtMultipleLines" runat="server" TextMode="MultiLine"
Rows="5"></asp:TextBox>
</div>
<div>
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
OnClick="btnSubmit_Click" />
</div>
<div>
<asp:Label ID="lblResult" runat="server" Text=""></asp:Label>
</div>
</div>
</form>
</body>
</html>

CSharp:-
using System;
using System.Web.UI;
public partial class TextBoxes : Page
{
protected void btnSubmit_Click(object sender, EventArgs e)
{
string singleLineText = txtSingleLine.Text;
string multipleLinesText = txtMultipleLines.Text;
// Display acquired values in the label
lblResult.Text = $"Single Line Text: {singleLineText}<br />Multiple Lines Text:
{multipleLinesText}";
}
}

° factorial of a number create a web page


HTML:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FactorialCalculator.aspx.cs"
Inherits="FactorialCalculator" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Factorial Calculator</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Factorial Calculator</h1>
Enter a number:
<asp:TextBox ID="txtNumber" runat="server"></asp:TextBox>
<asp:Button ID="btnCalculate" runat="server" Text="Calculate"
OnClick="btnCalculate_Click" />
<br /><br />
<asp:Label ID="lblResult" runat="server"></asp:Label>
</div>
</form>
</body>
</html>

CSharp:-
using System;
using System.Web.UI;
public partial class FactorialCalculator : Page
{
protected void btnCalculate_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtNumber.Text))
{
int number = int.Parse(txtNumber.Text);
long factorial = CalculateFactorial(number);
lblResult.Text = $"Factorial of {number} is {factorial}";
}
else
{
lblResult.Text = "Please enter a number.";
}
}

private long CalculateFactorial(int number)


{
if (number == 0 || number == 1)
{
return 1;
}
long result = 1;
for (int i = 2; i <= number; i++)
{
result *= i;
}
return result;
}
}

*********************************************************************
° display a number of visitors given on a webpage
HTML:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="VisitorCount.aspx.cs"
Inherits="VisitorCount" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Visitor Count</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Visitor Count</h1>
<p>Number of visitors: <asp:Label ID="lblVisitorCount"
runat="server"></asp:Label></p>
</div>
</form>
</body>
</html>

CSharp:-
using System;
using System.Web.UI;

public partial class VisitorCount : Page


{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
UpdateVisitorCount();
}
}
private void UpdateVisitorCount()
{
if (Application["VisitorCount"] == null)
{
Application["VisitorCount"] = 1;
}
else
{
int count = (int)Application["VisitorCount"];
Application["VisitorCount"] = count + 1;
}
lblVisitorCount.Text = Application["VisitorCount"].ToString();
}
}

° create a registation page which will accept the details like name age email address and
mobile number
HTML:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Registration.aspx.cs"
Inherits="Registration" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>User Registration</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>User Registration</h1>
<div>
Name: <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</div>
<div>
Age: <asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
</div>
<div>
Email Address: <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</div>
<div>
Mobile Number: <asp:TextBox ID="txtMobile" runat="server"></asp:TextBox>
</div>
<div>
<asp:Button ID="btnRegister" runat="server" Text="Register"
OnClick="btnRegister_Click" />
</div>
</div>
</form>
</body>
</html>
CSharp:-
using System;
using System.Web.UI;
public partial class Registration : Page
{
protected void btnRegister_Click(object sender, EventArgs e)
{
string name = txtName.Text;
string age = txtAge.Text;
string email = txtEmail.Text;
string mobile = txtMobile.Text;
// You can perform validation here before processing the data
// For demonstration purposes, let's just display the entered data
string userDetails = $"Name: {name}<br />Age: {age}<br />Email: {email}<br
/>Mobile: {mobile}";
Response.Write(userDetails);
}
}

*********************************************************************
° create a webpage for the server control for auto post back
HTML:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AutoPostBack.aspx.cs"
Inherits="AutoPostBack" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Auto PostBack Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Auto PostBack Example</h1>
<div>
<asp:DropDownList ID="ddlOptions" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddlOptions_SelectedIndexChanged">
<asp:ListItem Text="Option 1" Value="1"></asp:ListItem>
<asp:ListItem Text="Option 2" Value="2"></asp:ListItem>
<asp:ListItem Text="Option 3" Value="3"></asp:ListItem>
</asp:DropDownList>
</div>
<div>
<asp:Label ID="lblResult" runat="server" Text=""></asp:Label>
</div>
</div>
</form>
</body>
</html>

CSharp:-
using System;
using System.Web.UI;

public partial class AutoPostBack : Page


{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Perform initial setup if needed
}
}
protected void ddlOptions_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedOption = ddlOptions.SelectedItem.Text;
lblResult.Text = $"Selected option: {selectedOption}";
}
}

° count a number of times the current webpage is submitted


HTML:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SubmissionCount.aspx.cs"
Inherits="SubmissionCount" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Submission Count</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Submission Count</h1>
<asp:Label ID="lblCount" runat="server"></asp:Label>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
OnClick="btnSubmit_Click" />
</div>
</form>
</body>
</html>

CSharp:-
using System;
using System.Web.UI;
public partial class SubmissionCount : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Initialize count to 0 when the page is loaded for the first time
Session["SubmissionCount"] = 0;
UpdateCountLabel();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Increment the count each time the button is clicked
int count = Convert.ToInt32(Session["SubmissionCount"]);
count++;
Session["SubmissionCount"] = count;

UpdateCountLabel();
}

private void UpdateCountLabel()


{
int count = Convert.ToInt32(Session["SubmissionCount"]);
lblCount.Text = $"Number of submissions: {count}";
}
}

*********************************************************************
° factorial, money conversion, cube of a number and series
i)Finding factorial Value
using A=System.Console;
namespace Pracs2ai
{
class Program
{
static void Main(string[] args)
{
long fact = 1;
A.WriteLine("Enter a number: ");
int num = int.Parse(A.ReadLine());
for (int i = num; i >= 1; i--)
{
fact = fact * i;
}
A.Write("{0} is factorial of {1}", fact, num);
A.ReadKey();
}
}
}

ii)Money Conversion
using System;
namespace Pracs2aMoneyConversion
{
class Program
{
static void Main(string[] args)
{
int choice;
Console.WriteLine("----Practical 2a-ii----");
Console.WriteLine("\n----Money Conversion---- ");
Console.WriteLine("\nSelect one option : 1 - Dollar to Rupee 2 - Euro to Rupee ");
Console.Write("\nEnter your Choice : ");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
Double dollar, rupee1, val;
Console.Write("\nEnter the amount in Dollars : ");
dollar = Double.Parse(Console.ReadLine());
Console.Write("\nEnter the value of Dollar : ");
val = double.Parse(Console.ReadLine());
rupee1 = dollar * val;
Console.WriteLine("\n{0} USD = {1} INR", dollar, rupee1);
break;
case 2:
Double Euro, rupee2, valu;
Console.Write("\nEnter the amount in Euro : ");
Euro = Double.Parse(Console.ReadLine());
Console.Write("\nEnter the value of Euro : ");
valu = double.Parse(Console.ReadLine());
rupee2 = Euro * valu;
Console.WriteLine("\n{0} Euro = {1} INR", Euro, rupee2);
break;
}
Console.ReadKey();
}
}
}

iii)Cube:-
using System;
class Program
{
static void Main()
{
// Example usage:
int number = 5;
double cube = CalculateCube(number);
Console.WriteLine($"Cube of {number} is {cube}");
}

static double CalculateCube(double number)


{
return Math.Pow(number, 3);
}
}

iv)

° calendar control
CSharp:-
Calender properties set for this example:
<asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC"
BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest"
Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399"
Height="200px"
NextPrevFormat="ShortMonth" OnDayRender="Calendar1_DayRender"
ShowGridLines="True" Width="300px"
OnSelectionChanged="Calendar1_SelectionChanged" >
<DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
<NextPrevStyle BorderStyle="Solid" BorderWidth="2px" Font-Size="9pt"
ForeColor="#FFFFCC" />
<OtherMonthDayStyle BackColor="#FFCC99" BorderStyle="Solid"
ForeColor="#CC9966" />
<SelectedDayStyle BackColor="Red" Font-Bold="True" />
<SelectorStyle BackColor="#FFCC66" />
<TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt"
ForeColor="#FFFFCC" />
<TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
<WeekendDayStyle Height="50px" />
</asp:Calendar>
calndrCtrl.aspx.cs
protected void btnResult_Click(object sender, EventArgs e)
{
Calendar1.Caption = "SAMBARE";
Calendar1.FirstDayOfWeek = FirstDayOfWeek.Sunday;
Calendar1.NextPrevFormat = NextPrevFormat.ShortMonth;
Calendar1.TitleFormat = TitleFormat.Month;
Label2.Text = "Todays Date"+Calendar1.TodaysDate.ToShortDateString();
Label3.Text = "Ganpati Vacation Start: 9-13-2018";
TimeSpan d = new DateTime(2018, 9, 13) - DateTime.Now;
Label4.Text = "Days Remaining For Ganpati Vacation:"+d.Days.ToString();
TimeSpan d1 = new DateTime(2018, 12, 31) - DateTime.Now;
Label5.Text = "Days Remaining for New Year:"+d1.Days.ToString();
if (Calendar1.SelectedDate.ToShortDateString() == "9-13-2018")
Label3.Text = "<b>Ganpati Festival Start</b>";
if (Calendar1.SelectedDate.ToShortDateString() == "9-23-2018")
Label3.Text = "<b>Ganpati Festival End</b>";
}
protected void Calendar1_DayRender(object sender,
System.Web.UI.WebControls.DayRenderEventArgs e)
{
if (e.Day.Date.Day == 5 && e.Day.Date.Month == 9)
{
e.Cell.BackColor = System.Drawing.Color.Yellow;
Label lbl = new Label();
lbl.Text = "<br>Teachers Day!";
e.Cell.Controls.Add(lbl);
Image g1 = new Image();
g1.ImageUrl = "td.jpg";
g1.Height = 20;
g1.Width = 20;
e.Cell.Controls.Add(g1);
}
if (e.Day.Date.Day == 13 && e.Day.Date.Month == 9)
{
Calendar1.SelectedDate = new DateTime(2018, 9, 12);
Calendar1.SelectedDates.SelectRange(Calendar1.SelectedDate,
Calendar1.SelectedDate.AddDays(10));
Label lbl1 = new Label();
lbl1.Text = "<br>Ganpati!";
e.Cell.Controls.Add(lbl1);
}
}
protected void btnReset_Click(object sender, EventArgs e)
{
Label1.Text = "";
Label2.Text = "";
Label3.Text = "";
Label4.Text = "";
Label5.Text = "";
Calendar1.SelectedDates.Clear();
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
Label1.Text = "Your Selected Date:" +
Calendar1.SelectedDate.Date.ToString();
}

*********************************************************************
° accept EID Ename Edpartment and salary from the employees using drop-down list in the
ascending order
HTML:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EmployeeDetails.aspx.cs"
Inherits="EmployeeDetails" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Employee Details</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Employee Details</h1>
<div>
Employee ID:
<asp:DropDownList ID="ddlEmployeeID"
runat="server"></asp:DropDownList>
</div>
<div>
Employee Name:
<asp:DropDownList ID="ddlEmployeeName"
runat="server"></asp:DropDownList>
</div>
<div>
Department:
<asp:DropDownList ID="ddlDepartment" runat="server"></asp:DropDownList>
</div>
<div>
Salary:
<asp:DropDownList ID="ddlSalary" runat="server"></asp:DropDownList>
</div>
</div>
</form>
</body>
</html>

CSharp:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
public partial class EmployeeDetails : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Simulated employee data (you might retrieve this from a database)
List<Employee> employees = new List<Employee>
{
new Employee { EID = 101, Ename = "John Doe", Department = "IT", Salary =
50000 },
new Employee { EID = 102, Ename = "Jane Smith", Department = "HR", Salary
= 60000 },
new Employee { EID = 103, Ename = "Alice Johnson", Department = "Finance",
Salary = 55000 }
// Add more employees as needed
};
// Populate dropdown lists with employee data
PopulateDropDownList(ddlEmployeeID, employees.OrderBy(emp =>
emp.EID).ToList(), "EID");
PopulateDropDownList(ddlEmployeeName, employees.OrderBy(emp =>
emp.Ename).ToList(), "Ename");
PopulateDropDownList(ddlDepartment, employees.OrderBy(emp =>
emp.Department).ToList(), "Department");
PopulateDropDownList(ddlSalary, employees.OrderBy(emp =>
emp.Salary).ToList(), "Salary");
}
}
private void PopulateDropDownList(DropDownList ddl, List<Employee> employees, string
field)
{
ddl.DataTextField = field;
ddl.DataValueField = field;
ddl.DataSource = employees;
ddl.DataBind();
}
}
public class Employee
{
public int EID { get; set; }
public string Ename { get; set; }
public string Department { get; set; }
public decimal Salary { get; set; }
}

° accept number from user and display in 4 rows with different format
CSharp:-
using System;
class Program
{
static void Main()
{
Console.WriteLine("Enter a number:");
if (int.TryParse(Console.ReadLine(), out int number))
{
Console.WriteLine($"Number: {number}");
Console.WriteLine($"Padded: {number.ToString("0000")}");
Console.WriteLine($"Currency: {number.ToString("C")}");
Console.WriteLine($"Exponential: {number.ToString("E")}");
}
else
{
Console.WriteLine("Invalid input. Please enter a valid number.");
}
}
}

*********************************************************************
° display the date properties year, date, month, hour, minute, second and milliseconds as
well as display the number of days
CSharp:-
using System;
class Program
{
static void Main()
{
DateTime currentDateTime = DateTime.Now;
// Display date properties
Console.WriteLine($"Year: {currentDateTime.Year}");
Console.WriteLine($"Month: {currentDateTime.Month}");
Console.WriteLine($"Day: {currentDateTime.Day}");
Console.WriteLine($"Hour: {currentDateTime.Hour}");
Console.WriteLine($"Minute: {currentDateTime.Minute}");
Console.WriteLine($"Second: {currentDateTime.Second}");
Console.WriteLine($"Milliseconds: {currentDateTime.Millisecond}");
// Calculate number of days in the current month
int daysInMonth = DateTime.DaysInMonth(currentDateTime.Year,
currentDateTime.Month);
Console.WriteLine($"Number of days in the current month: {daysInMonth}");
// Calculate number of days since the start of the year
TimeSpan daysSinceStartOfYear = currentDateTime - new
DateTime(currentDateTime.Year, 1, 1);
Console.WriteLine($"Number of days since the start of the year:
{daysSinceStartOfYear.Days}");
}
}

° create a webpage to accept roll number name class phone and email using drop-down list
HTML:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="StudentDetails.aspx.cs"
Inherits="StudentDetails" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Student Details</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Student Details</h1>
<div>
Roll Number:
<asp:DropDownList ID="ddlRollNumber"
runat="server"></asp:DropDownList>
</div>
<div>
Name:
<asp:DropDownList ID="ddlName" runat="server"></asp:DropDownList>
</div>
<div>
Class:
<asp:DropDownList ID="ddlClass" runat="server">
<asp:ListItem Text="Class 1" Value="Class 1"></asp:ListItem>
<asp:ListItem Text="Class 2" Value="Class 2"></asp:ListItem>
<!-- Add more class options as needed -->
</asp:DropDownList>
</div>
<div>
Phone:
<asp:DropDownList ID="ddlPhone" runat="server"></asp:DropDownList>
</div>
<div>
Email:
<asp:DropDownList ID="ddlEmail" runat="server"></asp:DropDownList>
</div>
</div>
</form>
</body>
</html>

CSharp:-
using System;
using System.Collections.Generic;
using System.Web.UI;
public partial class StudentDetails : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Simulated student data (you might retrieve this from a database)
List<Student> students = new List<Student>
{
new Student { RollNumber = "001", Name = "John Doe", Phone =
"1234567890", Email = "john@example.com" },
new Student { RollNumber = "002", Name = "Jane Smith", Phone =
"9876543210", Email = "jane@example.com" },
// Add more students as needed
};
// Populate drop-down lists with student data
PopulateDropDownList(ddlRollNumber, students, "RollNumber");
PopulateDropDownList(ddlName, students, "Name");
PopulateDropDownList(ddlPhone, students, "Phone");
PopulateDropDownList(ddlEmail, students, "Email");
}
}
private void PopulateDropDownList(DropDownList ddl, List<Student> students, string field)
{
ddl.DataTextField = field;
ddl.DataValueField = field;
ddl.DataSource = students;
ddl.DataBind();
}
}
public class Student
{
public string RollNumber { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
}
*********************************************************************
° demonstrate all the string option
CSharp:-
using System;
namespace Pracs1b
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("----Practical 1b----");
String str1 = "Sumeet", str2 = " Rathod";
Console.WriteLine("\nString1 - " + str1);
Console.WriteLine("\nString2 - " + str2);
Console.WriteLine("\n--------------------");
//Length
Console.WriteLine("\nLength of String1 - " + str1.Length);
//Concat()
Console.WriteLine("\nConcat() - " + String.Concat(str1, str2));
//Trim()
Console.WriteLine("\nTrim() - " + str2.Trim());
//ToUpper()
Console.WriteLine("\nToUpper() - " + str1.ToUpper());
//ToLower()
Console.WriteLine("\nToLower() - " + str2.ToLower());
//Substring()
Console.WriteLine("\nSubstring() - " + str1.Substring(0,2));
Console.ReadKey();
}
}
}
° design a aap .net webpage using radio button drop-down list label and text box
HTML:-
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FormPage.aspx.cs"
Inherits="WebApplication.FormPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Form Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>User Information</h1>
<div>
<asp:Label ID="lblName" runat="server" Text="Name: "></asp:Label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</div>
<div>
<asp:Label ID="lblGender" runat="server" Text="Gender: "></asp:Label>
<asp:RadioButton ID="rbtnMale" runat="server" Text="Male"
GroupName="genderGroup" />
<asp:RadioButton ID="rbtnFemale" runat="server" Text="Female"
GroupName="genderGroup" />
</div>
<div>
<asp:Label ID="lblCountry" runat="server" Text="Country: "></asp:Label>
<asp:DropDownList ID="ddlCountry" runat="server">
<asp:ListItem Text="Select Country" Value=""></asp:ListItem>
<asp:ListItem Text="USA" Value="USA"></asp:ListItem>
<asp:ListItem Text="Canada" Value="Canada"></asp:ListItem>
<asp:ListItem Text="UK" Value="UK"></asp:ListItem>
<!-- Add more country options as needed -->
</asp:DropDownList>
</div>
<div>
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
OnClick="btnSubmit_Click" />
</div>
</div>
</form>
</body>
</html>

CSharp:-
using System;
using System.Web.UI;

namespace WebApplication
{
public partial class FormPage : Page
{
protected void btnSubmit_Click(object sender, EventArgs e)
{
string name = txtName.Text;
string gender = rbtnMale.Checked ? "Male" : "Female";
string country = ddlCountry.SelectedValue;

// Perform actions with the collected data, like saving to a database or displaying it
// For example, you could display the data in a label on the page:
lblResult.Text = $"Name: {name}<br />Gender: {gender}<br />Country: {country}";
}
}
}
*********************************************************************
° create a delegate ( reference 2c i)
CSharp:-
using System;
// Delegate declaration
public delegate void EventHandler(string message);
class Publisher
{
// Event declaration of delegate type
public event EventHandler RaiseEvent;
public void DoSomething()
{
// Raise the event
RaiseEvent?.Invoke("Something happened!");
}
}
class Subscriber
{
public void HandleEvent(string message)
{
Console.WriteLine($"Handled: {message}");
}
}
class Program
{
static void Main(string[] args)
{
Publisher publisher = new Publisher();
Subscriber subscriber = new Subscriber();
// Subscribe to the event
publisher.RaiseEvent += subscriber.HandleEvent;
// Trigger the action in the publisher
publisher.DoSomething();
}
}

° how to read and write cookie from a client


Read:-
using System;
using System.Web;
class Program
{
static void Main()
{
// Access the cookie from the request
HttpCookie cookie = HttpContext.Current.Request.Cookies["MyCookie"];
if (cookie != null)
{
// Read cookie values
string userID = cookie["UserID"];
string userName = cookie["UserName"];
Console.WriteLine($"UserID: {userID}, UserName: {userName}");
}
else
{
Console.WriteLine("Cookie not found or expired.");
}
}
}

Write:-
using System;
using System.Web;
class Program
{
static void Main()
{
// Creating a new cookie
HttpCookie cookie = new HttpCookie("MyCookie");
// Setting cookie values
cookie["UserID"] = "123";
cookie["UserName"] = "JohnDoe";
// Set expiration time (optional)
cookie.Expires = DateTime.Now.AddDays(1); // Expires in 1 day
// Add the cookie to the response
HttpContext.Current.Response.Cookies.Add(cookie);
Console.WriteLine("Cookie has been created and sent to the client.");
}
}

You might also like