• 0

how to check if datareader is null or not [VB]


Question

hey

how to check if DataReader is null or not in VB.net??

this is my code

   
Function GetBrandID(ByVal BrandName As String) As Integer
	    Dim rdr1 As MySqlDataReader
	    Dim cmd1 As MySqlCommand
	    cmd1 = New MySqlCommand("select BrandID from Brand where Des like'" & BrandName & "%'", conTmp)
	    rdr1 = cmd1.ExecuteReader
	    rdr1.Read()
	    If Not rdr1 Is Nothing Then			  'this doesn't work
		    Dim ID As Integer = rdr1(0)
		    rdr1.Close()
		    Return ID
	    Else
	    End If
  End Function

plz help

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

why not try this:

if (rdr1.hasrows)

or try this:

rdr.read()

any of them should do the trick, so just adjust your code accordingly

Link to comment
Share on other sites

  • 0

Function GetBrandID(ByVal BrandName As String) As Integer
Dim rdr1 As MySqlDataReader
Dim ID As Integer = -1
Dim cmd1 As MySqlCommand
cmd1 = New MySqlCommand("select BrandID from Brand where Des like'" & BrandName & "%'", conTmp) 'I should slap myself for using a LIKE on a query where I only want one row, I should SHOOT myself for not using a procedure...
rdr1 = cmd1.ExecuteReader
If rdr1.HasRows Then
rdr1.Read()
ID = rdr1.Item(0).value.ToInt32
End If

Return ID 'Now returns a value on all code paths; -1 would indicate an error
End Function
[/CODE]

Link to comment
Share on other sites

  • 0

why not try this:

if (rdr1.hasrows)

or try this:

rdr.read()

any of them should do the trick, so just adjust your code accordingly

didnt knew read() is a function and it returns a Boolean value (must read MSDN documentation :ermm: ) . it works like a charm .thnx :punk:

Link to comment
Share on other sites

  • 0

If you are only returning one value in your query, you can use ExecuteScalar instead of using a data reader. Also, you should use a parameterized query. If your "BrandName" variable has a quote in it the query will cause an unhandled exception. You can code something like this:



Dim objTemp As Object
Dim cmd As New SqlCommand

With cmd
.Connection = conTmp
.CommandText = "SELECT BrandID FROM Brand WHERE Des LIKE @parm1"
.Parameters.Add("@parm1", SqlDbType.NVarChar).Value = BrandName & "%"
objTemp = .ExecuteScalar
End With

If IsDBNull(objTemp) OrElse objTemp Is Nothing OrElse IsNumeric(objTemp) = False Then
Return -1
Else
Return objTemp
End If

[/CODE]

  • Like 1
Link to comment
Share on other sites

  • 0

If you are only returning one value in your query, you can use ExecuteScalar instead of using a data reader. Also, you should use a parameterized query. If your "BrandName" variable has a quote in it the query will cause an unhandled exception. You can code something like this:



Dim objTemp As Object
Dim cmd As New SqlCommand

With cmd
.Connection = conTmp
.CommandText = "SELECT BrandID FROM Brand WHERE Des LIKE @parm1"
.Parameters.Add("@parm1", SqlDbType.NVarChar).Value = BrandName & "%"
objTemp = .ExecuteScalar
End With

If IsDBNull(objTemp) OrElse objTemp Is Nothing OrElse IsNumeric(objTemp) = False Then
Return -1
Else
Return objTemp
End If

[/CODE]

didnt knew that. thnx

Link to comment
Share on other sites

This topic is now closed to further replies.