• 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

7 answers to this question

Recommended Posts

  • 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]

  • 0
  On 26/01/2012 at 18:22, paxa said:

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:

  • 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("@parm1lDbType.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
  • 0
  On 27/01/2012 at 20:34, apple jacks48 said:

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("@parm1lDbType.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

This topic is now closed to further replies.
  • Posts

    • How long until the Windows 11 hating bots come and say that Windows 11 committed atrocities against their family and their dog?
    • If you never consented to cookies (required), and you're blocking ad domains and you never see ads on Neowin (despite whitelisting) wouldn't you agree the adblock popup notice is doing what it is supposed to do?
    • It's just exactly what it looks like. The facial polygon count is low and rigid, and the background appears heavily blurred. You can clearly see jagged edges around the car window as it exits the tunnel. The character animations feel like they're from 2010. The Xbox Series S has performance similar to an RX 570 or GTX 1660, which is disappointing and contributes to holding back game development. The Switch GPU is roughly on par with a 2050 or 3050, which is even worse. Developers have often discussed the challenges and limitations of developing for outdated/low end hardware, noting how it can hold back progress. This isn't a new issue. I am sure we will hear about the "Challenges" they had to overcome here and sacrifices that had to be made for compatibility with the outdated/low end hardware.
    • Whether on Edge or Firefox, I don't get this prompt to accept cookies, because they're already whitelisted by the Cookie AutoDelete extension, both on Edge and Firefox. And, as I've already said, using Edge (yesterday), Neowin isn't whitelisted (I never use Edge), and I didn't get that pop-up. And this morning, after checking, still no pop-up, and everything is activated, the proof (screenshot), I have no ads on the right side. I must add that I block the following trackers Doubleclick, and Scorecardresearch, as well as a few other undesirable ones, via the Host file. Back to square one! After all, it's a simple click to continue, so I'll do with it. 🤷🏽‍♂️ Thank You
    • F'ing FIIINALLY! Much easier to ask people what their hardware is 🙏 and could also very well just say DDR4 or DDR5 without asking to bloat it too much. I'm also curious to see what it will say in More device info...
  • Recent Achievements

    • Week One Done
      jbatch earned a badge
      Week One Done
    • First Post
      Yianis earned a badge
      First Post
    • Rookie
      GTRoberts went up a rank
      Rookie
    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      400
    2. 2
      +FloatingFatMan
      178
    3. 3
      snowy owl
      171
    4. 4
      ATLien_0
      169
    5. 5
      Xenon
      134
  • Tell a friend

    Love Neowin? Tell a friend!