none
List Collection is passe as Table Values Parameters Error - Failed to convert parameter value from a List`1 to a IEnumerable`1. RRS feed

  • Question

  • hi,

     I have checkedlistbox in my Winform applciation to select some options. I am going to use merge query to insert or update the user selection as bulk. For this i need to pass the my List collection as Table valued parameters.But when i execute i got the error as 'Failed to convert parameter value from a List`1 to a IEnumerable`1.' The inner exception is '{"Object must implement IConvertible."}'

    The below is my Entry definition

        public class Master
        {
            public int ID { get; set; }
    
            private int StatusID { get; set; }
    
            public RecordStatus Status
            {
                get { return (RecordStatus)this.StatusID; }
                set { this.StatusID = (byte)value; }
            }
    
            public int CreatedBy  { get; set; }
    
            public string CreatedIP { get; set; }
        }
    
        public class BillItemWork : Master
        {
            public int BillItemID { get; set; }
    
            public int ItemGroupWorkID { get; set; }
    
        }

    Form my application i am passing List<BillItemWork> as the table parameters. My table type definition is

    CREATE TYPE [dbo].[BillItemWorkType] AS TABLE(
    	[BillItemID] [int] NULL,
    	[ItemGroupWorkID] [int] NULL,
    	[Status] [int] NULL,
    	[CreatedBy] [int] NULL,
    	[CreatedIP] [varchar](20) NULL
    )

    The following are my DAL code to execute

            public void SaveBillItemWorks(List<BillItemWork> inputItem, int type)
            {
                try
                {
    
                    SqlParameter[] parameters = new SqlParameter[]
                    {
                        new SqlParameter("@workCollection", SqlDbType.Structured)
                    };
    
                    parameters[0].Value = inputItem;
                   
                    DBHelper.TableOperation("myqueryName", parameters);
                }
                catch (SqlException ex)
                {
                    throw new ApplicationException("Error getting object from database.", ex);
                }
                catch (Exception ex)
                {
                    throw new ApplicationException("Error getting object from database.", ex);
                }
            }
    Can anyone help on this?





    Thursday, November 14, 2013 1:10 AM

Answers

  • Hello,

    System.Data.SqlClient supports populating table-valued parameters from DataTable, DbDataReader or System.Collections.Generic.IEnumerable<SqlDataRecord>([T:System.Collections.Generic.IEnumerable`1)] objects.

    We can see it here and search for ‘IEnumerable<SqlDataRecord>’.

    So we need to change List<BillItemWork> to IEnumerabler< BillItemWork > because List<T> is not supported.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 15, 2013 2:53 AM
    Moderator