plmce2005 / dapper-dot-net

Automatically exported from code.google.com/p/dapper-dot-net
Other
0 stars 0 forks source link

InvalidProgramException thrown on query #52

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
InvalidProgramException: 
"Common Language Runtime detected an invalid program."

Using with Dapper NET35

Pretty simple select the just returns strings:
select reason from dbo.CommonReasons where is_active=1 and reason_type='Test'

Original issue reported on code.google.com by mr.al.go...@gmail.com on 25 Jul 2011 at 4:54

GoogleCodeExporter commented 8 years ago
Please can you add an indication of your Query usage? The following works fine 
on NET35:

        SqlConnection connection = Program.GetOpenConnection();

        public void TestBasicStringUsage()
        {
            var arr = connection.Query<string>("select 'abc' union all select @txt", new {txt = "def"}, null, false, null, null).
                ToArray();
            arr.IsSequenceEqualTo(new[] { "abc", "def" });
        }

Original comment by marc.gravell on 25 Jul 2011 at 6:24

GoogleCodeExporter commented 8 years ago
as does:

        public void TestClassWithStringUsage()
        {
            var arr = connection.Query<BasicType>("select 'abc' as [Value] union all select @txt", new { txt = "def" }, null, false, null, null).
                ToArray();
            arr.Select(x => x.Value).IsSequenceEqualTo(new[] { "abc", "def" });
        }
        class BasicType
        {
            public string Value { get; set; }
        }

Original comment by marc.gravell on 25 Jul 2011 at 6:27

GoogleCodeExporter commented 8 years ago
I'm going to have to close this without a repro...

Original comment by marc.gravell on 4 Aug 2011 at 5:16

GoogleCodeExporter commented 8 years ago
Hi,

I am getting the same error for the latest version of Dapper (as of 12 SEP 
2011).  Hope this helps.

System: .Net 3.5 , MySql Net v6.3.7

Simplest reproduction:

using (MySqlConnection conn = DALBase.CreateConnection())
                {
                    var xx = conn.Query<int>("select 8", null);  //original query => "select ID from mgroup where CompanyID=@PCompanyID and MGroupCode=@PMGroupCode"
                }

Stack
System.InvalidProgramException: Common Language Runtime detected an invalid 
program.
   at Dapper.SqlMapper.GetStructDeserializer[T](Int32 index)
   at Dapper.SqlMapper.GetDeserializer[T](IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) in E:\TestSite\DapperTest\DapperTest\Dapper.cs:line 813
   at Dapper.SqlMapper.<QueryInternal>d__4`1.MoveNext() in E:\TestSite\DapperTest\DapperTest\Dapper.cs:line 501
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in E:\TestSite\DapperTest\DapperTest\Dapper.cs:line 452
   at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param) in E:\TestSite\DapperTest\DapperTest\Dapper.cs:line 373
   at DapperTest.Form1.btnAggregateRoot_Click(Object sender, EventArgs e) in E:\TestSite\DapperTest\DapperTest\Form1.cs:line 250

Original comment by charles...@gmail.com on 12 Sep 2011 at 5:27

GoogleCodeExporter commented 8 years ago
Interesting. I can't repro with SQL server (it just works), and in particular 
*we don't do any IL* in GetStructDeserializer. I will have to install MySql to 
repro. But thanks for coming back with a repro - makes it possible to 
investigate.

Original comment by marc.gravell on 12 Sep 2011 at 6:26

GoogleCodeExporter commented 8 years ago
Hi,

I'm not sure of the actual reason but I've made some changes to the 
GetStructDeserializer which eliminated the raising of the exception.

Basically, what I did was comment out the 3 special-handling case and threw 
NotSupportedException instead since they are unlikely to be used by my current 
test project.

Hope this helps to isolate the problem and I'm sorry I really can't provide 
much more beyond this.

#pragma warning disable 618

            Type tType = typeof(T);

            if ((tType == typeof(char)) || (tType == typeof(char?)) || (tType == typeof(System.Data.Linq.Binary)))
                throw new NotSupportedException("Type : " + tType.ToString() + " not supported.");
            //if (tType == typeof(char))
            //{ // this *does* need special handling, though
            //    return (Func<IDataReader, T>)(object)new Func<IDataReader, char>(r => SqlMapper.ReadChar(r.GetValue(index)));
            //}
            //if (tType == typeof(char?))
            //{
            //    return (Func<IDataReader, T>)(object)new Func<IDataReader, char?>(r => SqlMapper.ReadNullableChar(r.GetValue(index)));
            //}
            //if (tType == typeof(System.Data.Linq.Binary))
            //{
            //    return (Func<IDataReader, T>)(object)new Func<IDataReader, System.Data.Linq.Binary>(r => new System.Data.Linq.Binary((byte[])r.GetValue(index)));
            //}
#pragma warning restore 618

Original comment by charles...@gmail.com on 12 Sep 2011 at 8:36

GoogleCodeExporter commented 8 years ago
I made further tests and this apparently gave the result without the exception 
being thrown.  Hope this helps.

Changed:

            if (typeof(T) == typeof(char))
            { // this *does* need special handling, though
                return (Func<IDataReader, T>)(object)new Func<IDataReader, char>(r => SqlMapper.ReadChar(r.GetValue(index)));
            }

Changed To:
            if (typeof(T) == typeof(char))
            { // this *does* need special handling, though
                Func<IDataReader, T> meth = r => (T)Convert.ChangeType(SqlMapper.ReadChar(r.GetValue(index)), typeof(T));
                return meth;
            }

Original comment by charles...@gmail.com on 12 Sep 2011 at 1:52

GoogleCodeExporter commented 8 years ago
I've got same exception when before executing Query I will set 
"Dapper.SqlMapper.AddTypeMap(typeof(String), DbType.AnsiString);" 
Is there any fix for that? 

Original comment by kirek...@gmail.com on 8 May 2014 at 1:23

GoogleCodeExporter commented 8 years ago
I am working through the pull request and issue list currently; will 
investigate and get back to you

Original comment by marc.gravell on 8 May 2014 at 1:24

GoogleCodeExporter commented 8 years ago
Any news on this issue? Changing the TypeMap to AnsiString generates this 
exception when passing in string parameters in the where clause. This pull 
request seemed to fix it: 
https://github.com/StackExchange/dapper-dot-net/pull/117

Original comment by jstaw...@gmail.com on 21 Jul 2014 at 7:57

GoogleCodeExporter commented 8 years ago
Merged 117

Original comment by marc.gravell on 5 Aug 2014 at 3:50