0% found this document useful (0 votes)
44 views4 pages

Connectivity: Using Using Using Using Using Using Using Using Using

This document describes how to create a web service in .NET using data connectivity to perform CRUD operations on a SQL database. It defines a web service class with methods to update, delete, insert and find/view data in a database table. The consuming code shows how to call these web service methods to perform the CRUD operations and display the results in textboxes on an ASP.NET web form.

Uploaded by

koilraj1991
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views4 pages

Connectivity: Using Using Using Using Using Using Using Using Using

This document describes how to create a web service in .NET using data connectivity to perform CRUD operations on a SQL database. It defines a web service class with methods to update, delete, insert and find/view data in a database table. The consuming code shows how to call these web service methods to perform the CRUD operations and display the results in textboxes on an ASP.NET web form.

Uploaded by

koilraj1991
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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