These two methods below are similar, except one handles null values and the other does not. To handle null values, it uses SqlString type and checks the "get_IsNull" property.
Why might the first one be causing the error "A .NET Framework error occurred during execution of user-defined routine or aggregate "CheckMailingAddress": ." when run inside SQL CLR, while the second one does not?
In particular, the TSQL error is "Msg 10329, Level 16, State 49, Line 1 .Net Framework execution was aborted."
.method public hidebysig static bool CheckMailingAddress(valuetype [System.Data]System.Data.SqlTypes.SqlString param0) cil managed
{
.maxstack 8
L_0000: ldarga.s param0
L_0002: nop
L_0003: nop
L_0004: call instance bool [System.Data]System.Data.SqlTypes.SqlString::get_IsNull()
L_0009: brfalse L_0010
L_000e: ldc.i4.1
L_000f: ret
L_0010: ldarga.s param0
L_0012: nop
L_0013: nop
L_0014: call instance string [System.Data]System.Data.SqlTypes.SqlString::get_Value()
L_0019: call class DatabaseValues.MailingAddress DatabaseValues.MailingAddress::op_Explicit(string)
L_001e: pop
L_001f: ldc.i4.1
L_0020: ret
}
.method public hidebysig static bool CheckMailingAddress(string param0) cil managed
{
.maxstack 8
L_0000: ldarg.0
L_0001: call class DatabaseValues.CheckMailingAddress DatabaseValues.CheckMailingAddress::op_Explicit(string)
L_0006: pop
L_0007: ldc.i4.1
L_0008: ret
}
Keep in mind, the MSIL is correct as as far as I know, because both of these methods work when called in a standalone app. It's only when called inside SQL CLR that the first of the two crashes. In the SQL CLR, the function is defined with the "nvarchar(4000)" type, which should play nice with SqlString as far as I know.
I could probably implement the first method using "string" as well and still do the null check, but it uses SqlString to take advantage of the INullable interface properties "IsNull" and "Value", because it's part of a generic code generator where other Sql* types could be used.
SIMPLE PROBLEM SUMMARY:
For those distracted by the MSIL in the method body; ignore it. I recompiled the functions to do nothing at all, and my point is that when "SqlString", rather than "string", is the input type, the CLR blows up and terminates with no error message, and the return value is NULL rather than TRUE.
//Crashes when input parameter is "SqlString"
.method public hidebysig static bool CheckMailingAddress(valuetype [System.Data]System.Data.SqlTypes.SqlString param0) cil managed
{
.maxstack 8
L_001f: ldc.i4.1
L_0020: ret
}
//Doesn't Crash when input parameter is "string"
.method public hidebysig static bool CheckMailingAddress(string param0) cil managed
{
.maxstack 8
L_0007: ldc.i4.1
L_0008: ret
}
