Bug #56918 Reader.getstring fails on null value
Submitted: 22 Sep 2010 7:04 Modified: 22 Sep 2010 7:32
Reporter: Olivier Olivier Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.2.4 OS:Windows
Assigned to: CPU Architecture:Any

[22 Sep 2010 7:04] Olivier Olivier
Description:
Hi, 

I want to find max value from a table with a sql query like "Select max(id) from <table>;" My problem is that when the table is empty, this query returns one row with max='null'. So reader.read is true when reader.getstring(0) generates error. I tried to test isdbnull(reader.getstring(0)) but the error is still generated.

Am I doing wrong ? Is there another way of doing that ?

Thanks.

How to repeat:
        Try
            Dim cmd As New MySqlCommand(sql, cnx)
            Dim reader As MySqlDataReader
            reader = Nothing
            reader = cmd.ExecuteReader()
            If reader.HasRows Then
                reader.Read()
                If IsDBNull(reader.GetString(0)) Then ' Catch on "reader.getstring")
                    id = "0"
                Else
                    id = reader.GetString(0)
                End If
            End If
            If Not reader Is Nothing Then reader.Close()
        Catch ex As Exception
[22 Sep 2010 7:16] Olivier Olivier
Mistake. Right syntax is 

If reader.HasRows Then
                reader.Read()
                If reader.IsDBNull(0) Then
                    id = "0"
                Else
                    id = reader.GetString(0)
                End If
            End If