Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
Search :       Advanced Search �
Home

Author Rank :
Page Views :
Downloads : 0
Rating :
 Rate it
Level :
Become a Sponsor
Tags


Introduction

In this article I am going to make a XML Web service that inserts data into a Default table of the application. For the basics of XML Web Services read my last article. Click Here

First go through SQL Server and make a table;

The following snapshot shows the process.

img1.gif

Database- akshay

Table Name-member

Creating XML Web Service in .Net

Here is sample code which I use to create and consume ASP.NET Web Service:

Step 1 : Create the ASP.NET Web Service Source File

Open Visual Studio 2010 and create  a new web site.->Select .Net Framework 3.5. ->Select ASP.NET Web Service page -> Then, you have to give the name of your service. In this example I am giving it the name "mywebservice1". Then Click the ok Button. A screen-shot of this activity is shown below:

WEBSRVICE.gif

Step 2 : Click on the "OK" Button. You will see the following window:

img3.gif

Here (in the above figure), you will note that there is a predefined method "HelloWorld" which returns the string "Hello World". You can use your own method and can perform other operations. Here I made the simple method "SaveData" which returns a integer value.

Service.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
namespace MyWebService1
{
    /// <summary>
    /// Summary description for Service1
    /// </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 Service1 : System.Web.Services.WebService
    {
        SqlConnection con;
        SqlCommand cmd;
        [WebMethod]
        public int SaveData(int pid, string pna, string ad)
        {
            con = new SqlConnection(@"Data Source=.;Initial Catalog=akshay;Persist Security Info=True;User
ID=sa;pwd=wintellect"
);
            con.Open();
            cmd = new SqlCommand("insert into member values(" + pid + ",'" + pna + "','" + ad + "')", con);
            int temp = cmd.ExecuteNonQuery();
            return temp;
        }
    }
}

Step 3 : Build Web Service and Run the Web Service for testing by pressing F5 function key.

img4.gif

Copy the url of this web service for further use.

Step 5 : Click on SaveData Button to test the web service.

img5.gif

Enter the value of pid, pna and pw to test the web service where [pid=id,pna=name and pw=addres ] is in the database table.

img6.gif

By pressing the "Invoke" button a XML file is generated.

img7.gif

The '1' in the response to our data is inserted in specific  data base table (here "member") see here.

img8.gif

Now our web service is ready to use. We just need to create a new web site to consume the web service.

Example of Testing Web Service in .Net

Step 6 : Create a Test Web Site by File > New > Web Site > ASP.Net Web Site.

WEBSRVICE1.gif

Named the web site, for example here I have chosen name "MyTest" and click on 'ok' button.

Step 7 : Right Click Solution Explorer and Choose "Add Web Reference":

img10.gif

Step 8 : Past the url of the web service and click on "Green arrow" button and then "Add reference".

img11.gif

Step 9 : Now your web service is ready to use; in the Solution Explorer you will see:

img12.gif

Step 10 : Go to the design of the Default.aspx page; drag and drop three TextBoxes, one button and a Lable.

img13.gif

Rename the Button as 'saved'.

step 11 : Go to Default.cs page and on the button click event use the following code.

    protected void Button1_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(TextBox1.Text);
        string na = TextBox2.Text;
        string ad = TextBox3.Text;
        localhost.Service1 myservice = new localhost.Service1(); // you need to create the object of the web
service
        int roweffected=myservice.SaveData(id,na,ad);
        if (roweffected == 1)
        {
            Label1.Text = "Record is saved";
        }
        else

        {
            Label1.Text = "sorry record is not saved try again";

        }
    }
}

Step 12 : Pressing the F5 function key to run the website, you will see:

img14.gif

Enter the value of textbox

img15.gif

Press the saved button.

img16.gif

The record is saved; you can check it from your database.

img17.gif

 

Resources

 [Top] Rate this article
 
 About the author
 
Author
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
 Comments

 � 2025  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.