Working With The Error Cannot Implicitly Convert Type 'ListName[]' to 'System.Collections.Generic.IList<ListName>' in WCF.

In this article I am going to discuss about a common error that most of the time comes while working with WCF.

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.
  1. public partial class CRUD : System.Web.UI.Page  
  2.     {  
  3.         ServiceReference.Service1Client obj = new ServiceReference.Service1Client();  
  4.         ServiceReference.Employee1 emp = new ServiceReference.Employee1();  
  5.         protected void Page_Load(object sender, EventArgs e)  
  6.         {  
  7.               
  8.             if(!IsPostBack)  
  9.             {  
  10.   
  11.                 BindData();  
  12.   
  13.             }  
  14.   
  15.         }  
  16.         public void BindData()  
  17.         {  
  18.             List<ServiceReference.Employee1> li = new List<ServiceReference.Employee1>();  
  19.   
  20.   
  21.             li = obj.ShowAll();  
  22.   
  23.             GridView1.DataSource = li;  
  24.             GridView1.DataBind();  
  25.              
  26.   
  27.         }  
 And here I have the following HTML code for GridView.
  1. <div>  
  2. <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">  
  3.               <Columns>  
  4.                   <asp:TemplateField HeaderText="EMPLOYEE ID">  
  5.                        
  6.                       <ItemTemplate>  
  7.                           <asp:Label ID="lbl_empid" runat="server" Text='<%# Bind("EmpId") %>'></asp:Label>  
  8.                       </ItemTemplate>  
  9.                       <ItemStyle Width="150px" />  
  10.                   </asp:TemplateField>  
  11.                   <asp:TemplateField HeaderText="EMPLOYEE NAME">  
  12.                        
  13.                       <ItemTemplate>  
  14.                           <asp:Label ID="Label2" runat="server" Text='<%# Bind("EmpName") %>'></asp:Label>  
  15.                       </ItemTemplate>  
  16.                       <ItemStyle Width="150px" />  
  17.                   </asp:TemplateField>  
  18.                   <asp:TemplateField HeaderText="COMPANY NAME">  
  19.                        
  20.                       <ItemTemplate>  
  21.                           <asp:Label ID="Label3" runat="server" Text='<%# Bind("CompanyName") %>'></asp:Label>  
  22.                       </ItemTemplate>  
  23.                       <ItemStyle Width="100px" />  
  24.                   </asp:TemplateField>  
  25.                   <asp:TemplateField HeaderText="LOCATION">  
  26.                        
  27.                       <ItemTemplate>  
  28.                           <asp:Label ID="Label4" runat="server" Text='<%# Bind("Location") %>'></asp:Label>  
  29.                       </ItemTemplate>  
  30.                       <ItemStyle Width="150px" />  
  31.                   </asp:TemplateField>  
  32.   
  33.                    <asp:TemplateField HeaderText="DEPARTMENT">  
  34.                      
  35.                       <ItemTemplate>  
  36.                           <asp:Label ID="Label5" runat="server" Text='<%# Bind("Dept") %>'></asp:Label>  
  37.                       </ItemTemplate>  
  38.                       <ItemStyle Width="150px" />  
  39.                   </asp:TemplateField>  
  40.         
  41.                   <asp:TemplateField HeaderText="EDIT" ShowHeader="False">  
  42.                        
  43.                       <ItemTemplate>  
  44.                           <asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />  
  45.                       </ItemTemplate>  
  46.                   </asp:TemplateField>  
  47.         
  48.                   <asp:TemplateField HeaderText="DELETE">  
  49.                       <ItemTemplate>  
  50.                           <asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" />  
  51.                       </ItemTemplate>  
  52.                   </asp:TemplateField>  
  53.         
  54.           </Columns>  
  55.                <FooterStyle BackColor="#CCCCCC" />  
  56.                <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />  
  57.                <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />  
  58.                <RowStyle BackColor="White" />  
  59.                <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />  
  60.                <SortedAscendingCellStyle BackColor="#F1F1F1" />  
  61.                <SortedAscendingHeaderStyle BackColor="#808080" />  
  62.                <SortedDescendingCellStyle BackColor="#CAC9C9" />  
  63.                <SortedDescendingHeaderStyle BackColor="#383838" />  
  64.            </asp:GridView>  
  65.   
  66.        </div>  
  67.      
  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: