Today, I have provided an article showing you how to insert and display data in a DataGridView control using a WCF service from C# code. To insert and display data in a DataGridView control using a WCF service, we must do the following 3 things:
  1. Create Database Table
  2. Create WCF Service
  3. Create Windows Forms Application
In the first step we will create a table in SQL Server; after that we create a simple function to insert and display data in a DataGridView control using a WCF service. In a web application, add a reference for the service to insert and display data in the DataGridView control. Let's take a look at a practical example. The example application is developed in Visual Studio 2010 and SQL Server 2008.

Step 1: Creating Database Table

  1. Database name: Registration
  2. Database table name: RegistrationTable
RegistrationTable Table
image1.jpg

Step 2: Creating WCF Service

Now you have to create a WCF Service:
image2.jpg
Now click on the project and select WCF Service Application and provide a name for the service:
image3.jpg
Now click on the Ok Button. Then you will get 3 files in Solution Explorer.
  1. IService.cs
  2. Service.svc
  3. Service.svc.cs
The following image shows the following files:
image4.jpg
For inserting data into the database you need to write the following code in the IService1.cs file which contains the two sections:
  1. OperationContract
  2. DataContract
The OperationContract section is used to add service operations and a DataContract is used to add types to the service operations.
Iservice1.cs File
Now we create a function in the OperationContract section of the Iservice1.cs file:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.ServiceModel;
  6. using System.ServiceModel.Web;
  7. using System.Text;
  8. using System.Data.SqlClient;
  9. using System.Data;
  10. namespace WCFServiceForInsert
  11. {
  12. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
  13. [ServiceContract]
  14. public interface IService1
  15. {
  16. [OperationContract]
  17. string InsertUserDetails(UserDetails userInfo);
  18. [OperationContract]
  19. DataSet SelectUserDetails();
  20. }
  21. // Use a data contract as illustrated in the sample below to add composite types to service operations.
  22. [DataContract]
  23. public class UserDetails
  24. {
  25. int userid;
  26. string username;
  27. string password;
  28. string country;
  29. string email;
  30. [DataMember]
  31. public int UserID
  32. {
  33. get { return userid; }
  34. set { userid = value; }
  35. }
  36. [DataMember]
  37. public string UserName
  38. {
  39. get { return username; }
  40. set { username = value; }
  41. }
  42. [DataMember]
  43. public string Password
  44. {
  45. get { return password; }
  46. set { password = value; }
  47. }
  48. [DataMember]
  49. public string Country
  50. {
  51. get { return country; }
  52. set { country = value; }
  53. }
  54. [DataMember]
  55. public string Email
  56. {
  57. get { return email; }
  58. set { email = value; }
  59. }
  60. }
  61. }
Service.svc.cs File
In this file we define the definition of the function InsertUserDetails(UserDetails userInfo).
And replace the code with the following:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.ServiceModel;
  6. using System.ServiceModel.Web;
  7. using System.Text;
  8. using System.Data.SqlClient;
  9. using System.Data;
  10. namespace WCFServiceForInsert
  11. {
  12. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
  13. public class Service1 : IService1
  14. {
  15. public DataSet SelectUserDetails()
  16. {
  17. SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=registration;User ID=sa;Password=wintellect");
  18. con.Open();
  19. SqlCommand cmd = new SqlCommand("Select * from RegistrationTable", con);
  20. SqlDataAdapter sda = new SqlDataAdapter(cmd);
  21. DataSet ds = new DataSet();
  22. sda.Fill(ds);
  23. cmd.ExecuteNonQuery();
  24. con.Close();
  25. return ds;
  26. }
  27. public string InsertUserDetails(UserDetails userInfo)
  28. {
  29. string Message;
  30. SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=registration;User ID=sa;Password=wintellect");
  31. con.Open();
  32. SqlCommand cmd = new SqlCommand("insert into RegistrationTable(UserName,Password,Country,Email) values(@UserName,@Password,@Country,@Email)", con);
  33. cmd.Parameters.AddWithValue("@UserName", userInfo.UserName);
  34. cmd.Parameters.AddWithValue("@Password", userInfo.Password);
  35. cmd.Parameters.AddWithValue("@Country", userInfo.Country);
  36. cmd.Parameters.AddWithValue("@Email", userInfo.Email);
  37. int result = cmd.ExecuteNonQuery();
  38. if (result == 1)
  39. {
  40. Message = userInfo.UserName + " Details inserted successfully";
  41. }
  42. else
  43. {
  44. Message = userInfo.UserName + " Details not inserted successfully";
  45. }
  46. con.Close();
  47. return Message;
  48. }
  49. }
  50. }
Testing the Service
Press F5 to run the service. A WCF Test Client form will be displayed and it will load the service.
image5.jpg
Now double-click the InserUserDetails() method under IService1. The InserUserDetails tab will be displayed.
image22.jpg
The service was added successfully.
Now open the service in the browser.
Now right-click on the service1.vcs -> open in browser:
image6.jpg
Now copy the URL.
image8.jpg
URL
http://localhost:2268/Service1.svc

Step 3: Create Windows Forms Application (Accessing the Service)

Now, you have to create a windows Forms Application.
img1.jpg
Now add a new page to the website:
img2.jpg
Now again go to the Solution Explorer and click on the add the service reference.
img3.jpg
The following window will be opened:
img4.jpg
Now paste the above URL in the address and click on the "Go" button:
img5.jpg
Click on the Ok Button. Now the reference has been added in the Solution Explorer.
img6.jpg
Now create a new Windows Form and drag and drop controls onto the Windows Form. The designing form looks like below:
img7.jpg
Double-click on the "Save" button, and add the following code with the click event handler:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace WindowsFormsApplication1
  10. {
  11. public partial class Registration : Form
  12. {
  13. ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client(); // Add service reference
  14. public Registration()
  15. {
  16. InitializeComponent();
  17. showdata();
  18. }
  19. private void showdata() // to show the data in the DataGridView
  20. {
  21. DataSet ds = new DataSet();
  22. ds = obj.SelectUserDetails();
  23. dataGridView1.DataSource = ds.Tables[0];
  24. dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
  25. }
  26. private void button1_Click(object sender, EventArgs e)
  27. {
  28. ServiceReference1.UserDetails objuserdetail=new ServiceReference1.UserDetails(); // add type reference
  29. objuserdetail.UserName =textBoxUserName.Text ;
  30. objuserdetail.Password =textBoxPassword.Text ;
  31. objuserdetail.Country =textBoxCountry .Text ;
  32. objuserdetail.Email =textBoxEmail .Text ;
  33. obj.InsertUserDetails(objuserdetail);
  34. showdata();
  35. }
  36. }
  37. }
Now run the application.
Press CTRL+F5 to run the application.
img8.jpg
Now enter the UserName, Password, country and Email and click on the save Button.
img9.jpg
Now click on the save Button. Data will be saved in the database table and also displayed in the DataGridView on the form.
img10.jpg
Data has been inserted into the SQL Server database table and check it.
img11.jpg