简体   繁体   English

DataTable.Load()指定的参数超出了有效值的范围

[英]DataTable.Load() Specified argument was out of the range of valid values

I am trying to work with the NuGet Package FastMember to load a List Object into a DataTable object. 我正在尝试使用NuGet包FastMember将List对象加​​载到DataTable对象中。

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. 我正在使用带有c#.net控制台应用程序的visual studio 2012。

I'm not familiar with the FastMember package but it seems you can't use array of strings for the data source. 我不熟悉FastMember包,但似乎你不能使用数组源字符串。 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; }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 指定的参数超出有效值范围-RowDataBound - Specified argument was out of the range of valid values - RowDataBound “指定的参数超出了有效值的范围” - “Specified argument was out of the range of valid values” DropDownListFor没有“指定的参数超出有效值范围” - No “Specified argument was out of the range of valid values” with DropDownListFor 错误:指定的参数超出有效值范围。 在RowDataBound Gridview中 - Error: Specified argument was out of the range of valid values. in RowDataBound Gridview aspx.net 指定的参数超出了有效值的范围 - aspx.net Specified argument was out of the range of valid values 从URI下载:指定的参数超出有效值范围 - downloading from a URI : Specified argument was out of the range of valid values Sharepoint UploadFile&#39;指定的参数超出有效值范围&#39; - Sharepoint UploadFile 'Specified argument was out of range of valid values' 指定的参数超出有效值范围。 参数名称:大小 - Specified argument was out of the range of valid values. Parameter name: size 指定的参数超出有效值范围-dataGridView maskedTextBox - Specified argument was out of the range of valid values - dataGridView maskedTextBox 得到“指定的参数超出有效值范围。 参数名称:” - Getting “Specified argument was out of the range of valid values. Parameter name:”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM