• 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

    • The Irony... China wouldn't be what it is today without Apple 😂
    • Microsoft makes it easier to find PC specs in Windows 11 Settings by Taras Buria Windows 11 has already received several improvements that make it easier to learn about your computer's specifications. Recently, Microsoft released Spec Cards for the System > About section, which provide basic information about the PC's main components, such as processor, memory, storage, graphics card, and video memory. Now, the Settings app is getting a new way to find your device info. Microsoft wants to display basic device information right on the Home page of the Settings app. The latest preview builds from the Dev and Beta Channels introduced a new "Your device info" card for the Settings' Home page. It displays specs like processor name and speed, graphics card and the amount of video memory, storage, and RAM. The card also has a link to the "About" section, where you will find more information about your computer, its Windows edition, product ID, and the recently introduced FAQ section that answers common hardware-related questions. The "Your device info" card joins the existing cards on the Settings app's home page. While the section offers useful information like quick access to Bluetooth devices, Wi-Fi, personalization, and recommended settings, users received it with mixed reactions, as many considered it another way for Microsoft to promote its services and subscriptions like Microsoft 365, OneDrive, and Game Pass (seriously, who thinks about Game Pass when opening Settings?). Now, the Settings' Home page is a bit more useful, as it saves you a few clicks when checking your computer's specs. If you want to test the new "Your device info" card, update your PC to build 26200.5622 or newer (Dev Channel). Just keep in mind that Microsoft is rolling it out gradually, and it requires signing in with a Microsoft Account in the United States. Other changes in build 26200.5622 include a new Settings section for Quick Machine Recovery, widget improvements, more app recommendations in the "Open with" dialog, and more. Check out the full release notes here.
    • Ponies will finally have good games to play after replaying Last of Us for the 100th time. Oh and I lied, Silent Hill f looks pretty great too, but we already knew about that.
  • 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
      397
    2. 2
      +FloatingFatMan
      177
    3. 3
      snowy owl
      170
    4. 4
      ATLien_0
      167
    5. 5
      Xenon
      134
  • Tell a friend

    Love Neowin? Tell a friend!