locked
WCF and Generic list error !! RRS feed

  • Question

  • User-193169305 posted

    Dear All,

    My requirement is to bind datagridview in UI, using WCF.

    UI communicated to WCF and later BLL, BE and DAL.

    This is my DAL layer:

    *********************************************

    public List<UserRegistrationEntity> PopulateUserRegistrationData()
            {
                string constring = ConfigurationManager.ConnectionStrings["XXXXX"].ConnectionString;
                UserRegistrationEntity objUserRegistrationEntity = new UserRegistrationEntity();

                using (SqlConnection con = new SqlConnection(constring))
                {
                    using (SqlCommand cmd = new SqlCommand("select * from userdetails", con))
                    {
                        con.Open();
                        using (SqlDataReader sdr = cmd.ExecuteReader())
                        {                       
                            while (sdr.Read())
                            {
                                objUserRegistrationEntity.UserRegistrationCode = Int32.Parse(sdr["UserRegCd"].ToString());
                                objUserRegistrationEntity.PlotNumber = sdr["PlotNumber"].ToString();
                                objUserRegistrationEntity.FirstName = sdr["FirstName"].ToString();
                                objUserRegistrationEntity.LastName = sdr["LastName"].ToString();
                                objUserRegistrationEntity.Gender = sdr["Gender"].ToString();
                            }
                            con.Close();
                            List<UserRegistrationEntity> lstUserRegistrationEntity = new List<UserRegistrationEntity>();
                            lstUserRegistrationEntity.Add(objUserRegistrationEntity);

                            return lstUserRegistrationEntity;
                        }
                    }
                }
            }

    ********************************************** 

    BLL

    ***********************************************

    public List<UserRegistrationEntity> PopulateUserRegistrationData()
            {
                return objUserRegistrationDA.PopulateUserRegistrationData();
            }

    ***********************************************

    WCF

    *******************************************

    IService.cs

            [OperationContract]
            List<UserRegistrationDetails> PopulateUserRegistrationData(string strUserRegistrationCode); 

    .cs file

    public List<UserRegistrationEntity> PopulateUserRegistrationData(string strUserRegistrationCode)
            {
                lstUserRegistrationEntity = objUserRegistrationFacade.PopulateUserRegistrationData();
                return lstUserRegistrationEntity;
            }

    *******************************************

     When trying to compile, I am getting the compilation error at the below definition:

    namespace MyService
    {
        public class MyWebService : IService1 -->
        {

    ==>Error 1 'xxxxx.xxxxService' does not implement interface member 'xxxxxxx.Ixxxxervice.PopulateUserRegistrationData(string)'. 'xxxxxx.xxxxService.PopulateUserRegistrationData(string)' cannot implement 'xxxxxxx.IxxxxxxService.PopulateUserRegistrationData(string)' because it does not have the matching return type of 'System.Collections.Generic.List<xxxxService.UserRegistrationDetails>'

    Any help would be greatly appreciated.

    Saturday, June 15, 2013 9:05 AM

Answers

  • User1759999623 posted

    So, to clarify, you are doing more than what you have written above in your PopulateUserRegistrationData method, correct?

    Now, have you tried creating a method that only sends one of this objects to the client?

    Also, is lstUserRegistrationDetails arriving at the client as a List object, or as an array?  Try looking at it as an array on the client and then convert it to a List object.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 17, 2013 9:40 AM
  • User503812343 posted

    Only last record is getting added or displayed is because you have added the required code out of loop.

    Below code is out of loop so only one record is gettinfg added.

    List<UserRegistrationEntity> lstUserRegistrationEntity = new List<UserRegistrationEntity>();
    lstUserRegistrationEntity.Add(objUserRegistrationEntity);


    Even you are returning the the List of your DataContract at client side you will get he Array of DataContracts.

    check the articles on step by step implementation DataContract

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 17, 2013 12:13 PM

All replies

  • User1336021323 posted

    Hi!,

    I thin the problem is that in Interface IService.cs you are mentioning List<UserRegistrationDetails> whereas it should be List<UserRegistrationEntity>.


    Thanks

    Ritesh

    Saturday, June 15, 2013 11:22 AM
  • User1759999623 posted

    In addition to the previous response, have you defined a data contracts for UserRegistrationEntity and UserRegistrationDetails?  This will provide proper serialization of your data between the client and the service.  How does the client know about these classes?

    Saturday, June 15, 2013 11:23 AM
  • User-193169305 posted

    Thanks Ritesh.

    Yes, you are right. Lately I found out and corrected just after the post. I am new to WCF. Now, my problem is, how can I call this in the WCF service layer and later call in UI for binding into the grid.

     

    Saturday, June 15, 2013 1:25 PM
  • User1759999623 posted

    The purpose of a WCF service is to provide a proxy for the client, that is, your UI within your client application.  To accomplish this for a .NET application, you can add a service reference that will build the proxy class for you.  This should provide a set of methods that call their corresponding methods on the service-side and it will include your data contracts that the client will know how to work with your custom data types.

    Saturday, June 15, 2013 6:20 PM
  • User-193169305 posted

    I have already done all those basics. Now my problem is that, how can i convert as below and the datareader is looping through all the rows, but basically returning only the last row of the table:

    Services.cs file

    public List<UserRegistrationDetails> PopulateUserRegistrationData()
            {
                List<UserRegistrationEntity> lstUserRegistrationEntity = objUserRegistrationFacade.PopulateUserRegistrationData();
                List<UserRegistrationDetails> lstUserRegistrationDetails = new List<UserRegistrationDetails>();

                return lstUserRegistrationDetails; ==> This is returning null to UI (Proxy)
            }

    My DAL layers is as below:

    public UserRegistrationEntity GetUserRegistrationDetails()
            {
                UserRegistrationEntity objUserRegistrationEntity = new UserRegistrationEntity();
                try
                {
                    Database dbNADS = EnterpriseLibraryContainer.Current.GetInstance<Database>("dbConnect");
                    DbCommand cmdUserRegistration = dbNADS.GetStoredProcCommand(myprocedurename);


                    DataSet dsUserRegInfo = new DataSet();
                    dsUserRegInfo = dbNADS.ExecuteDataSet(cmdUserRegistration);
                    objUserRegistrationEntity.UserRegistrationCode = Int32.Parse(dsUserRegInfo.Tables[0].Rows[0]["UserRegCd"].ToString());
                    objUserRegistrationEntity.PlotNumber = dsUserRegInfo.Tables[0].Rows[0]["PlotNumber"].ToString();
                    objUserRegistrationEntity.FirstName = dsUserRegInfo.Tables[0].Rows[0]["FirstName"].ToString();
                    objUserRegistrationEntity.LastName = dsUserRegInfo.Tables[0].Rows[0]["LastName"].ToString();
                    objUserRegistrationEntity.Gender = dsUserRegInfo.Tables[0].Rows[0]["Gender"].ToString();
                    objUserRegistrationEntity.DateOfBirth = dsUserRegInfo.Tables[0].Rows[0]["DateOfBirth"].ToString();
                    objUserRegistrationEntity.PhoneNumber = dsUserRegInfo.Tables[0].Rows[0]["Phone"].ToString();
                    objUserRegistrationEntity.MobileNumber = dsUserRegInfo.Tables[0].Rows[0]["Mobile"].ToString();
                    objUserRegistrationEntity.EmailAddress = dsUserRegInfo.Tables[0].Rows[0]["Email"].ToString();
                    objUserRegistrationEntity.Nationality = dsUserRegInfo.Tables[0].Rows[0]["Nationality"].ToString();
                    objUserRegistrationEntity.UserType = dsUserRegInfo.Tables[0].Rows[0]["UserType"].ToString();
                }
         catch { }
         return objUserRegistrationEntity;

     

    [DataContract]
        public class UserRegistrationDetails
        {
            int intUserRegistrationCode = 0;
            string strPlotNumber = string.Empty;
            string strFirstname = string.Empty;
            string strLastname = string.Empty;
            string strGender = string.Empty;
            string strDateofBirth = string.Empty;
            string strPhonenumber = string.Empty;
            string strMobileNumber = string.Empty;
            string strEmailAddress = string.Empty;
            string strNationality = string.Empty;
            string strUsertype = string.Empty;

            [DataMember]
            public int UserRegistrationCode { get; set; }
            [DataMember]
            public string PlotNumber { get; set; }
            [DataMember]

    Monday, June 17, 2013 7:06 AM
  • User1759999623 posted

    If this is your actual PopulateUserRegistrationDetail method, then you are returning back a brand new empty List<UserRegistrationDetails>.  It will contain no values.  What is the proxy supposed to do with your empty list?  Does the proxy understand the UserRegistrationDetails data type, noting that it can only know about the UserRegistrationCode and the PlotNumber?

    Monday, June 17, 2013 9:16 AM
  • User-193169305 posted

    The complete list is as below:

    [DataMember]
            public int UserRegistrationCode { get; set; }
            [DataMember]
            public string PlotNumber { get; set; }
            [DataMember]
            public string FirstName { get; set; }
            [DataMember]
            public string LastName { get; set; }
            [DataMember]
            public string Gender { get; set; }
            [DataMember]
            public string DateOfBirth { get; set; }
            [DataMember]
            public string PhoneNumber { get; set; }
            [DataMember]
            public string MobileNumber { get; set; }
            [DataMember]
            public string EmailAddress { get; set; }
            [DataMember]
            public string Nationality { get; set; }
            [DataMember]
            public string UserType { get; set; }
        }

    How can I convert the generic list from BLL to be converted to WS as below:

    public List<UserRegistrationDetails> PopulateUserRegistrationData()
            {
                List<UserRegistrationEntity> lstUserRegistrationEntity = objUserRegistrationFacade.PopulateUserRegistrationData();
                List<UserRegistrationDetails> lstUserRegistrationDetails = new List<UserRegistrationDetails>();

    /***********************/

    here i want to convert "lstUserRegistrationEntity " to "lstUserRegistrationDetails " and later return as below.

    /***********************/

                return lstUserRegistrationDetails; --> This is understtod by proxy and then I can bind to a gridview.
            }

     

    Monday, June 17, 2013 9:28 AM
  • User1759999623 posted

    So, to clarify, you are doing more than what you have written above in your PopulateUserRegistrationData method, correct?

    Now, have you tried creating a method that only sends one of this objects to the client?

    Also, is lstUserRegistrationDetails arriving at the client as a List object, or as an array?  Try looking at it as an array on the client and then convert it to a List object.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 17, 2013 9:40 AM
  • User503812343 posted

    Only last record is getting added or displayed is because you have added the required code out of loop.

    Below code is out of loop so only one record is gettinfg added.

    List<UserRegistrationEntity> lstUserRegistrationEntity = new List<UserRegistrationEntity>();
    lstUserRegistrationEntity.Add(objUserRegistrationEntity);


    Even you are returning the the List of your DataContract at client side you will get he Array of DataContracts.

    check the articles on step by step implementation DataContract

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 17, 2013 12:13 PM