Link to home
Start Free TrialLog in
Avatar of newtoperlpgm
newtoperlpgmFlag for United States of America

asked on

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
Avatar of Qlemo
Qlemo
Flag of Germany image

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.
ASKER CERTIFIED SOLUTION
Avatar of ChloesDad
ChloesDad
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of newtoperlpgm

ASKER

Changing it to Dim strWhere As String = "ObsSID IN (" & strObsSIDs & ")" worked.
Thanks.