0% found this document useful (0 votes)
30 views30 pages

Web Services & WCF Services (AIML)

The document discusses creating web services and WCF services to perform operations like arithmetic, data insertion and selection. It includes code examples of creating services to add numbers, manage a student database, and demonstrate other common service tasks.

Uploaded by

s1062230126
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views30 pages

Web Services & WCF Services (AIML)

The document discusses creating web services and WCF services to perform operations like arithmetic, data insertion and selection. It includes code examples of creating services to add numbers, manage a student database, and demonstrate other common service tasks.

Uploaded by

s1062230126
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Assignment - 3

Web Services & WCF Services

1. Write a program to create a web service which performs arithmetic


operations and also create a client to call that web service.

WebService1.asmx

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

namespace webService
{
/// <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 int Add(int a, int b)
{ return a + b; }

[WebMethod]
public int Sub (int a, int b)
{ return a + b; }

[WebMethod]
public int Multi (int a, int b)
{ return a + b; }
[WebMethod]
public int divi (int a, int b)
{ return a + b; }

}
}

https://localhost:44330/WebService1.asmx

WebService1.asmx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication13
{
public partial class WebForm1 : System.Web.UI.Page
{
localhost.WebService1 service1 = new localhost.WebService1();
protected void Page_Load(object sender, EventArgs e)
{

protected void Button1_Click(object sender, EventArgs e)


{
int result = service1.Add(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text));
Label1.Text = result.ToString();
}

protected void Button2_Click(object sender, EventArgs e)


{
int result = service1.Sub(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text));
Label1.Text = result.ToString();
}

protected void Button3_Click(object sender, EventArgs e)


{
int result = service1.Multi(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text));
Label1.Text = result.ToString();
}

protected void Button4_Click(object sender, EventArgs e)


{
int result = service1.divi(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text));
Label1.Text = result.ToString();
}
}
}
2. Create a web service that demonstrates insert, select and searching
functionality.

WebService1.asmx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Remoting.Messaging;
using System.Diagnostics;

namespace Webserver2
{
/// <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
{
SqlConnection con;

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
public void Connect()
{
con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;
AttachDbFilename=C:\Users\timscdr\source\repos\Webserver2\Webserver2\App_Data\
emp.mdf;
Integrated Security=True");
con.Open();
}
[WebMethod]
public int InsterData(string name, string salary)
{
Connect();
SqlCommand cmd = new SqlCommand("insert into EMP_info (name, salary)
values('" + name + "', " + salary + ")", con);
return cmd.ExecuteNonQuery();
}
[WebMethod]
public DataSet select()
{
DataSet ds = new DataSet();
Connect();
SqlDataAdapter da = new SqlDataAdapter("Select * from EMP_info", con);
da.Fill(ds);
return ds;
}

[WebMethod]
public DataSet SearchRecord(string searchdata)

{
Connect();
SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from EMP_info
where name like '%" + searchdata+"%'", con);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
return ds;
}
}
}
WebService1.asmx

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

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

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)


{

protected void TextBox2_TextChanged(object sender, EventArgs e)


{

}
}
}
3. Write a program to create a WCF Service which adds two numbers and also
create a client to call that service.

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;

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

[OperationContract]
int Add(int a, int b);

[OperationContract]
int Sub(int a, int b);

[OperationContract]
int Divi(int a, int b);

[OperationContract]
int Multi(int a, int b);
}

// Use a data contract as illustrated in the sample below to add composite types to
service operations.
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";

[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}

[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
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;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the
class name "Service" in code, svc and config file together.
public class Service : IService
{
public int Add(int a, int b)
{
return a+b;
}
public int Sub(int a, int b)
{
return a - b;
}
public int Divi(int a, int b)
{
return a/b;
}
public int Multi(int a, int b)
{
return a * b;
}

public CompositeType GetDataUsingDataContract(CompositeType composite)


{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}

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 WebApplication16
{
public partial class WebForm1 : System.Web.UI.Page
{
ServiceReference1.ServiceClient obj = new ServiceReference1.ServiceClient();
protected void Page_Load(object sender, EventArgs e)
{

protected void Button1_Click(object sender, EventArgs e)


{
int res = obj.Add(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text));
Response.Write(res);
}

protected void Button2_Click(object sender, EventArgs e)


{
int res = obj.Sub(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text));
Response.Write(res);
}

protected void Button3_Click(object sender, EventArgs e)


{
int res = obj.Divi(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text));
Response.Write(res);
}

protected void Button4_Click(object sender, EventArgs e)


{
int res = obj.Multi(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text));
Response.Write(res);
}
}
}
Add OUTPUT
Sub OUTPUT

Divi OUTPUT
Multi OUTPUT
4. Create a WCF service which is used to display student information and
also create a client that displays the same information.

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

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

[OperationContract]
void DoWork();

[OperationContract]
int IntsertData(string name, int proce);

[OperationContract]
DataSet SelectRecode();

[OperationContract]
DataSet SearchData(string name);
}

// Use a data contract as illustrated in the sample below to add composite types to
service operations.
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}

[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}

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

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the
class name "Service" in code, svc and config file together.
public class Service : IService
{
public void DoWork()
{
throw new NotImplementedException();
}

public string GetData(int value)


{
return string.Format("You entered: {0}", value);
}

public CompositeType GetDataUsingDataContract(CompositeType composite)


{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
SqlConnection con;
public void Connect()
{
con = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial
Catalog=EMPLOYEE;Integrated Security=True;");
con.Open();
}

public int IntsertData(string name, int price)


{
Connect();
SqlCommand cmd = new SqlCommand("insert into emp (Name, Price) values ('" +
name + "','" + price + "')", con);
return cmd.ExecuteNonQuery();
}

public DataSet SelectRecode()


{
Connect ();
SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from emp", con);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
return ds;

}
public DataSet SearchData(string name)
{
Connect();
SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from emp where
Name like '%" + name + "%'", con);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
return ds;
}

}
WebForm1.aspx.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication17.ServiceReference1;

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

protected void Page_Load(object sender, EventArgs e)


{

protected void Button1_Click(object sender, EventArgs e)


{
ServiceReference5.ServiceClient serviceClient = new
ServiceReference5.ServiceClient();
int result = serviceClient.IntsertData(TextBox1.Text,int.Parse(TextBox2.Text));
DataSet dataSet = new DataSet();
dataSet = serviceClient.SelectRecode();
GridView1.DataSource = dataSet;
GridView1.DataBind();

protected void Button2_Click(object sender, EventArgs e)


{
ServiceReference5.ServiceClient serviceClient = new
ServiceReference5.ServiceClient();
DataSet dataSet = new DataSet();
dataSet = serviceClient.SearchData(TextBox1.Text);
GridView1.DataSource = dataSet;
GridView1.DataBind();
}
}
}
5. Create a MVC application that displays data from the model.
6. Write an application that accepts data from one page and displays it in another
page using MVC (Passing data between views).
7. Write a program that shows crud operation using MVC.
Index.cshtml
@model WebApplication3.Models.emp

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm("Showemp", "emp", FormMethod.Post))


{
<div>Enter Id @Html.TextBox("empid")</div>
<div>
Enter Name @Html.TextBox("empname")

</div>
<input type="submit" value="Display" />

Showemp.cshtml
@model WebApplication3.Models.emp

@{
ViewBag.Title = "Showemp";
}

<h2>Showemp</h2>
@Model.Id
@Model.Name

empController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication3.Models;

namespace WebApplication3.Controllers
{
public class empController : Controller
{
// GET: emp
public ActionResult Index()
{
return View();
}
public ActionResult Showemp()
{
emp emp1 = new emp();
emp1.Id = int.Parse(Request.Form["empid"]);
emp1.Name = Request.Form["empname"];
return View(emp1);
}
}
}

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

namespace WebApplication3.Models
{
public class emp
{
public int Id { get; set; }
public string Name { get; set; }

}
}
OUTPUT

You might also like