I have written a CLR stored procedure which I call through code in MS Access.
It throws a Null Reference Exception. I guess it must be due to one of two variables I use inside the CLR procedure, for both of which the Refactor gives me a message 'Variable is used before it has been assigned a value. A null reference exception could result at runtime'.
I have assigned values to these variables and it worked fine for other parameters I executed the procedure with. But I changed the parameters and now I get the error.
Does anyone know why this may be happening?
Below I give you just the lines of the code where these variables are mentioned:
Dim emailBody As String
SQLcmd.CommandText = "SELECT fld_content FROM dbo.tbl_email_content WHERE fld_content_id = " & CStr(contID)
SQLcmd.CommandType = CommandType.Text
SQLcmd.Connection = SQLConn
SQLConn.Open()
RD = SQLcmd.ExecuteReader()
If RD.Read() Then
emailBody = RD(0).ToString
End If
SQLConn.Close()
SQLcmd.CommandText = "SELECT dbo.tbl_Persons.fld_prosfonisi, dbo.tbl_Persons.fld_pros, dbo.tbl_Companies.fld_company_formal_name " & _
"FROM dbo.itbl_company_person INNER JOIN " & _
"dbo.tbl_Persons ON dbo.itbl_company_person.fld_person_id = dbo.tbl_Persons.fld_person_id INNER JOIN " & _
"dbo.tbl_sent_history ON dbo.tbl_Persons.fld_person_id = dbo.tbl_sent_history.fld_person_id INNER JOIN " & _
"dbo.tbl_Companies ON dbo.itbl_company_person.fld_company_id = dbo.tbl_Companies.fld_company_id " & _
"WHERE (dbo.tbl_sent_history.fld_mass_sendID = '" & sentID & "')"
SQLcmd.CommandType = CommandType.Text
SQLcmd.Connection = SQLConn
SQLConn.Open()
RD = SQLcmd.ExecuteReader()
RD.Read()
tmpEmailBody = emailBody 'This line is where I get the message
emailBody = Replace(tmpEmailBody, "!@#$$#@!", RD(0).ToString)
SQLConn.Close()
And this one's for the second variable (an alternate view for an e-mail to be sent):
Dim htmlView As AlternateView
htmlView = AlternateView.CreateAlternateViewFromString(emailBody, System.Text.Encoding.UTF8, "text/html")
Mail.AlternateViews.Add(htmlView)
htmlView.Dispose() 'This is where I get the error
It throws a Null Reference Exception. I guess it must be due to one of two variables I use inside the CLR procedure, for both of which the Refactor gives me a message 'Variable is used before it has been assigned a value. A null reference exception could result at runtime'.
I have assigned values to these variables and it worked fine for other parameters I executed the procedure with. But I changed the parameters and now I get the error.
Does anyone know why this may be happening?
Below I give you just the lines of the code where these variables are mentioned:
Dim emailBody As String
SQLcmd.CommandText = "SELECT fld_content FROM dbo.tbl_email_content WHERE fld_content_id = " & CStr(contID)
SQLcmd.CommandType = CommandType.Text
SQLcmd.Connection = SQLConn
SQLConn.Open()
RD = SQLcmd.ExecuteReader()
If RD.Read() Then
emailBody = RD(0).ToString
End If
SQLConn.Close()
SQLcmd.CommandText = "SELECT dbo.tbl_Persons.fld_prosfonisi, dbo.tbl_Persons.fld_pros, dbo.tbl_Companies.fld_company_formal_name " & _
"FROM dbo.itbl_company_person INNER JOIN " & _
"dbo.tbl_Persons ON dbo.itbl_company_person.fld_person_id = dbo.tbl_Persons.fld_person_id INNER JOIN " & _
"dbo.tbl_sent_history ON dbo.tbl_Persons.fld_person_id = dbo.tbl_sent_history.fld_person_id INNER JOIN " & _
"dbo.tbl_Companies ON dbo.itbl_company_person.fld_company_id = dbo.tbl_Companies.fld_company_id " & _
"WHERE (dbo.tbl_sent_history.fld_mass_sendID = '" & sentID & "')"
SQLcmd.CommandType = CommandType.Text
SQLcmd.Connection = SQLConn
SQLConn.Open()
RD = SQLcmd.ExecuteReader()
RD.Read()
tmpEmailBody = emailBody 'This line is where I get the message
emailBody = Replace(tmpEmailBody, "!@#$$#@!", RD(0).ToString)
SQLConn.Close()
And this one's for the second variable (an alternate view for an e-mail to be sent):
Dim htmlView As AlternateView
htmlView = AlternateView.CreateAlternateViewFromString(emailBody, System.Text.Encoding.UTF8, "text/html")
Mail.AlternateViews.Add(htmlView)
htmlView.Dispose() 'This is where I get the error