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>";
}
}
}
Saturday, 27 July 2013
Connectivity with Sql Server 2008 using Web Services………………………..
Connectivity
with Sql Server 2008 using Web Services………………………..
First we create WebMethod in Web
Service which web service name is WebService.cs and Method name is Login…………………………………….
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
/// <summary>
/// Summary description for WebService
/// </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 WebService : System.Web.Services.WebService {
public
WebService () {
//Uncomment
the following line if using designed components
//InitializeComponent();
}
//create WebMethod for
Login………………………………….
[WebMethod]
public Boolean
Login (string username,string
password)
{
SqlConnection
con = new SqlConnection();
con.ConnectionString="Data Source=KUSH-PC\\KUSH;Initial
Catalog=test;Integrated Security=True";
SqlCommand
cmd=new SqlCommand();
cmd.CommandText="select 1 from logintable where username='"+username+"' and Password='"+password+"'";
cmd.Connection=con;
con.Open();
SqlDataReader
dr = cmd.ExecuteReader();
if
(dr.HasRows)
{
return
true;
}
else
{
return
false;
}
}
//create WebMethod for
Details………………………………….
[WebMethod]
public DataSet Details(string
tbname)
{
SqlConnection
con = new SqlConnection("Data Source=KUSH-PC\\KUSH;Initial
Catalog=test;Integrated Security=True");
SqlCommand com = new
SqlCommand("select
* from " + tbname, con);
SqlDataAdapter
da = new SqlDataAdapter(com);
DataSet
ds = new DataSet();
da.Fill(ds);
return
ds;
}
We take another
Website and call above
services in this……………………..
//code for login ……………………………..
protected void BtnLogin_Click(object
sender, EventArgs e)
{
abc.WebService
ww = new abc.WebService();
if
(ww.Login(txtusername.Text, txtpassword.Text))
{
Response.Redirect("WelCome.aspx");
}
else
{
//string
msg = @"<script>alert('username or password is not
match')</script>";
//Page.ClientScript.RegisterStartupScript(this.GetType(),
"message", msg, true);
Or
//
For this code …. Script Manager must
exist in web page
string
msg = @"alert('userid or password not
match');";
ScriptManager.RegisterStartupScript(this, this.GetType(),
"message", msg, true);
}
//code for gettable
from database ……………………………..
protected void
btngettable_Click(object sender, EventArgs e)
{
try
{
abc.WebService
ww = new abc.WebService();
DataSet
ds = ww.Details(txttbname.Text);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
lblmessage.ForeColor = Color.Red;
lblmessage.Text = ex.Message;
}
}