AWT Assignment 3
AWT Assignment 3
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();*/
}
}
}
OutPut:
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();
}
clear();
Response.Write("Record inserted sucessfully");
GridView1.DataBind();
}
void clear()
{
TextBox1.Text = TextBox2.Text = TextBox3.Text = " ";
Button1.Text = "Save";
model1.Emp_ID = 0;
}
Update:
Q3)Design Web Application to produce and Consume a WCF Service for
calculator.
Aim:
Objective:
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)
{
WebForm1.aspx
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:
Objective:
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);
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
{
}
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:
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:
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);
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)
{
Output:
Q7)Write a program to demonstrate the ViewState.
Aim:
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)
{
Aim:
Objective:
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)
{
Output:
9.Write a program to demonstrate the query string.
Aim:
Objective:
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)
{
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();
}
}
}
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:
Objective:
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);
// 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:
Objective:
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: