Results 1 to 10 of 10

Thread: SQL Reader Problems

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    14

    SQL Reader Problems

    Hey guys, I'm pretty new to SQL and I'm having the following problem.

    I am creating a program in VB that looks through Multiple SQL tables for information. I have all the looping done to access all the tables, however, the SQL reader fails whenever I hit a table that doesn't have any information in the column I'm looking up. (These tables are constantly being modified so it may go from having data in the column one day, to the column being cleared the next)

    Is there a way to use the reader but instead of throwing exceptions (crashing the program), make the reader skip over tables that don't have anything in the column I'm searching under???

    Thanks in advance!!

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: SQL Reader Problems

    You can do it one of two ways. Handle it in your SELECT statement (Use isnull(field, 'default value')) or you can handle it in your app; like so:
    VB Code:
    1. Dim read As SqlClient.SqlDataReader
    2.             If Not read.IsDBNull(0) Then
    3.                 Myval = read.GetValue(0)
    4.             End If

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: SQL Reader Problems

    well if you insert proper try/catch exception handling then your program won't crash when it hits an exception..

    or you may not need exception handling, it may be that you just need to check the column to see if its null, and skip it if so.. but I am not sure if that is what you meant by "doesn't have any information in the column i'm looking up"

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    14

    Re: SQL Reader Problems

    Quote Originally Posted by kleinma
    well if you insert proper try/catch exception handling then your program won't crash when it hits an exception..

    or you may not need exception handling, it may be that you just need to check the column to see if its null, and skip it if so.. but I am not sure if that is what you meant by "doesn't have any information in the column i'm looking up"
    that is exactly what I need to do... the VB example above is still throwing the "Invalid attempt to read when no data is present" exception. I'll see what I can do with the SQL example...

  5. #5
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: SQL Reader Problems

    it sounds like there's nothing in your reader. Check .HasRows to see if anything was returned.

    Actually, you know what? Just post your reader code.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    14

    Re: SQL Reader Problems

    VB Code:
    1. sqlConn.Open()
    2.                 DatabaseCommand.Connection = sqlConn
    3.                 DatabaseCommand.CommandText = "SELECT Counter FROM " & tableList2(table).ToString & " WHERE Counter = (SELECT MAX(Counter) FROM " & tableList2(table).ToString & ") ;"
    4.                 Dim rdrMyReader As SqlDataReader
    5.                 rdrMyReader = DatabaseCommand.ExecuteReader
    6.                 rdrMyReader.Read()
    7.                 If Not rdrMyReader.IsDBNull(0) Then
    8.                     UpperBound = rdrMyReader.GetValue(0)
    9.                 End If
    10.                 sqlConn.Close()

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: SQL Reader Problems

    like seven said.. check for rows before reading..
    VB Code:
    1. sqlConn.Open()
    2.                 DatabaseCommand.Connection = sqlConn
    3.                 DatabaseCommand.CommandText = "SELECT Counter FROM " & tableList2(table).ToString & " WHERE Counter = (SELECT MAX(Counter) FROM " & tableList2(table).ToString & ") ;"
    4.                 Dim rdrMyReader As SqlDataReader
    5.                 rdrMyReader = DatabaseCommand.ExecuteReader
    6.                 if rdrMyReader.HasRows then
    7.                     rdrMyReader.Read()
    8.                     If Not rdrMyReader.IsDBNull(0) Then
    9.                         UpperBound = rdrMyReader.GetValue(0)
    10.                     End If
    11.                     sqlConn.Close()
    12.                 else
    13.                     'anything you may do when there are no rows
    14.                 end if

  8. #8
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: SQL Reader Problems

    VB Code:
    1. Try
    2.             sqlConn.Open()
    3.             DatabaseCommand.Connection = sqlConn
    4.             DatabaseCommand.CommandText = "SELECT TOP 1 Counter FROM " & tableList2(table).ToString & " ORDER BY counter DESC;"
    5.             Dim rdrMyReader As SqlClient.SqlDataReader
    6.             rdrMyReader = DatabaseCommand.ExecuteReader(CommandBehavior.SingleRow)
    7.             While rdrMyReader.Read
    8.                 If Not rdrMyReader.IsDBNull(0) Then
    9.                     UpperBound = rdrMyReader.GetValue(0)
    10.                 End If
    11.             End While
    12.         Catch ex As Exception
    13.             'Handle
    14.         Finally
    15.             If sqlConn.State = ConnectionState.Open Then
    16.                 sqlConn.Close()
    17.             End If
    18.         End Try
    Last edited by sevenhalo; Jul 14th, 2006 at 04:03 PM. Reason: Changed everything BUT the problem :P *Fixed*

  9. #9
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: SQL Reader Problems

    Or if I'm reading your code right, you can totally do away with the reader:
    VB Code:
    1. Try
    2.             sqlConn.Open()
    3.             DatabaseCommand.Connection = sqlConn
    4.             DatabaseCommand.CommandText = "SELECT TOP 1 Counter FROM " & tableList2(table).ToString & " ORDER BY counter DESC;"
    5.             UpperBound = DatabaseCommand.ExecuteScalar
    6.         Catch ex As Exception
    7.             'Handle
    8.         Finally
    9.             If sqlConn.State = ConnectionState.Open Then
    10.                 sqlConn.Close()
    11.             End If
    12.         End Try

  10. #10

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    14

    Re: SQL Reader Problems

    Quote Originally Posted by sevenhalo
    Or if I'm reading your code right, you can totally do away with the reader:
    VB Code:
    1. Try
    2.             sqlConn.Open()
    3.             DatabaseCommand.Connection = sqlConn
    4.             DatabaseCommand.CommandText = "SELECT TOP 1 Counter FROM " & tableList2(table).ToString & " ORDER BY counter DESC;"
    5.             UpperBound = DatabaseCommand.ExecuteScalar
    6.         Catch ex As Exception
    7.             'Handle
    8.         Finally
    9.             If sqlConn.State = ConnectionState.Open Then
    10.                 sqlConn.Close()
    11.             End If
    12.         End Try


    Thanks man! That worked out perfectly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width