WCF Tutorial with sample program | Insert data to database using WCF Service and aspx page

In this example i am showing how to insert data using WCF service with aspx page.

Before reading this article please go through Difference between Webservices and WCF.

VS 2008/2010 --> file menu --> new --> website --> wcf service --> give name "RegistrationDB"

Here IService is the interface having InsertData method. this method we will implement in the Service class. We have to add some settings in the web.config file as shown below.


Below 7 steps will give is enough for sending data into database using WCF services.


Step 1:  service contract, operation contract, Data contract and data member. 
[ServiceContract]
public interface IService
{
    [OperationContract]
    string InsertData(InputData input);
}

// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class InputData
{
    [DataMember]
    public string strUserName { get; set; }

    [DataMember]
    public string strPassword { get; set; }

    [DataMember]
    public string strConfirmPassword { get; set; }

    [DataMember]
    public string strFirstName { get; set; }

    [DataMember]
    public string strLastName { get; set; }
}

step 2:  IService implementation in the service class
public class Service : IService
{

    #region IService Members

    public string InsertData(InputData input)
    {
        try
        {
            string strQuery = "insert into UserDetails values('" + input.strUserName + "','" + input.strPassword + "','" + input.strFirstName + "','" + input.strLastName + "')";
            SqlConnection con = new SqlConnection("Server=localhost;Database=Rajesh;Trusted_Connection=True;");
            con.Open();

            SqlCommand cmd = new SqlCommand(strQuery, con);
            cmd.ExecuteNonQuery();

            return "0";
        }
        catch(Exception ex)
        {
            return ex.Message;
        }
    }

    #endregion
}
Step 3: Web.config settings int the Service
 <system.serviceModel>
  <services>
   <service name="DBService">
    <endpoint address="" binding="wsHttpBinding" contract="IService">
     <identity>
      <dns value="localhost"/>
     </identity>
    </endpoint>
   </service>
  </services>



step 4: Host above service in the IIS.


step 5: In this aspx page we have registration form elements.
Adding service through cSharp


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnClear").click(function () {
                $("#txtUserName").val("");
                $("#txtPassword").val("");
                $("#txtConfirmPassword").val("");
                $("#txtFirstName").val("");
                $("#txtLastName").val("");
                return false;
            });
        });
    </script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table style="width: 100%;">
            <tr>
                <td>
                    UserName
                </td>
                <td>
                    <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td>
                    Password
                </td>
                <td>
                    <asp:TextBox ID="txtPassword" runat="server" EnableTheming="True" TextMode="Password"></asp:TextBox>
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td>
                    Confirm Password
                </td>
                <td>
                    <asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td>
                    FirstName
                </td>
                <td>
                    <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td>
                    Last Name
                </td>
                <td>
                    <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
                        onclick="btnSubmit_Click" />
                </td>
                <td>
                    <asp:Button ID="btnClear" runat="server" OnClientClick="return;" Text="Clear" />
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td>
                    <div id="statusMessage">
                    
                    </div></td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
Step 6: Solution explorer(right click) --> Add Service Reference -->  http://localhost/RegistrationDB/Service.svc -->Add

Step 7: write below csharp code and run the application. 

 protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            ServiceClient srv = new ServiceClient();
            InputData input = new InputData();
            input.strUserName = txtUserName.Text;
            input.strPassword = txtPassword.Text;
            input.strConfirmPassword = txtConfirmPassword.Text;
            input.strFirstName = txtFirstName.Text;
            input.strLastName = txtLastName.Text;

            string output = srv.InsertData(input);
            if (output == "0")
            {
                Response.Write("inserted Successful");
            }
            else
            {
                Response.Write("inserted unsuccessful");
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

If we run above application we may get Login failed for user 'IIS APPPOOL\User'


Please share your valuable points on this post.. I will enhance this post. 
Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar