none
Attempting to call CLR stored proc but getting cast exception from "Anonymously Hosted DynamicMethods Assembly" RRS feed

  • Question

  • So a quick overview.  This is a VS2010 project and I'm using LinqToSql and attempting to call a CLR stored procedure named "GetChartsByCoder".  Here is the code that actually does the call.

                object[] tempCharts =
                    DbContext.GetChartsByCoder(coderID, numberChartsOnLaptop).ToArray<GetChartsByCoderResult>();

    So when I run the code the following exception occurs:

       EXCEPTION MESSAGE
       ========
       Unable to cast object of type 'System.String' to type 'System.Byte[]'.
      
      
       SOURCE OF EXCEPTION
       ===========
       Anonymously Hosted DynamicMethods Assembly
      
      
       STACK TRACE
       ============
          at Read_GetChartsByCoderResult(ObjectMaterializer`1 )
          at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
          at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
          at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
          at CodeStarBL.ChartService.GetChartsEligibleForDownload(String coderID, Int32 numberChartsOnLaptop) in C:\Documents and Settings\BSmith\My Documents\Projects\CodeStar\CodeStarBL\ChartService.cs:line 33

    I have stepped thru the code and the error is thrown right after leaving the default constructor of GetChartsByCoderResult - btw there is no code within the default constructor - just the opening and closing brace.  But as soon as I step thru the constructors closing brace the error is thrown.  And as mentioned above, the error message states "Unable to cast object of type 'System.String' to type 'System.Byte[]".  I've searched the entire project for System.Byte[] and can't find it.  What in the world is going on here?  Thanks!
    Monday, June 21, 2010 5:22 PM

All replies

  • What does the store procedure return.. and what is the code of the class GetChartsByCoderResult

    Monday, June 21, 2010 7:17 PM
  • Am using SqlContext.Pipe.SendResultsStart/End to return the rows.  The record layout for the rows being returned is shown below and also the class definition for GetChartsByCoderResult is also below, thanks!

    ============================================

    SqlDataRecord record = new SqlDataRecord(
    new SqlMetaData("ChartImageID", SqlDbType.UniqueIdentifier),
    new SqlMetaData("ClientChartID", SqlDbType.UniqueIdentifier),
    new SqlMetaData("ScanDate", SqlDbType.DateTime),
    new SqlMetaData("ChartFileName", SqlDbType.VarChar, 254),
    new SqlMetaData("ChartDirectory", SqlDbType.VarChar, 254),
    new SqlMetaData("IsCodingRequired", SqlDbType.Bit),
    new SqlMetaData("IsSpanish", SqlDbType.Bit),
    new SqlMetaData("DateOfService", SqlDbType.DateTime),
    new SqlMetaData("WorkLevel", SqlDbType.VarChar, 100),
    new SqlMetaData("IsParkedChart", SqlDbType.Bit),
    new SqlMetaData("HICN", SqlDbType.VarChar, 254),
    new SqlMetaData("PlanNumber", SqlDbType.VarChar, 254),
    new SqlMetaData("ProviderName", SqlDbType.VarChar, 254),
    new SqlMetaData("PatientName", SqlDbType.VarChar, 254),
    new SqlMetaData("Gender", SqlDbType.VarChar, 254),
    new SqlMetaData("EligibilityDate", SqlDbType.DateTime),
    new SqlMetaData("HasChartNotes", SqlDbType.Bit),
    new SqlMetaData("ChartNotes", SqlDbType.VarChar, 4000)
    );

     

    Here is the class definition for GetChartsByCoderResult:

    ============================================

        public partial class GetChartsByCoderResult
        {
           
            private System.Nullable<System.Guid> _ChartImageID;
           
            private System.Nullable<System.Guid> _ClientChartID;
           
            private System.Nullable<System.DateTime> _ScanDate;
           
            private string _ChartFilename;
           
            private string _ChartDirectory;
           
            private System.Nullable<bool> _IsCodingRequired;
           
            private System.Nullable<bool> _IsSpanish;
           
            private System.Nullable<System.DateTime> _DateOfService;
           
            private System.Data.Linq.Binary _Worklevel;
           
            private System.Nullable<bool> _IsParkedChart;
           
            private string _HICN;
           
            private string _PlanNumber;
           
            private string _ProviderName;
           
            private string _PatientName;
           
            private string _Gender;
           
            private System.Nullable<System.DateTime> _EligibilityDate;
           
            private System.Nullable<bool> _HasChartNotes;
           
            private string _ChartNotes;
           
            public GetChartsByCoderResult()
            {
            }
           
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ChartImageID", DbType="UniqueIdentifier")]
            public System.Nullable<System.Guid> ChartImageID
            {
                get
                {
                    return this._ChartImageID;
                }
                set
                {
                    if ((this._ChartImageID != value))
                    {
                        this._ChartImageID = value;
                    }
                }
            }
           
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ClientChartID", DbType="UniqueIdentifier")]
            public System.Nullable<System.Guid> ClientChartID
            {
                get
                {
                    return this._ClientChartID;
                }
                set
                {
                    if ((this._ClientChartID != value))
                    {
                        this._ClientChartID = value;
                    }
                }
            }
           
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ScanDate", DbType="DateTime")]
            public System.Nullable<System.DateTime> ScanDate
            {
                get
                {
                    return this._ScanDate;
                }
                set
                {
                    if ((this._ScanDate != value))
                    {
                        this._ScanDate = value;
                    }
                }
            }
           
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ChartFilename", DbType="VarChar(254)")]
            public string ChartFilename
            {
                get
                {
                    return this._ChartFilename;
                }
                set
                {
                    if ((this._ChartFilename != value))
                    {
                        this._ChartFilename = value;
                    }
                }
            }
           
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ChartDirectory", DbType="VarChar(254)")]
            public string ChartDirectory
            {
                get
                {
                    return this._ChartDirectory;
                }
                set
                {
                    if ((this._ChartDirectory != value))
                    {
                        this._ChartDirectory = value;
                    }
                }
            }
           
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_IsCodingRequired", DbType="Bit")]
            public System.Nullable<bool> IsCodingRequired
            {
                get
                {
                    return this._IsCodingRequired;
                }
                set
                {
                    if ((this._IsCodingRequired != value))
                    {
                        this._IsCodingRequired = value;
                    }
                }
            }
           
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_IsSpanish", DbType="Bit")]
            public System.Nullable<bool> IsSpanish
            {
                get
                {
                    return this._IsSpanish;
                }
                set
                {
                    if ((this._IsSpanish != value))
                    {
                        this._IsSpanish = value;
                    }
                }
            }
           
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_DateOfService", DbType="DateTime")]
            public System.Nullable<System.DateTime> DateOfService
            {
                get
                {
                    return this._DateOfService;
                }
                set
                {
                    if ((this._DateOfService != value))
                    {
                        this._DateOfService = value;
                    }
                }
            }
           
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Worklevel", DbType="VarBinary(100)")]
            public System.Data.Linq.Binary Worklevel
            {
                get
                {
                    return this._Worklevel;
                }
                set
                {
                    if ((this._Worklevel != value))
                    {
                        this._Worklevel = value;
                    }
                }
            }
           
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_IsParkedChart", DbType="Bit")]
            public System.Nullable<bool> IsParkedChart
            {
                get
                {
                    return this._IsParkedChart;
                }
                set
                {
                    if ((this._IsParkedChart != value))
                    {
                        this._IsParkedChart = value;
                    }
                }
            }
           
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_HICN", DbType="VarChar(254)")]
            public string HICN
            {
                get
                {
                    return this._HICN;
                }
                set
                {
                    if ((this._HICN != value))
                    {
                        this._HICN = value;
                    }
                }
            }
           
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_PlanNumber", DbType="VarChar(254)")]
            public string PlanNumber
            {
                get
                {
                    return this._PlanNumber;
                }
                set
                {
                    if ((this._PlanNumber != value))
                    {
                        this._PlanNumber = value;
                    }
                }
            }
           
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ProviderName", DbType="VarChar(254)")]
            public string ProviderName
            {
                get
                {
                    return this._ProviderName;
                }
                set
                {
                    if ((this._ProviderName != value))
                    {
                        this._ProviderName = value;
                    }
                }
            }
           
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_PatientName", DbType="VarChar(254)")]
            public string PatientName
            {
                get
                {
                    return this._PatientName;
                }
                set
                {
                    if ((this._PatientName != value))
                    {
                        this._PatientName = value;
                    }
                }
            }
           
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Gender", DbType="VarChar(254)")]
            public string Gender
            {
                get
                {
                    return this._Gender;
                }
                set
                {
                    if ((this._Gender != value))
                    {
                        this._Gender = value;
                    }
                }
            }
           
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_EligibilityDate", DbType="DateTime")]
            public System.Nullable<System.DateTime> EligibilityDate
            {
                get
                {
                    return this._EligibilityDate;
                }
                set
                {
                    if ((this._EligibilityDate != value))
                    {
                        this._EligibilityDate = value;
                    }
                }
            }
           
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_HasChartNotes", DbType="Bit")]
            public System.Nullable<bool> HasChartNotes
            {
                get
                {
                    return this._HasChartNotes;
                }
                set
                {
                    if ((this._HasChartNotes != value))
                    {
                        this._HasChartNotes = value;
                    }
                }
            }
           
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ChartNotes", DbType="VarChar(4000)")]
            public string ChartNotes
            {
                get
                {
                    return this._ChartNotes;
                }
                set
                {
                    if ((this._ChartNotes != value))
                    {
                        this._ChartNotes = value;
                    }
                }
            }
        }

     

    Monday, June 21, 2010 7:53 PM
  • That seems autogenerated code so it should not be the problem...

    The problem is likely to be in the stored procedure, are you passing the exact parameter type it needs, to do what it does, could you put the code for the store procedure..

    I'm pretty sure is this, one of the parameters you are passing is a string and somewhere inside the procedure you need an byte[].

    Post it to be sure.  I'll take a look tomorrow.

    Regards

    Monday, June 21, 2010 8:28 PM
  • It is a lot of code in this proc so I don't think you really want me to post the whole thing.  I have included the client side code that calls the stored procedure and the function signature of the stored proc to show the values being passed.   Also one of the last things this stored proc does before returning results back to the client is populate a table with some records and that table always gets populated so that lets me know that the stored proc is not erroring out.  And I can definitely say that I am not using byte[] anywhere in the stored proc.  Judging by the stack trace info (included below) the error is being thrown somewhere within the process of sending/reading the records returned by the stored proc.

     

    Function Signature of the Stored Proc

        [Microsoft.SqlServer.Server.SqlProcedure]
        public static void GetChartsByCoder(string coderLoginName, int numChartsOnLaptop)

     

    Code calling the stored proc

            public Queue<GetChartsByCoderResult> GetChartsEligibleForDownload(string coderID, int numberChartsOnLaptop)
            {

                object[] tempCharts =
                    DbContext.GetChartsByCoder(coderID, numberChartsOnLaptop).ToArray<GetChartsByCoderResult>();

    //remaining code removed to keep listing short

            }

     

    NOTE: Error must be happening somewhere in the sending or receiving of the result set.  I say this because of the Read_GetChartsByCoderResult and ObjectReader`2.MoveNext() entries found in the stack trace.

       STACK TRACE
       ============
          at Read_GetChartsByCoderResult(ObjectMaterializer`1 )
          at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
          at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
          at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
          at CodeStarBL.ChartService.GetChartsEligibleForDownload(String coderID, Int32 numberChartsOnLaptop) in C:\Documents and Settings\BSmith\My Documents\Projects\CodeStar\CodeStarBL\ChartService.cs:line 33

    Tuesday, June 22, 2010 4:15 PM
  • My guess would be this, this store procedure give some rows of a table and then you try to cast each row to  GetChartsByCoderResultand that fails...

    Possible reason for that are

    1- The store procedure does not send a data that can be converted to GetChartsByCoderResult

    2- There is not a explicit conversion between the rows and GetChartsByCoderResult (1 and 2 are alike)

    3- There is a problem with something else...

    Now you could use linq or sql instead...

     

    Tuesday, June 22, 2010 4:49 PM
  • Could there be a data type compatibility problem between the SqlDbType.Bit fields and the related record.SetBoolean() calls I am using to populate the bit fields? (see below):

     

     

     private static void SendChartsBackToClient(List<ChartImage> charts)
     {
      SqlDataRecord record = new SqlDataRecord(
            new SqlMetaData("ChartImageID", SqlDbType.UniqueIdentifier),
            new SqlMetaData("ClientChartID", SqlDbType.UniqueIdentifier),
            new SqlMetaData("ScanDate", SqlDbType.DateTime),
            new SqlMetaData("ChartFileName", SqlDbType.VarChar, 254),
            new SqlMetaData("ChartDirectory", SqlDbType.VarChar, 254),
            new SqlMetaData("IsCodingRequired", SqlDbType.Bit),
            new SqlMetaData("IsSpanish", SqlDbType.Bit),
            new SqlMetaData("DateOfService", SqlDbType.DateTime),
            new SqlMetaData("WorkLevel", SqlDbType.VarChar, 100),
            new SqlMetaData("IsParkedChart", SqlDbType.Bit),
            new SqlMetaData("HICN", SqlDbType.VarChar, 254),
            new SqlMetaData("PlanNumber", SqlDbType.VarChar, 254),
            new SqlMetaData("ProviderName", SqlDbType.VarChar, 254),
            new SqlMetaData("PatientName", SqlDbType.VarChar, 254),
            new SqlMetaData("Gender", SqlDbType.VarChar, 254),
            new SqlMetaData("EligibilityDate", SqlDbType.DateTime),
            new SqlMetaData("HasChartNotes", SqlDbType.Bit),
            new SqlMetaData("ChartNotes", SqlDbType.VarChar, 4000)
            );
    
      SqlContext.Pipe.SendResultsStart(record);  
      
      foreach (ChartImage chart in charts)
      {
       Guid tempID = new Guid(chart.ChartImageID);
       record.SetGuid(0, tempID);
       tempID = new Guid(chart.ClientChartID);
       record.SetGuid(1, tempID);
       record.SetDateTime(2, chart.ScanDate);
       record.SetString(3, chart.DocumentName);
       record.SetString(4, chart.DocumentDirectory);   
       record.SetBoolean(5, chart.IsCodingRequired);
       record.SetBoolean(6, chart.IsSpanish);
       record.SetDateTime(7, chart.ImportDate);
       string workLevelDescription = Enum.GetName(typeof(WorkLevelCode), chart.WorkLevel);
       record.SetString(8, workLevelDescription);
       record.SetBoolean(9, chart.IsParkedChart);
       record.SetString(10, chart.HICN);
       record.SetString(11, chart.PlanNumber);
       record.SetString(12, chart.ProviderName);
       record.SetString(13, chart.PatientName);
       record.SetString(14, chart.Gender);
       record.SetDateTime(15, chart.EligibilityDate);
       record.SetBoolean(16, chart.HasChartNotes);
       record.SetString(17, chart.ChartNotes);
       SqlContext.Pipe.SendResultsRow(record);
      }
    
      SqlContext.Pipe.SendResultsEnd();  
     }

     

    Tuesday, June 22, 2010 4:50 PM
  • No, that seems fine, my question is about the mapping of the procedure, that might be the problem.

    http://msdn.microsoft.com/en-us/library/bb425822.aspx

    There are examples of that in this link under the invoking procedures part...

    PS: I assume you know that Bit is 0, 1 or null whereas bool is false or true and bool? is false, true or null

    Tuesday, June 22, 2010 6:06 PM
  • Hi,

     

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 

     

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, June 28, 2010 2:21 AM
    Moderator