• 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

    • This 2TB NVMe Gen4 SSD is priced just $94 and you also get a free 64GB UHS-I memory card by Sayan Sen A few days ago, we reported on Team Group's T-FORCE G50 4TB NVMe SSD that was up for grabs at just $200 thanks to a promo coupon. Sadly, that deal has expired although you can still WD's SN8100 (Gen5) and SN7100 (Gen4) offerings as they are still running the discount. If you don't have the budget for those or are shopping for lower capacity drives then Crucial's T500 Gen4 drive discount is still live, and you can get them for just $125. And while the G50 4TB deal has expired, Team Group is now offering its 2TB model at its lowest ever price and you also get a free Micro SD card with it. The Team Group G50 is also a TLC (triple level cell) NAND flash SSD, and thus the endurance on the T-FORCE SSD is quite good, as it is rated for 1300 TBW (terabytes written) for the 2TB variant. Its MTBF, or Mean Time Between Failure, is claimed at 3,000,000 hours. The operating temperature is 0~70 C. The G50 does not have a dedicated DRAM cache (only the G50 Pro SKUs have it), but since it is based on NVMe version 1.4 which supports HMB (host memory buffer) technology; thus, the drive can use system memory for caching. In terms of performance, Team Group promises sequential read and write speeds of up to 5000 MB/s and 4500 MB/s, respectively. However, the firm does not disclose random throughput metrics. Get the Team Group G50 at the link below (deal is said to be ending in less than 10 hours): Team Group T-FORCE G50 SSD (TM8FFE002T0C129) + Team Group 64GB Elite microSDXC UHS-I U3, V30, A1, Micro SD with SD Adapter, to 100MB/s (TEAUSDX64GIV30A103): $105.99 + $12 off with promo code SSETA665 (Shipped and Sold by Newegg US) This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • I know what you're getting at, but Microsoft themselves have said that the Xbox Ally specifically disables a bunch of stuff from Windows to improve performance and save 2GB of memory. And that special game mode is coming to Windows 11 next year.
    • It could be good old fashion neglect, lower priority focus, less resources then it use to vs active sabotage.
  • Recent Achievements

    • First Post
      Ian_ earned a badge
      First Post
    • Explorer
      JaviAl went up a rank
      Explorer
    • Reacting Well
      Cole Multipass earned a badge
      Reacting Well
    • Reacting Well
      JLP earned a badge
      Reacting Well
    • Week One Done
      Rhydderch earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      647
    2. 2
      ATLien_0
      268
    3. 3
      Michael Scrip
      218
    4. 4
      +FloatingFatMan
      184
    5. 5
      Steven P.
      145
  • Tell a friend

    Love Neowin? Tell a friend!