In this article I am going to discuss a common problem that we will encounter while working with WCF. WCF (Windows Communication Foundation) is a technology provided by Microsoft as a part of DotNet framework to develop rich distributed application. In other words, we need WCF to create a service which helps in communication between multi-platform clients.
Here, I have created a WCF service and now I will consume this service in my client application. For consuming this application I did the following.
In my client application solution explorer I am adding a Service Reference as follows.
In this service I have 2 methods and here I want to use ShowAll method to display all the data from and table and bind the data to a GridView. Now here is my code which will bind the data in the database.
- public partial class CRUD : System.Web.UI.Page
- {
- ServiceReference.Service1Client obj = new ServiceReference.Service1Client();
- ServiceReference.Employee1 emp = new ServiceReference.Employee1();
- protected void Page_Load(object sender, EventArgs e)
- {
-
- if(!IsPostBack)
- {
-
- BindData();
-
- }
-
- }
- public void BindData()
- {
- List<ServiceReference.Employee1> li = new List<ServiceReference.Employee1>();
-
-
- li = obj.ShowAll();
-
- GridView1.DataSource = li;
- GridView1.DataBind();
-
-
- }
And here I have the following HTML code for GridView.
- <div>
- <asp:GridView ID="GridView1" runat="server" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" Width="700px" AutoGenerateColumns="False" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting">
- <Columns>
- <asp:TemplateField HeaderText="EMPLOYEE ID">
-
- <ItemTemplate>
- <asp:Label ID="lbl_empid" runat="server" Text='<%# Bind("EmpId") %>'></asp:Label>
- </ItemTemplate>
- <ItemStyle Width="150px" />
- </asp:TemplateField>
- <asp:TemplateField HeaderText="EMPLOYEE NAME">
-
- <ItemTemplate>
- <asp:Label ID="Label2" runat="server" Text='<%# Bind("EmpName") %>'></asp:Label>
- </ItemTemplate>
- <ItemStyle Width="150px" />
- </asp:TemplateField>
- <asp:TemplateField HeaderText="COMPANY NAME">
-
- <ItemTemplate>
- <asp:Label ID="Label3" runat="server" Text='<%# Bind("CompanyName") %>'></asp:Label>
- </ItemTemplate>
- <ItemStyle Width="100px" />
- </asp:TemplateField>
- <asp:TemplateField HeaderText="LOCATION">
-
- <ItemTemplate>
- <asp:Label ID="Label4" runat="server" Text='<%# Bind("Location") %>'></asp:Label>
- </ItemTemplate>
- <ItemStyle Width="150px" />
- </asp:TemplateField>
-
- <asp:TemplateField HeaderText="DEPARTMENT">
-
- <ItemTemplate>
- <asp:Label ID="Label5" runat="server" Text='<%# Bind("Dept") %>'></asp:Label>
- </ItemTemplate>
- <ItemStyle Width="150px" />
- </asp:TemplateField>
-
- <asp:TemplateField HeaderText="EDIT" ShowHeader="False">
-
- <ItemTemplate>
- <asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
- </ItemTemplate>
- </asp:TemplateField>
-
- <asp:TemplateField HeaderText="DELETE">
- <ItemTemplate>
- <asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" />
- </ItemTemplate>
- </asp:TemplateField>
-
- </Columns>
- <FooterStyle BackColor="#CCCCCC" />
- <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
- <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
- <RowStyle BackColor="White" />
- <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
- <SortedAscendingCellStyle BackColor="#F1F1F1" />
- <SortedAscendingHeaderStyle BackColor="#808080" />
- <SortedDescendingCellStyle BackColor="#CAC9C9" />
- <SortedDescendingHeaderStyle BackColor="#383838" />
- </asp:GridView>
-
- </div>
-
This will generate the following GridView.
Now while running this code i am getting the following exception as given below.
This error indicating that WCF.serviceReferance.Employee1[] array cannot convert to system.collections.Generic.List<WCFClient.ServiceReferance.Employee1>, where employee1 si my model that is taken in wcf service.
So to convert this we have to follow the below given steps.
- Firstly, Right click on your Service Reference and go to the "Configure Service Reference".
- Now it will show the following screen after clicking this.
Now as I marked the system.Array change it to the "System.Collection.Generic.List" and then build the solution.
After building the solution run the solution it will work perfectly and bind the data to the GridView.
So in this we we can solve the above problem.
Read more articles on WCF: