Getting Error Variable is used before it has been assigned a value. A null reference exception could result at runtime.

Here is my code.
 Public Shared Function GetObsSNames(ByVal strObsStIDs As String) As String
            Dim strWhere As String = Append(strWhere, "ObsSID IN (" & strObsSIDs & ")", " AND ")
            Dim strSql As String = "SELECT * FROM ObsS WHERE " & strWhere
            Dim strSNames As String = ""
            Dim strTitle As String = ""
            Dim objSql As New SqlDataAccess(GetConnectionString(True), strSql, CommandType.Text)
            objSql.ExecuteReader()
            While objSql.Read
                strTitle = objSql.GetString("Title")
                strSNames = strSNames & " " & strTitle
            End While
            objSql.CloseAll()
            Return strSNames
        End Function
newtoperlpgmAsked:
Who is Participating?
 
ChloesDadCommented:
Change it to

Dim strWhere As String = "ObsSID IN (" & strObsSIDs & ")"

Open in new window


I suspect the original code was building up a complex where clause as the end AND is not needed either
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Dim strWhere As String = Append(strWhere, "ObsSID IN (" & strObsSIDs & ")", " AND ")

Open in new window

strWhere is used in the Append, and that makes no sense.
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
When you say that you have an error, please tell us on which line. We have to spend time on all the code while we could focus directly on the error if we knew where it occurs.

Also, strWhere ends the command (strSql), so the command is incomplete because it terminates abruptly with AND (unless Append adds something else).

Also, what is the type of
Dim objSql As (???) =  New SqlDataAccess(GetConnectionString(True), strSql, CommandType.Text)

Finally, you way of returning the data might be problematic. If a title contains spaces, it will be mixed up with everything else because the titles are separated by spaces in strNames. You should separate the results with a character that you are sure will not appear in any title. The pipe character "|" or a tabulation (vbTab) is often used in these situations.
0
 
newtoperlpgmAuthor Commented:
Changing it to Dim strWhere As String = "ObsSID IN (" & strObsSIDs & ")" worked.
Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.