Thursday, 2 August 2012

Web Services in ASP.NET

A Web Service is programmable application logic accessible via standard Web protocols. One of these Web protocols is the Simple Object Access Protocol (SOAP). SOAP is a W3C submitted note (as of May 2000) that uses standards based technologies (XML for data description and HTTP for transport) to encode and transmit application data.
Consumers of a Web Service do not need to know anything about the platform, object model, or programming language used to implement the service; they only need to understand how to send and receive SOAP messages (HTTP and XML).

Try simple code :

ASPX :->


<html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server"><title></title></
head><
body><form id="form1" runat="server"><div><asp:GridView ID="GridView1" runat="server"><Columns><asp:BoundField DataField="id" HeaderText="ID" /><asp:BoundField DataField="f_name" HeaderText="Firts name" /><asp:BoundField DataField="adds" HeaderText="ADDS" /></Columns></asp:GridView><asp:Button ID="Button1" runat="server" Text="Click" onclick="Button1_Click" /></div></form></
body></
html>

CS ->

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using webServiceTrial;namespace webServiceTrial{

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

protected void Button1_Click(object sender, EventArgs e){
webServiceTrial.
WebService1 wrr = new webServiceTrial.WebService1();GridView1.DataSource = wrr.getDetails();
GridView1.DataBind();
}
}
}


WebService asmx :-?


using System.Web.Services;using System.Data;using System.Data.Sql;using System.Data.SqlClient;namespace webServiceTrial{

/// <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 DataSet getDetails(){

SqlConnection conn;
SqlDataAdapter myDataAdapter;
DataSet myDataSet;
const string cmdString = "Select * From Info";conn =
new SqlConnection("Data Source=ADS;Initial Catalog=TestApp;Persist Security Info=True;User ID=Nihar.Kulkarni;Password=bflmcd");myDataAdapter =
new SqlDataAdapter(cmdString, conn);myDataSet =
new DataSet();myDataAdapter.Fill(myDataSet,
"Info");
return myDataSet;
}
}
}

No comments:

Post a Comment