0% found this document useful (0 votes)
23 views

AWT Assignment 3

Uploaded by

kamble2682001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

AWT Assignment 3

Uploaded by

kamble2682001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Assignment 3

Q1)Design a webpage to display the use of LINQ.


1. Create a Database.
2. Perform functions like Filtering, Ordering, and Joining.

Aim :
To design a webpage and display use of LINQ
Objective :
To create a database and perform functions to it.
Theory :
LINQ (Language Integrated Query) is uniform query syntax in C# and VB.NET to retrieve
data from different sources and formats. LINQ queries return results as objects. It enables you
to uses object-oriented approach on the result set and not to worry about transforming
different formats of results into objects. LINQ provides functions to query cached data from
all kinds of data sources.
Aim :
To design a webpage and display use of LINQ
Objective :
To create a database and perform functions to it.
Theory :
LINQ (Language Integrated Query) is uniform query syntax in C# and VB.NET to retrieve
data from different sources and formats. LINQ queries return results as objects. It enables you
to uses object-oriented approach on the result set and not to worry about transforming
different formats of results into objects. LINQ provides functions to query cached data from
all kinds of data sources.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LinqD22
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
/* DataClasses1DataContext context = new DataClasses1DataContext();
var result = from Employee in context.Employees
where Employee.Gender == "Male"
select Employee;
GridView1.DataSource = result;
GridView1.DataBind();

//ordering
DataClasses1DataContext context = new DataClasses1DataContext();
var result = from Employee in context.Employees
where Employee.Department == "mca"
orderby Employee.Emp_fname ascending
select Employee;
GridView1.DataSource = result;
GridView1.DataBind();
DataClasses1DataContext context = new DataClasses1DataContext();
var result = from Employee in context.Employees
group Employee by Employee.Department;
GridView1.DataSource = result;
GridView1.DataBind();*/

DataClasses1DataContext context = new DataClasses1DataContext();


var result = from Employee in context.Employees
join dist in context.dists on Employee.Gender equals dist.Gender
select new { Employee = Employee.Emp_fname, distGender = dist.Gender
};
GridView1.DataSource = result;
GridView1.DataBind();

}
}
}

OutPut:

1.Where employee.gender= =”male


2.Where Employee.Department==”mca”/Ascending

3.Joining the names between table dist & Employee


Q2)Demonstrate the working of entity framework in dot net with Insert,
Update, Delete
Aim:
To design entity framework in dot net
Objective:
To demonstrate the working of entity framework in dot net with following function.
Theory:
Entity framework is an Object Relational Mapping (ORM) framework that offers an automated
mechanism to developers for storing and accessing the data in the database. An ORM takes
care of creating database connections and executing commands, as well as taking query results
and automatically materializing those results as your application objects. An ORM also helps
to keep track of changes to those objects, and when instructed, it will also persist those changes
back to the database for you.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;

namespace Entity1
{
public partial class WebForm1 : System.Web.UI.Page
{
emp model1 = new emp();
protected void Page_Load(object sender, EventArgs e)
{
//clear();
}

protected void Button1_Click(object sender, EventArgs e)


{
model1.Name = TextBox1.Text.Trim();
model1.City = TextBox2.Text.Trim();
model1.Department = TextBox3.Text.Trim();
using (Database1Entities db = new Database1Entities())
{
db.emps.Add(model1);
db.SaveChanges();
}

clear();
Response.Write("Record inserted sucessfully");
GridView1.DataBind();
}
void clear()
{
TextBox1.Text = TextBox2.Text = TextBox3.Text = " ";
Button1.Text = "Save";
model1.Emp_ID = 0;
}

protected void Button2_Click(object sender, EventArgs e)


{
model1.Name = TextBox1.Text.Trim();
model1.City = TextBox2.Text.Trim();
model1.Department = TextBox3.Text.Trim();
model1.Emp_ID = Convert.ToInt32(TextBox4.Text);

using (Database1Entities db = new Database1Entities())


{
db.Entry(model1).State = EntityState.Deleted;
db.SaveChanges();
}
Response.Write("Record Deleted successfully");
GridView1.DataBind();

protected void Button3_Click(object sender, EventArgs e)


{
model1.Name = TextBox1.Text.Trim();
model1.City = TextBox2.Text.Trim();
model1.Department = TextBox3.Text.Trim();
model1.Emp_ID = Convert.ToInt32(TextBox4.Text);
using (Database1Entities db = new Database1Entities())
{
db.Entry(model1).State = EntityState.Modified;
db.SaveChanges();
}
Response.Write("Record Updated successfully");
GridView1.DataBind();
}
}
}
Output:
Delete:

Update:
Q3)Design Web Application to produce and Consume a WCF Service for
calculator.

Aim:

To design web application for wcf service.

Objective:

To produce and consume a wcf service for calculator.

Theory:

WCF stands for Windows Communication Foundation. The elementary feature of WCF is
interoperability. It is one of the latest technologies of Microsoft that is used to build service-
oriented applications. Based on the concept of message-based communication, in which an
HTTP request is represented uniformly, WCF makes it possible to have a unified API
irrespective of diverse transport mechanisms. WCF services offer enhanced reliability as well
as security in comparison to ASMX (Active Server Methods) web services. It offers scalability
and support for up-coming web service standards.

Code:
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebServices_C23047
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

protected void Button1_Click(object sender, EventArgs e)


{
Double a=Convert.ToDouble(TextBox1.Text);
Double b=Convert.ToDouble(TextBox2.Text);
localhost.WebService1 service1 = new localhost.WebService1();
Double res =service1.Addition(a, b);
Label1.Text = "Addition is "+res.ToString();
}

protected void Button2_Click(object sender, EventArgs e)


{
Double a = Convert.ToDouble(TextBox1.Text);
Double b = Convert.ToDouble(TextBox2.Text);
localhost.WebService1 service1 = new localhost.WebService1();
Double res = service1.Subtraction(a, b);
Label1.Text = "Subtraction is " + res.ToString();
}

protected void Button3_Click(object sender, EventArgs e)


{
Double a = Convert.ToDouble(TextBox1.Text);
Double b = Convert.ToDouble(TextBox2.Text);
localhost.WebService1 service1 = new localhost.WebService1();
Double res = service1.Multiplication(a, b);
Label1.Text = "Multiplication is " + res.ToString();
}

protected void Button4_Click(object sender, EventArgs e)


{
Double a = Convert.ToDouble(TextBox1.Text);
Double b = Convert.ToDouble(TextBox2.Text);
localhost.WebService1 service1 = new localhost.WebService1();
Double res = service1.Division(a, b);
Label1.Text = "Division is " + res.ToString();
}
}
}

WebForm1.aspx

To add this WebService1.asmx File Search For Web Service (ASMX)

WebService1.asmx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebServices_C23047
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX,
uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public Double Addition(double a,double b)
{
return a + b;
}
[WebMethod]
public Double Subtraction(double a, double b)
{
return a - b;
}
[WebMethod]
public Double Multiplication(double a, double b)
{
return a * b;
}
[WebMethod]
public Double Division(double a, double b)
{
return (a / b);
}
}
}
Output:
Q4)Design Web Application to produce and Consume a WCF Service,
Perform in that insertion of record in database.

Aim:

To design web application and insert record in database.

Objective:

To produce and consume a wcf service to perform insertion of record in databse.

Theory:
WCF stands for Windows Communication Foundation. The elementary feature of WCF is
interoperability. It is one of the latest technologies of Microsoft that is used to build service-
oriented applications. Based on the concept of message-based communication, in which an
HTTP request is represented uniformly, WCF makes it possible to have a unified API
irrespective of diverse transport mechanisms. WCF services offer enhanced reliability as well
as security in comparison to ASMX (Active Server Methods) web services. It offers scalability
and support for up-coming web service standards.
Code:

1. Service.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data.SqlClient;

namespace Database3wcf
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the
class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please
select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
public string Insert(InsertUser user)
{
string msg;
SqlConnection con = new SqlConnection(@"Data
Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\user\source\repos\Database3wcf
\Database3wcf\App_Data\Database1.mdf;Integrated Security=True");

con.Open();
SqlCommand cmd = new SqlCommand("Insert into emp(Name,City)
values(@Name,@City)", con);

cmd.Parameters.AddWithValue("@Name", user.Name);
cmd.Parameters.AddWithValue("@City", user.City);

int result = cmd.ExecuteNonQuery();


if (result == 1)
{
msg = "Record inserted Sucessfully";
}
else
{
msg = "Failed to insert";
}

return msg;
}

}
}

2. IService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace Database3wcf
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the
interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{

[OperationContract]
string Insert(InsertUser user); //use class name InsertUser
// TODO: Add your service operations here
}

// Use a data contract as illustrated in the sample below to add composite types
to service operations.
[DataContract]
public class InsertUser
{
string name = string.Empty;
string city = string.Empty;
[DataMember]
public string Name
{
get { return name; }
set { name = value; }
}
[DataMember]
public string City
{
get { return city; }
set { city = value; }
}
}
}

1. WebForm1.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication1.ServiceReference1;

namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)


{

}
ServiceReference1.Service1Client client = new
ServiceReference1.Service1Client();
protected void Button1_Click(object sender, EventArgs e)
{
InsertUser u = new InsertUser();
u.Name = TextBox1.Text;
u.City = TextBox2.Text;
string r = client.Insert(u);
Label1.Text = r.ToString();
GridView1.DataBind();

}
}
}

Output:
Q5)Demonstrate data consumption by web application using simple web
service

Aim:

To design web application using simple web service.

Objective:

To demonstrate data consumption by designing web application for simple web service.

Theory:

Entity framework is an Object Relational Mapping (ORM) framework that offers an automated
mechanism to developers for storing and accessing the data in the database. An ORM takes
care of creating database connections and executing commands, as well as taking query results
and automatically materializing those results as your application objects. An ORM also helps
to keep track of changes to those objects, and when instructed, it will also persist those changes
back to the database for you.
Code:
1. WebService1.asmx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace hellowebservices
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://myservice.com/service")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX,
uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string WelcomeMessage(string fname, string lname)
{
return string.Format("{0} {1}Welcome to Our Web Service",fname,lname);
}
}
}
1. WebForm1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
ServiceReference1.WebService1SoapClient client = new
ServiceReference1.WebService1SoapClient();
protected void Button1_Click(object sender, EventArgs e)
{
string result = client.WelcomeMessage(TextBox1.Text, TextBox2.Text);
Label1.Text = result;
}
}
}
Output:
Q6)Design web applications to produce and consume simple web services.

Aim:

To design web applications to produce and consume simple web services.

Objective:
To design simple web services.

Theory:
Entity framework is an Object Relational Mapping (ORM) framework that offers an
automated mechanism to developers for storing and accessing the data in the database. An
ORM takes care of creating database connections and executing commands, as well as
taking query results and automatically materializing those results as your application
objects. An ORM also helps to keep track of changes to those objects, and when
instructed, it will also persist those changes back to the database for you.

Code:

1. WebService1.asmx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace SimpleWebService6Rightwala
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://Pranav.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX,
uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{

[WebMethod]
public string Insert(string name, string city)
{
Class1 abc = new Class1
{
Name = name,
City = city
};
string status = abc.Insert();
if (status == "PASS")
return "Record inserted Sucessfully";
else
return "Failed to insert";
}
}
}

1. Class1.cs
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace SimpleWebService6Rightwala
{
public class Class1
{
private string name;
private string city;
public string Name
{
get { return name; }
set { name = value; }
}
public string City
{
get { return city; }
set { city = value; }
}
public string Insert()
{
string msg;
SqlConnection con = new SqlConnection(@"Data
Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\mishr\source\repos\SimpleWebSe
rvice6Rightwala\SimpleWebService6Rightwala\App_Data\Database1.mdf;Integrated
Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("Insert into Student(Name,City)
values(@Name,@City)", con);

cmd.Parameters.AddWithValue("@Name", Name);
cmd.Parameters.AddWithValue("@City", City);

int result = cmd.ExecuteNonQuery();


if (result == 1)
{
msg = "PASS";
}

else
{
msg = "FAIL";
}

return msg;

}
}
}

1. WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Client
{
public partial class WebForm1 : System.Web.UI.Page
{
ServiceReference1.WebService1SoapClient client = new
ServiceReference1.WebService1SoapClient();
protected void Page_Load(object sender, EventArgs e)
{

protected void Button1_Click(object sender, EventArgs e)


{
string result = client.Insert(TextBox1.Text, TextBox2.Text);
Label1.Text = result.ToString();
GridView1.DataBind();
}
}

Output:
Q7)Write a program to demonstrate the ViewState.

Aim:

To demonstrate the viewstate.

Objective:

To demonstrate ViewState.

Theory:
View state is the method that the ASP.NET page framework uses to preserve page and
control values between round trips. When the HTML markup for the page is rendered, the
current state of the page and values that must be retained during postback are serialized into
base64-encoded strings. This information is then put into the view state hidden field or fields

Code:
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace viewstate
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

protected void Button2_Click(object sender, EventArgs e)


{
if (ViewState["myvalue"] != null)
TextBox1.Text = ViewState["myvalue"].ToString();

protected void Button1_Click(object sender, EventArgs e)


{
ViewState["myvalue"] = TextBox1.Text.ToString();
TextBox1.Text = " ";
}
}
}
Output:
Q8)Write a program to demonstrate the Hidden Object.

Aim:

To demonstrate the hidden object.

Objective:

To demonstrate the hidden objects


Theory:

HiddenField, as name implies, is hidden. This is non visual control in ASP.NET where you
can save the value. This is one of the types of client-side state management tools. It stores the
value between the roundtrip

Code:
1. WebForm1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Hiddenfield
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

protected void Button1_Click(object sender, EventArgs e)


{
HiddenField1.Value = TextBox1.Text;
}

protected void Button2_Click(object sender, EventArgs e)


{
Label1.Text = HiddenField1.Value;
}
}
}

Output:
9.Write a program to demonstrate the query string.

Aim:

To demonstrate the query string.

Objective:

To demonstrate the query string.

Theory:

A query string is one of the techniques in Web applications to send data from one webform to
another through the URL. A query string consists of two parts, field and value, and each of
pair separated by ampersand (&). The ?(question mark in a query string indicates the
beginning of a query string and it's value.

Code:

1. WebForm1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace querystring
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

protected void Button1_Click(object sender, EventArgs e)


{
Response.Redirect("WebForm2.aspx?myname= " + TextBox1.Text.ToString());
}
}
}
]2.WebForm2.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace querystring
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "Welcome" + Request.QueryString["myname"];
}
}
}

Output:
Q10)Create a MVC application that displays data from the model.
Studentcontoller.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC2.Models;

namespace MVC2.Controllers
{
public class studentController : Controller
{
// GET: student
public ActionResult Index()
{
student std = new student();
std.ID = 1;
std.Name = "Mahesh";
return View(std);
}
}
}

Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVC2.Models
{
public class student
{
public int ID
{
get;
set;
}
public string Name
{
get;
set;
}
}
}
Index.cshtml
@model MVC2.Models.student

@{
ViewBag.Title = "Index";
}

<h2>student information</h2>
student ID:@Model.ID </br>
student Name:@Model.Name
Output:
Q11)Write an application that accepts data from one page and displays it in
another page using MVC (Passing data between views).
Aim:
To write an application that accepts data from one page and displays it in another page using
MVC.
Objective:
To accept data from one page and display it in another page.
Theory:
The Model component of MVC application represents the state of a particular data part or data
portion of the application and it usually interacts with the file, database, and the web service
etc. It actually represent the logic

Code:
1.Studentcontroller.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCgetdatastudent23.Models;

namespace MVCgetdatastudent23.Controllers
{
public class StudentController : Controller
{
// GET: Student
public ActionResult Index()
{
return View();
}

public ActionResult ShowStudent()


{
Student std = new Student();
std.StudentId = int.Parse(Request.Form["sid"].ToString());
std.StudentName = Request.Form["sname"].ToString();
return View(std);

}
}
2.Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCgetdatastudent23.Models
{
public class Student
{
public int StudentId
{
get;
set;
}
public string StudentName
{
get;
set;
}
}
}
3.Showstudent.cshtml
@model MVCgetdatastudent23.Models.Student

@{
ViewBag.Title = "ShowStudent";
}

<h2>ShowStudent</h2>
<div>Student ID: @Model.StudentId</div>
<div>Student Name: @Html.Label("sname",Model.StudentName)</div>

4.index.cshtml
@model MVCgetdatastudent23.Models.Student

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>
@using (Html.BeginForm("ShowStudent", "Student", FormMethod.Post))
{
<div>Enter ID @Html.TextBox("sid")</div>
<div>Enter Name @Html.TextBox("sname")</div>
<input type="submit" value="Display" />

Output:
Q12)Write a program that shows crud operation using MVC.
Aim:

To show crud operation using MVC

Objective:

To perform curd operation using MVC.

Theory:

CRUD operation in MVC is the basic operations, where CRUD denotes create, read, update,
and delete. ... MVC is the Model View Controller. MVC is a design pattern that is used to

differentiate the data from business logic and presentation logic. It gives a pattern that helps
in designing the web application.

Code:
1. Product.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace crudmvc.Models
{
public class product
{
public int id { get; set; }
public string name { get; set; }
public int price { get; set; }
}
}
2. ProductController.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using crudmvc.Models;

namespace crudmvc.Controllers
{
public class ProductController : Controller
{
SqlConnection con = new
SqlConnection(@"DataSource=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\jahnv\sourc
e\repos\crumvc\crudmvc\App_Data\Database1.mdf;Integrated Security=True");
// GET: Product
public ActionResult Index()
{
SqlDataAdapter da = new SqlDataAdapter("select * fromProductDetails", con);
DataSet ds = new DataSet();
da.Fill(ds);
List<product> prod = new List<product>();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
product p1 = new product();
p1.id = int.Parse(ds.Tables[0].Rows[i][0].ToString());
p1.name = ds.Tables[0].Rows[i][1].ToString();
p1.price = int.Parse(ds.Tables[0].Rows[i][2].ToString());
prod.Add(p1);
}
return View(prod);

// GET: Product/Details/5
public ActionResult Details(int id)
{
SqlDataAdapter da = new SqlDataAdapter("Select * from ProductDetails where Id=" + id,
con);

DataSet ds = new DataSet();


da.Fill(ds);
product p = new product();
p.id = int.Parse(ds.Tables[0].Rows[0][0].ToString());
p.name = ds.Tables[0].Rows[0][1].ToString();
p.price = int.Parse(ds.Tables[0].Rows[0][2].ToString());
return View(p);
}

// GET: Product/Create{to show data}


public ActionResult Create()
{
return View();
}

// POST: Product/Create{to enter data in database}


[HttpPost]
public ActionResult Create(product p)
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand("Insert into ProductDetailsvalues('" + p.name + "',"
+ p.price + ")", con);
cmd.ExecuteNonQuery();
con.Close();
return RedirectToAction("Index");
// TODO: Add insert logic here
}
catch
{
return View();
}
}
// GET: Product/Edit/5
public ActionResult Edit(int id)
{
SqlDataAdapter da = new SqlDataAdapter("select * fromProductDetails where id=" + id, con);
DataSet ds = new DataSet();
da.Fill(ds);
product p1 = new product();
p1.id = int.Parse(ds.Tables[0].Rows[0][0].ToString());
p1.name = ds.Tables[0].Rows[0][1].ToString();
p1.price = int.Parse(ds.Tables[0].Rows[0][2].ToString()); ;
return View(p1);
}

// POST: Product/Edit/5
[HttpPost]
public ActionResult Edit(int id, product p)
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand("update ProductDetails setName='" + p.name + "',
Price = " + p.price + " where Id =" + id, con);
cmd.ExecuteNonQuery();
con.Close();
return RedirectToAction("Index");
// TODO: Add update logic here
}
catch
{
return View();
}
}

// GET: Product/Delete/5
public ActionResult Delete(int id)
{
SqlDataAdapter da = new SqlDataAdapter("select * from ProductDetails where id=" + id,
con);
DataSet ds = new DataSet();
da.Fill(ds);
product p1 = new product();
p1.id = int.Parse(ds.Tables[0].Rows[0][0].ToString());
p1.name = ds.Tables[0].Rows[0][1].ToString();
p1.price = int.Parse(ds.Tables[0].Rows[0][2].ToString()); ;
return View(p1);

// POST: Product/Delete/5
[HttpPost]
public ActionResult Delete(int id, product p)
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand(" delete from ProductDetails where id=" + p.id ,
con);
cmd.ExecuteNonQuery();
con.Close();
return RedirectToAction("Index");
// TODO: Add delete logic here
}
catch
{
return View();
}
}
}
}

Output:
13. Write a program to count the number of live users in your web
application

Aim:

To write a program to count the number of live users in web application.

Objective:

To count the number of live users in web application.

Theory:

We have all seen many websites displaying the number of online users live currently on their
web page so similarly we can count the number of person live on our website using the
following code and implementing a counter.

Code:
1. WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace vistercounter1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int count = 0;
if (Application["pcount"] != null)
{
count = Int32.Parse(Application["pcount"].ToString());
}
count = count + 1;
Label1.Text = "Visitor Count is:" + count.ToString();
Application["pcount"] = count.ToString();
}
}
}
Output:

You might also like