Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with SqlDataReader null values in VB.net

I have the follwoing code that performs a query and returns a result. However, I looked around and found some examples to take care of null values but I get an error: "Invalid attempt to read when no data is present." I also got the error: "Conversion from type 'DBNull' to type 'Decimal' is not valid."

Can someone help me out with this code to prevent null values from crashing my program?

Private Sub EFFICIENCY_STACKRANK_YTD(ByVal EMPLOYEE As String)

    Dim queryString As String = "SELECT " & _
    " (SELECT CAST(SUM(TARGET_SECONDS) AS DECIMAL)/ CAST(SUM(ROUTE_SECONDS) AS DECIMAL) FROM dbo.APE_BUSDRIVER_MAIN WITH(NOLOCK) WHERE APE_AREA_OBJID = " & lblAreaOBJID.Text & " AND EMPLOYEE_NAME = '" & EMPLOYEE & "' AND YEAR_TIME = '" & cbYear.Text & "' AND ACTIVE = 1) AS RESULT1" & _
    " FROM dbo.APE_BUSDRIVER_MAIN "


    Using connection As New SqlConnection(SQLConnectionStr)
        Dim command As New SqlCommand(queryString, connection)
        connection.Open()
        Dim reader As SqlDataReader = command.ExecuteReader()

        If reader.Read Then
            RESULT1 = reader("RESULT1")
        Else
            RESULT1 = 0
        End If

    End Using
End Sub
like image 874
Joseph.Scott.Garza Avatar asked Dec 31 '13 19:12

Joseph.Scott.Garza


People also ask

How do I handle null in DataReader?

Handling Null column values using turnery operator: In this example I used turnery operator with DBNull. Value to check sql data reader columns value is null or not, if found null then column value assigned to some default value.

What is Sqldatareader in VB net?

DataReader is a readonly, forward only and connected recordset from the database. In DataReader, database connection is opened until the object is closed unlike DataSet. Using DataReader we can able to access one row at a time so there it is not required storing it in memory.

How does ado net handle null value?

For working with database ANSI SQL null values, use System. Data. SqlTypes nulls rather than Nullable. For more information on working with CLR value nullable types in Visual Basic see Nullable Value Types, and for C# see Nullable value types.

What is the role of data DataReader in VB net?

This component is used to set up a connection with a data source. A command is a SQL statement or a stored procedure used to retrieve, insert, delete or modify data in a data source. Data reader is used to retrieve data from a data source in a read-only and forward-only mode.


1 Answers

You have opened the reader, but have not asked it to actually read anything.

After this line:

Dim reader As SqlDataReader = command.ExecuteReader()

add

If reader.Read() Then

and wrap the result reading into this if statement, i.e.

If reader.Read() Then
    Dim index As Integer = reader.GetOrdinal("RESULT1")
    If reader.IsDBNull(index) Then
        RESULT1 = String.Empty
    Else
        RESULT1 = reader(index)
    End If
End If

Note that this works because your SQL should only return a single record. In the event that you were reading multiple records, you would need to call the Read statement in a loop until there were no more records, i.e.

Do While reader.Read()

Loop
like image 114
competent_tech Avatar answered Sep 19 '22 18:09

competent_tech