Search code examples
c#.netado.netclrfatal-error

Fatal error when trying to populate list object


I am getting below error when i am trying to populate list object :

Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in 'C:\Users\admin\Documents\Visual Studio 2015\Projects\Testing\Testing\bin\Debug\Testing.vshost.exe'.

Additional information: The runtime has encountered a fatal error. The address of the error was at 0x5845e24f, on thread 0x2278. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

Error is thrown on this below line :

model n = new model
{
     Id = reader.IsDBNull(id) ? null : reader.GetString(id),
     Value = reader.IsDBNull(value) ? 0 : reader.GetDecimal(value)
};

Datatype for Id and Value in database is :

Id: varchar(60)
Value : numeric(31,13)

Total Records in Table : 3066700

My list is getting filled with 322283 much records after it throws exception.

As per this Link it says this is a compiler bug.

I am failing to understand the problem what i am doing wrong.

Can anybody please help me with this?

Code :

public class model
{
    public string Id { get; set; }
    public decimal Value { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        var sourceData = GetData(SourceConnectionstring,
                                    "select Id,Value from source");

        var targetData = GetData(TargetConnectionstring,
                                    "select Id,Value from target");

        var data = (from s in sourceData
                    join t in targetData on s.Id equals t.Id
                    where s.Value != t.Value
                    select new
                    {
                        srrId = s.Id,
                        srcValue = s.Value,
                        tgtId = t.Id,
                        tgtValue = t.Value
                    }).ToArray();
    }

    public static List<model> GetData(string connectionString, string sqlQuery)
    {
        List<model> m = new List<model>();
        using (var sqlConnection = new SqlConnection(connectionString))
        {
            using (var command = new SqlCommand(sqlQuery, sqlConnection))
            {
                sqlConnection.Open();
                using (var reader = command.ExecuteReader())
                {
                    var id = reader.GetOrdinal("Id");
                    var value = reader.GetOrdinal("Value");
                    int c = 0;
                    while (reader.Read())
                    {
                        model n = new model
                        {
                            Id = reader.IsDBNull(id) ? null : reader.GetString(id),
                            Value = reader.IsDBNull(value) ? 0 : reader.GetDecimal(value)
                        };
                         m.Add(n);
                         c = c + 1;
                      }
                      reader.Close();
                    }
                }
                sqlConnection.Close();
            }
            return m;
        }
    }

Update : Getting a strange behaviour like sometimes it is throwing error and sometimes not.

When i update query like this then its working fine :

select top 900000 Id,Value from source

But when i debug GetData method it throws error.

It is working fine with datatable.

Code :

public static DataTable GetData(string connectionString, string sqlQuery)
        {
            using (SqlConnection sqlConn = new SqlConnection(connectionString))
            using (SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn))
            {
                sqlConn.Open();
                DataTable dt = new DataTable();
                dt.Load(cmd.ExecuteReader());
                return dt;
            }
    }

I have tested List object code on my colleague Pc and uptill 900000 it is working fine but now when i work with all records it throws error :

Unable to cast object of type 'System.Int32' to type 'System.String'

Solution

  • I guess the issue was with the data because when i tested data with 900000 then it was working fine but then when i included all data it was throwing error(Unable to cast object of type 'System.Int32' to type 'System.String') so then i realize there might be something wrong with the data conversion hence because of this either one of the below 2 lines might be failing while converting data:

    Id = reader.IsDBNull(id) ? null : reader.GetString(id),
    Value = reader.IsDBNull(value) ? 0 : reader.GetDecimal(value)
    

    This is how i manage to resolve this issue and now it is working fine with 3069300 records also :

    public class model
        {
            public object Id { get; set; }
            public object Value { get; set; }
        }
        model n = new model
        {
            Id = reader.GetValue(keyColumn),
            Value = reader.GetValue(snapshotValue)    
        };