+ Post New Thread
Results 1 to 11 of 11
Coding Thread, vb.net reader.item help in Coding and Web Development; Hi gang! I'm creating an app in vb.net 2008 that uses an MS Access db. I'm using the reader to ...
  1. #1

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241

    vb.net reader.item help

    Hi gang!

    I'm creating an app in vb.net 2008 that uses an MS Access db. I'm using the reader to get data from a table, something like this;

    Code:
    If reader.HasRows Then
        While reader.Read
            msgbox(reader.item("name_of_field_in_db"))
        End While
    End If
    If the field has something in it, it will happily show the message box. But if the data is empty then it errors (I have the reader code in a try statement so the catch ex part is executed for some reason?!).

    Any ideas how to get it NOT to error even when the data is empty?

  2. #2

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,406
    Thank Post
    639
    Thanked 961 Times in 661 Posts
    Blog Entries
    2
    Rep Power
    324
    Something like If reader.item("foo") Is Not Null Then....? Basically just use VB code to check if it's set/empty/null before doing something with it. What type of object is 'reader' anyway? The API docs for that object should tell you what methods are available for checking these sort of things.

  3. #3

    bossman's Avatar
    Join Date
    Nov 2005
    Location
    England
    Posts
    3,915
    Thank Post
    1,189
    Thanked 1,063 Times in 754 Posts
    Rep Power
    329
    @Hightower:

    Not a coder myself but I would have thought that if there is a Null Field (no data in the database field) then obviously the program will return an error what you will need to add I suppose is an error catch line which tells the prog what to do when no data is found.

    Can't tell you how but logic would presume that it would work this way.

  4. #4

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241
    reader is a variable associated to system.data.oledbcommand

    I think the problem lies with the Try/Catch:

    Code:
    Try
        While reader.read
            If reader.Item <> "" then
                msgbox(reader.item("field_here"))
            Else
                msgbox("THIS FIELD IS EMPTY!")
            End If
        End While
    Catch ex As Exception
        msgbox("SOMETHING HAS GONE WRONG!")
    End Try
    If the field isn't empty it shows the field, otherwise it shows the "SOMETHING HAS GONE WRONG" error instead of the "THIS FIELD IS EMPTY ERROR" that it should display.

  5. #5

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,406
    Thank Post
    639
    Thanked 961 Times in 661 Posts
    Blog Entries
    2
    Rep Power
    324
    Can you do anything else with the 'ex' variable in the exception to find out what exception is being thrown? Codes/descriptions etc?

  6. #6
    Jamo's Avatar
    Join Date
    Jan 2009
    Posts
    1,350
    Thank Post
    66
    Thanked 175 Times in 147 Posts
    Rep Power
    60
    Does it show all the fields up to the last field? If it is reading past the end of the database then it will give an exception.

    If its not that you could try the catch statement when reading the db each loop rather than just giving it one chance to fail. A fall back could then be used in the app so that even after a failed read you could still read to the end of the database file rather than it stopping half way through due to an error.

    eg

    Code:
     
    Try
        While reader.read
            
            Try
                   msgbox(reader.item("field_here"))
            Catch ex As Exception
                   msgbox("!ERROR!")
            End Try
     
        End While

  7. #7
    Jamo's Avatar
    Join Date
    Jan 2009
    Posts
    1,350
    Thank Post
    66
    Thanked 175 Times in 147 Posts
    Rep Power
    60
    Quote Originally Posted by webman View Post
    Can you do anything else with the 'ex' variable in the exception to find out what exception is being thrown? Codes/descriptions etc?
    Good idea try a ex.ToString method in a message box to find out more info

  8. #8

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,452
    Thank Post
    1,439
    Thanked 1,170 Times in 798 Posts
    Rep Power
    707
    Quote Originally Posted by Hightower View Post
    reader is a variable associated to system.data.oledbcommand

    I think the problem lies with the Try/Catch:

    Code:
    Try
        While reader.read
            If reader.Item <> "" then
                msgbox(reader.item("field_here"))
            Else
                msgbox("THIS FIELD IS EMPTY!")
            End If
        End While
    Catch ex As Exception
        msgbox("SOMETHING HAS GONE WRONG!")
    End Try
    If the field isn't empty it shows the field, otherwise it shows the "SOMETHING HAS GONE WRONG" error instead of the "THIS FIELD IS EMPTY ERROR" that it should display.
    Try changing this line:
    Code:
    If reader.Item <> "" then
    to this:
    Code:
    If Not IsDBNull(reader.Item) Then

  9. #9

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241
    Found the problem - bit of a school boy doh! Helps when you take a step back and think logically instead of staring at the problem!

    Quote Originally Posted by Jamo View Post
    Good idea try a ex.ToString method in a message box to find out more info
    It was similar to this....

    Code:
    msgbox(reader.item("item"))
    ... didn't seem to be in the right format, so simply changing that to force it to read it as a string....

    Code:
    msgbox(reader.item("item").ToString())
    ... got it working

  10. #10

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,156
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    124
    Good to see you've got it working but, as @jamo says, your try/catch are in the wrong place. Remember you can have more than one of them but the way you had it, you're saying that if anything goes wrong anywhere between try and catch then you jump out of the section to the catch.

    don't know if you started programming with VB but I'm guessing you used to do "on error resume next" - with that, you would have got to your "error handler". Try Catch is much more flexible but needs a different approach :-)

  11. #11

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241
    Quote Originally Posted by srochford View Post
    Good to see you've got it working but, as @jamo says, your try/catch are in the wrong place. Remember you can have more than one of them but the way you had it, you're saying that if anything goes wrong anywhere between try and catch then you jump out of the section to the catch.

    don't know if you started programming with VB but I'm guessing you used to do "on error resume next" - with that, you would have got to your "error handler". Try Catch is much more flexible but needs a different approach :-)
    Yeah - I started with VB6 and used to use On Error GOTO..... Will have to read up on try/catch - but I struggle to find any examples of connecting to an MS Access database, even in books. And from what I've gathered so far it's so much different to connect to MS Access in .net than VB6.

SHARE:
+ Post New Thread

Similar Threads

  1. Recommend a Wireless Barcode Reader for Eclipse.Net
    By robknowles in forum Hardware
    Replies: 10
    Last Post: 27th June 2014, 03:09 AM
  2. [Website] Was/Is this the most expensive item on Amazon?
    By tech_guy in forum Jokes/Interweb Things
    Replies: 5
    Last Post: 7th May 2010, 08:11 AM
  3. the spelling in this item cannot be checked. try again later
    By danrhodes in forum Windows Server 2000/2003
    Replies: 1
    Last Post: 2nd April 2010, 04:07 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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