My coworker wrote a package in VB.NET to manipulate strings. I have been asked to implement them through CLR. The package accesses a reference database on the same server as the CLR dll file deployed to standardize the strings. I imported the package to my CLR code to create the functions for SQL Server.
After I deploy the dll file to sql server, the functions work fine except occasionally I get the error
System.NullReferenceException: Object reference not set to an instance of an object.
I have to execute my query several times to make it work. It seems the table is locked or sleeping. My query is like
SELECT EMAIL_ADDRESS, dbo.ufn_getEmailDomain(Email_Address) AS EDOMAIN FROM CONTACT_TEMP
WHERE Email_Address IS NOT NULL AND Email_Address<>”
dbo.ufn_getEmailDomain is the CLR function.
There is no online access to this server at all. I searched for a while and couldn’t find why this error comes up occasionally or how to fix it.
Your feedback is greatly appreciated.
my CLR functions here.
<Microsoft.SqlServer.Server.SqlFunction(DataAccess:=DataAccessKind.Read)> _
Public Shared Function ufn_getEmailDomainSLD(ByVal email As String) As String
If email Is Nothing Then
Return Nothing
End If
Dim de As New DataEmail(email)
Dim dm As New DataDomain
Dim emailDomain As String
dm = de.Domain
emailDomain = dm.SLD
Return emailDomain
End Function
I found the reason eventually. In the connection string in the CLR code, I should set ENLIST=FALSE because it connects to a different database. Once I set ENLIST=FALSE, the problem seems to be fixed.