Fastmember exception: Specified argument was out of the range of valid values. Parameter name: name
Asked Answered
S

4

6

I've been encountering this error Specified argument was out of the range of valid values. Parameter name: name

When im almost just copying the example here https://code.google.com/p/fast-member/

The error happens on the bcp.WriteToServer(reader), been looking for additional information but i still dont know what is causing the problem and the example is so simple... And i dont even know where the parameter named name is coming from.

My actual code is below

        using (var bcp = new SqlBulkCopy(configvalue1))
        using (var reader = ObjectReader.Create(DataToLoad, new string[]{"id","field1","field2","field3"}))
        {
            bcp.DestinationTableName = string.Format(DestinationTableFormat, DestinationDb, DestinationSchema, DestinationTable);
            bcp.BatchSize = BatchSize ?? 10000;
            bcp.WriteToServer(reader);
            bcp.Close();
        }

Can someone help?

Thanks in advance

Sailboat answered 13/3, 2015 at 17:26 Comment(1)
I'm not sure about the details of the generic type in variable DataToLoad, but it is possible that it has inherited Interface members in it, which seems to be not supported by FastMember in the current version. I suggested a solution here: #41241631Kus
G
3

For me this was caused by a mismatch between the property names on source data object and the parameter list in the reader creation step.

class Supplier
{
    public string Code;
    public string Name;
}

// version that caused the error 
using (var rdr = ObjectReader.Create(suppliers, "code", "name"))

// compared to re-cased version to match the Supplier object property casing
using (var rdr = ObjectReader.Create(suppliers, "Code", "Name"))

I had used Re-sharper which re-case the "code" and "name" properties in the Supplier object and that meant the reader couldn't map to the properties. I left the properties in the revised case and amended the reader parameter list to match as shown in the second line.

Grateful answered 2/2, 2017 at 12:7 Comment(0)
S
2

I believe i now know why this is happening.

This is an example that actually works, this one uses a concrete class as its POCO and creates a generic List of that POCO as seen below.

        IList<MyClass> ls = new List<MyClass>();
        ls.Add(new MyClass { MyColumn1 = "The", MyColumn2 = "Test2" });
        ls.Add(new MyClass { MyColumn1 = "Big", MyColumn2 = "Test2" });
        ls.Add(new MyClass { MyColumn1 = "Ant", MyColumn2 = "Test2" });
        DataTable dt = new DataTable();
        using (var reader = ObjectReader.Create(ls))
        {
            dt.Load(reader);
        }

This one is more of a list of anonymous objects that you assign properties at real time.

        IList<object> ls2 = new List<object>();
        ls2.Add(new { MyColumn1 = "The", MyColumn2="Test2" });
        ls2.Add(new { MyColumn1 = "Big", MyColumn2="Test2" });
        ls2.Add(new { MyColumn1 = "Ant", MyColumn2="Test2" });
        DataTable dt2 = new DataTable();
        using (var reader2 = ObjectReader.Create(ls2))
        {
            dt2.Load(reader2);
        }

This doesnt work as well

        IList<dynamic> ls3 = new List<dynamic>();
        ls3.Add(new { MyColumn1 = "The", MyColumn2 = "Test2" });
        ls3.Add(new { MyColumn1 = "Big", MyColumn2 = "Test2" });
        ls3.Add(new { MyColumn1 = "Ant", MyColumn2 = "Test2" });
        DataTable dt3 = new DataTable();
        using (var reader3 = ObjectReader.Create(ls3))
        {
            dt3.Load(reader3);
        }

Even though the 3 lists are functionally the same, one a list of a POCO , one a list of anonymous objects, one a list of dynamic members, FASTMEMBER can't properly read the properties of an anonymous/ dynamic object inside a List at run time (ex: CANT SEE THE MyColumn1 and MyColumn2) even if it is there.

So this is more of a limitation

Sailboat answered 13/3, 2015 at 18:25 Comment(0)
B
2

For us this was caused by combination of generics and inheritance

abstract class A { public string A; }
class B : A { public string B; }

class ItemContainer { public A A; }


async Task DumpDataToTable<T>(IEnumerable<T> items, string tableName, string[] properties)
{
    using var sqlCopy = new SqlBulkCopy(AC.OpenConnection);

    sqlCopy.DestinationTableName = tableName;

    using var reader = ObjectReader.Create(items, properties);
    await sqlCopy.WriteToServerAsync(reader);
}

IEnumerable<ItemContainer> items = ....    
var manyAs = items.Select(item => item.A);

// here generic type parameter is `A`, and it doesn't contain `B`
await DumpDataToTable(manyAs, "#temptable", new[]{"A", "B"})

The solution was to correct the dumptable to:

async Task DumpDataToTable<T>(IEnumerable<T> items, string tableName, string[] properties)
{
    using var sqlCopy = new SqlBulkCopy(AC.OpenConnection));
    
    sqlCopy.DestinationTableName = tableName;
    sqlCopy.BatchSize = 500;
    
    var item = items.FirstOrDefault();
    if (item == null)
        return;

    using var reader = new ObjectReader(item.GetType(), items, properties));
    await sqlCopy.WriteToServerAsync(reader);
}

That will not handle mixed types for ItemContainer.A, but for us we know that they're the same type if any.

Byssinosis answered 26/3, 2018 at 7:53 Comment(0)
A
0

In my case it is due to a leading empty space in the column configuration. It took an hour to cross check all the 40+ columns in the list. Note: columnB has leading spaec. columnC has trailing space. Check the case as well.

var columns = new string[] { "columnA", " columnB", "columnC " };

        using (var bcp = new SqlBulkCopy(connStr))
        using (var reader = ObjectReader.Create(newItems.ToList(), columns))
        {
            bcp.DestinationTableName = "targetTable1";
            bcp.WriteToServer(reader);
        }
Anabasis answered 10/4, 2018 at 3:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.