Monday 29 July 2013

How call Class’s Property, List Property using Web Service

How call Class’s Property, List Property  using Web Service


First we create a web services which  name MyWebService.cs  is   in Website and call its another  Website……………….

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

/// <summary>
/// Summary description for MyWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 MyWebService : System.Web.Services.WebService {

public MyWebService () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

public class Student
{

private int RollNo;

public int RollNo1
{
get { return RollNo; }
set { RollNo = value; }
}
private string Name;

public string Name1
{
get { return Name; }
set { Name = value; }
}




}
[WebMethod]
public Student customtype()
{
Student st = new Student();
Student st1 = new Student();
st.RollNo1 = 20;
st.Name1 = "Naved Khan";
st1.RollNo1 = 34;
st1.Name1 = "Somesh Katiyar";
return st;
}

[WebMethod]
public List<Student> CustomerType1()
{
List<Student> ll = new List<Student>();
Student st = new Student();
Student st1 = new Student();
st.RollNo1 = 23;
st.Name1 = "Ajit Sir";
st1.RollNo1 = 25;
st1.Name1 = "Ramesh Singh";
ll.Add(st);
ll.Add(st1);
return ll;

}
[WebMethod]
public List<Student> StudentType()
{
List<Student> ll1 = new List<Student>
{
new Student {RollNo1=10,Name1="Vinay Singh"},
new Student {RollNo1=11,Name1="Kush Tiwari"},
new Student {RollNo1=12,Name1="Manoj Yadav"},
new Student {RollNo1=26,Name1="Naresh Singh"}
};
return ll1;

}
}


Take  another Website  and  call above Web Services ……………………………

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

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

}
localhost.MyWebService mws = new localhost.MyWebService();

// how to call Class Property...............................
protected void Button1_Click(object sender, EventArgs e)
{

localhost.Student s = mws.customtype();
lblresult1.Text = "RollNo is  " + s.RollNo1.ToString() + "   " + "Name is   " + s.Name1.ToString();
}
// how to call List Property....................................
protected void Button2_Click(object sender, EventArgs e)
{
List<localhost.Student> s = mws.CustomerType1().ToList();
foreach (var a in s)
{
lblresult2.Text += "RollNo-" + "  " + a.RollNo1 + "  " + "Name-" + "  " + a.Name1 + "<br>";
}

}
// how to call List Property....................................
protected void Button3_Click(object sender, EventArgs e)
{

List<localhost.Student> s = mws.StudentType().ToList();
foreach (var a in s)
{
lblresult2.Text += "RollNo-" + "  " + a.RollNo1 + "  " + "Name-" + "  " + a.Name1 + "<br>";
}

}
}


0 comments:

Post a Comment