Question

I am trying to work with the NuGet Package FastMember to load a List Object into a DataTable object.

Following is the code I am using.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using FastMember;

namespace ConsoleScratchPad
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] a = { "The", "Big", "Ant" };
            List<string> ls = a.ToList();
            DataTable dt = new DataTable();
            using (var reader = ObjectReader.Create(ls))
            {
                dt.Load(reader);
                     //^--------------------Error
            }
        }
    }
}

At the specified mark I am getting the following error.

System.ArgumentOutOfRangeException was unhandled
  HResult=-2146233086
  Message=Specified argument was out of the range of valid values.
Parameter name: name
  Source=FastMember_dynamic
  ParamName=name
  StackTrace:
       at FastMember_dynamic.String_1.get_Item(Object , String )
       at FastMember.ObjectReader.System.Data.IDataRecord.GetValues(Object[] values) in c:\Dev\fast-member\FastMember\ObjectReader.cs:line 300
       at System.Data.ProviderBase.DataReaderContainer.CommonLanguageSubsetDataReader.GetValues(Object[] values)
       at System.Data.ProviderBase.SchemaMapping.LoadDataRow()
       at System.Data.Common.DataAdapter.FillLoadDataRow(SchemaMapping mapping)
       at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
       at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
       at System.Data.Common.LoadAdapter.FillFromReader(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
       at System.Data.DataTable.Load(IDataReader reader, LoadOption loadOption, FillErrorEventHandler errorHandler)
       at System.Data.DataTable.Load(IDataReader reader)
       at ConsoleScratchPad.Program.Main(String[] args) in f:\Dropbox\Projects\visual studio 2012\Projects\ConsoleScratchPad\ConsoleScratchPad\Program.cs:line 31
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Want to know what I am doing wrong here and what is the fix for it.

I am using visual studio 2012 with c#.net console application.

Était-ce utile?

La solution

I'm not familiar with the FastMember package but it seems you can't use array of strings for the data source. This will work:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using FastMember;

namespace ConsoleScratchPad
{
    class Program
    {
        static void Main(string[] args)
        {
            IList<MyClass> ls = new List<MyClass>();
            ls.Add(new MyClass { MyColumn1 = "The" });
            ls.Add(new MyClass { MyColumn1 = "Big" });
            ls.Add(new MyClass { MyColumn1 = "Ant" });
            DataTable dt = new DataTable();
            using (var reader = ObjectReader.Create(ls))
            {
                dt.Load(reader);
            }
        }
    }

    public class MyClass
    {
        public string MyColumn1 { get; set; }
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top