articles

Home / DeveloperSection / Articles / Inserting Records in Table by Using Web Services

Inserting Records in Table by Using Web Services

Anonymous User16214 27-Jan-2011

Web Services are application components which communicate system to system by using open protocols such as http. Web services worked with the help of two platforms. One is by using xml language which provides a way to communicate between different platforms and languages and still express complex functions and objects and another one is http (Hyper Text Transfer Protocol) which is most usable internet protocol.

In this demonstration am showing you a simple web application which use the concept of web services for inserting record of student in a database. For this purpose I have created a web service named Service1.asmx and a web application named Web Service used.

Note: Always remember the web services that you have created have an extension .asmx and for .cs file it has extension .asmx.cs.

Creating a web service

For creating web service follow the following steps.

  •    Open the visual studio 2010.
  •    Go to file menu then followed by New Web Site menu option then click it.
  •    Then New Web Site dialog box is open. Then go to .net framework template and then select .net framework version 3.5.
  •    Then from template section select ASP.NET Web services.
  •    Then click Ok button.

Inserting Records in Table by Using Web Services

Screen shot of the New Web Site dialog box.

After following these steps a file is opened which have a name Service1.asmx.cs and which is looked like these steps.

Inserting Records in Table by Using Web Services

I am going to create a web service which inserts record in a database that’s why use a name space named System.Data.SqlClient firstly. This namespace contains all the necessary classes and interfaces which you need to work with database.

privateSqlConnection createSqlConnection()//This method returns a SqlConnection class //object.
{
            SqlConnection con = newSqlConnection("connection string of your datasource”);
            return con;
}
//This method accept SqlConnection class object as a parameter abd close that connection
privatevoid closeConnection(SqlConnection con)
{
            try
            {
                if (con != null)
                {
                    con.Close();
                }
            }
            catch (Exception e){            
            }
 }
//this attribute tell IIS that this method can be called by other web application over internet.
        //If you want to make any method which is invoked by other web application over internet then write this
        //attribute before the method like this
        [WebMethod]
        publicint insertRecord(string sid, string sname, int sage, string scity)
        {
            int rows = 0;
            SqlConnection tempConnection = createSqlConnection();
            tempConnection.Open();
            string query = "insert into Student values('" + sid + "','" + sname + "'," + sage + ",'" + scity + "')";
            SqlCommand cmd = newSqlCommand(query, tempConnection);
            rows = cmd.ExecuteNonQuery();
            closeConnection(tempConnection);
            return rows;
        }

 

Now after performing these steps you have to build your web service so that dll of that service is created.

After building your web service execute that web service and copy the address which displayed in address bar of browser.

Inserting Records in Table by Using Web Services

When you execute your web service it will display all the methods that are marked by [WebMethod].

After creating web service create a web application which uses this web service.

Design View of Web Application That Uses The Web Service

Inserting Records in Table by Using Web Services

Source Code of User Interface
<body>
    <formid="form1"runat="server">
    <div>
        Student Id :&nbsp;&nbsp;<asp:TextBoxID="txtStudentId"runat="server"/><br/>
       Student Name :&nbsp;&nbsp;<asp:TextBoxID="txtStudentName"runat="server"/><br/>
       Student Age :&nbsp;&nbsp;<asp:TextBoxID="txtStudentAge"runat="server"/><br/>
       Student City :&nbsp;&nbsp;<asp:TextBoxID="txtStudentCity"runat="server"/><br/>
        <asp:ButtonID="btnInsert"Text="Insert Record"runat="server"
            style="margin-left:50px"onclick="btnInsert_Click"/><br/><br/>
        <asp:ButtonID="btnDisplay"Text="Display Record"runat="server"
            style="margin-left:50px"onclick="btnDisplay_Click"/><br/><br/>
    </div>
    </form>
</body>

 

After designing your web application that uses the web service add reference of  web service in your web application by following these steps.

  •    Open the solution explorer of your web application.
  •    Right click on the solution explorer and then click add Web reference option.
  •    A new page open in which you add the web reference. The screen shot of that page is as follows.

Inserting Records in Table by Using Web Services

  • In the URL text box paste the web reference of your web services that you have copied when executing your web services.
  • After entering the url of web reference give a web reference name. By default it is localhost you can also change the name of web reference.
  • Then click Add Reference Button.

By following these steps a web services is added in the solution folder of your project. You noticed that after performing these steps a new folder is created in your solution explorer named App_WebReferences which contains web references. After performing these steps click Insert Record button and type following code.

protectedvoid btnInsert_Click(object sender,  EventArgs e)
    {
        InsertRecord.Service1 ins = new InsertRecord.Service1();//Create an object of Service class.
        //Here InsertRecord is named of Web Service and Service1 is the name of the class.
        int rows = ins.insertRecord(txtStudentId.Text, txtStudentName.Text, Convert.ToInt32(txtStudentAge.Text), txtStudentCity.Text);//call the insert record method
        if (rows > 0)
        {
            Response.Write("<script>alert('Record Inserted Successfully.')</script>");//Show the success message.
            txtStudentId.Text = ""; txtStudentName.Text = ""; txtStudentAge.Text = ""; txtStudentCity.Text = "";//Clear all the text box.
        }
    }


Output Of This Demonstration


Inserting Records in Table by Using Web Services

Inserting Records in Table by Using Web Services

 

 

 


Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By