You are on page 1of 4

CREATING WEBSERVICE IN .

NET USING DATA CONNECTIVITY


using System; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Linq; using System.Data.Common; using System.Data.SqlClient; using System.Data; [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 Service : System.Web.Services.WebService { public Service() { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] public void update(String old, String new1) { SqlConnection con = new SqlConnection("Data Source=(local);integrated security=SSPI;initial catalog=Northwind"); con.Open(); SqlDataAdapter sda = new SqlDataAdapter("select * from lib1", con); String sql = String.Format("update lib1 set title='{1}' where title='{0}'", old, new1); SqlCommand cmd = new SqlCommand(sql, con); int rowset = cmd.ExecuteNonQuery(); con.Close(); } [WebMethod] public void delete(String title) { SqlConnection con = new SqlConnection("Data Source=(local);integrated security=SSPI;initial catalog=Northwind"); con.Open();

SqlDataAdapter sda = new SqlDataAdapter("select * from lib1", con); String sql = String.Format("delete from lib1 where title='{0}'", title); SqlCommand cmd = new SqlCommand(sql, con); int rowset = cmd.ExecuteNonQuery(); con.Close(); } [WebMethod] public void insert(int bookid, string bookname, string author, string title) { SqlConnection con = new SqlConnection("Data Source=(local);integrated security=SSPI;initial catalog=Northwind"); con.Open(); SqlDataAdapter sda = new SqlDataAdapter("select * from lib1", con); String sql = String.Format("insert into lib1 values({0},'{1}','{2}','{3}')", bookid, bookname, author, title); SqlCommand command = new SqlCommand(sql, con); int rowset = command.ExecuteNonQuery(); con.Close(); } [WebMethod] public DataSet find(int i) { SqlConnection con = new SqlConnection("Data Source=(local);integrated security=SSPI;initial catalog=Northwind"); SqlDataAdapter sda = new SqlDataAdapter("select * from lib1 where bookid='" + i + "'", con); DataSet ds= new DataSet(); con.Open(); sda.Fill(ds, "lib1"); return ds; } [WebMethod] public DataSet view() { SqlConnection con = new SqlConnection("Data Source=(local);integrated security=SSPI;initial catalog=Northwind"); SqlDataAdapter sda = new SqlDataAdapter("select * from lib1", con); DataSet ds = new DataSet(); con.Open(); sda.Fill(ds, "lib1"); return ds; } }

CONSUMING WEB SERVICE IN .NET USING DATA CONNECTIVITY:


using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.Common; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { localhost.Service ss = new localhost.Service(); protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { String i = TextBox1.Text; String j = TextBox2.Text; ss.update(i, j); } protected void Button2_Click(object sender, EventArgs e) { String i = TextBox1.Text; ss.delete(i); } protected void Button3_Click(object sender, EventArgs e) { int bookid = Int32.Parse(TextBox3.Text); string bookname = TextBox4.Text; string author = TextBox5.Text; string title = TextBox6.Text; ss.insert(bookid,bookname,author,title); } protected void Button4_Click(object sender, EventArgs e)

{ DataSet ds = ss.view(); foreach (DataRow r in ds.Tables["lib1"].Rows) { TextBox3.Text = r[0].ToString(); TextBox4.Text = r[1].ToString(); TextBox5.Text = r[2].ToString(); TextBox6.Text = r[3].ToString(); } } protected void Button5_Click(object sender, EventArgs e) { int i = Int32.Parse(TextBox3.Text); DataSet ds = ss.find(i); foreach (DataRow r in ds.Tables["lib1"].Rows) { TextBox3.Text = r[0].ToString(); TextBox4.Text = r[1].ToString(); TextBox5.Text = r[2].ToString(); TextBox6.Text = r[3].ToString(); } } }

You might also like