Thursday, May 15, 2014

Insert Data using WCF Into Database in ASP.NET C#

In this article I will show you how to insert data into database using Windows Communication Foundation(W.C.F.). 

For that first you need to create a WCF Service Application.

Now to insert data into database you need to get help of two things.

  • OperationContract (Add services operation)
  • DataConntract (add type to services operation)
Now in the IService1.cs create your own method named InsertData(UserData user)

string InsertUserDetails(UserData user);

Now add the UserData class with its members  at IService1.cs

public class UserData
        string password = string.Empty;
        string country = string.Empty;
        string email = string.Empty;
        public string Password
            get { return password; }
            set { password = value; }
        public string Country
            get { return country; }
            set { country = value; }
        public string Email
            get { return email; }
            set { email = value; }

Now add the following code in the Service1.svc

public string InsertUserDetails(UserData userInfo)
            string Message;
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=WCF_Demo;Trusted_Connection=true");
            SqlCommand cmd = new SqlCommand("insert into RegistrationTable(Password,Country,Email) values(@Password,@Country,@Email)", con);
            cmd.Parameters.AddWithValue("@Password", userInfo.Password);
            cmd.Parameters.AddWithValue("@Country", userInfo.Country);
            cmd.Parameters.AddWithValue("@Email", userInfo.Email);
            int result = cmd.ExecuteNonQuery();
            if (result == 1)
                Message ="successful.";
                Message = "Sorry Unsuccessful.";
            return Message;

Now run the Service1.svc of the project. and will get an url in the browser.

Now create another project(Website) and add a new webform.

Now add a web reference to the project by right clicking on the project name in solution explorer.

And after adding this reference you will find that in your solution explorer.

Now add some textbox and a button to make a registration form. Something looking like this one.

Now in the code behind add the code following.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WCF_Insert.ServiceReference1;
namespace WCF_Insert
    public partial class WebForm1 : System.Web.UI.Page
        ServiceReference1.Service1Client objServiceClientobjService = new ServiceReference1.Service1Client();
        protected void Page_Load(object sender, EventArgs e)
        protected void Button1_Click(object sender, EventArgs e)
            UserDetails userInfo = new UserDetails();
            userInfo.Password = TextBoxPassword.Text;
            userInfo.Country = TextBoxCountry.Text;
            userInfo.Email = TextBoxEmail.Text;
            string result = objServiceClientobjService.InsertUserDetails(userInfo);
            LabelMessage.Text = result;

Before closing the application create a table named RegistrationTable with coloumns of..

CREATE TABLE [dbo].[RegistrationTable]
      [Password] [varchar](20) NOT NULL,
      [Country] [varchar](100) NOT NULL,
      [Email] [varchar](200) NOT NULL

Now its over. Run your project and enjoy the WCF.


Post a Comment

Popular Posts