Jump to content

Recommended Posts

Posted

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;

 

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?

Posted
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.
Posted

@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. :)

Posted

reader is a variable associated to system.data.oledbcommand

 

I think the problem lies with the Try/Catch:

 

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.

Posted
Can you do anything else with the 'ex' variable in the exception to find out what exception is being thrown? Codes/descriptions etc?
Posted

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

 


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

   End While



Posted
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

Posted
reader is a variable associated to system.data.oledbcommand

 

I think the problem lies with the Try/Catch:

 

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:

If reader.Item <> "" then

to this:

If Not IsDBNull(reader.Item) Then

Posted

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!

 

Good idea try a ex.ToString method in a message box to find out more info

 

It was similar to this....

 

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....

 

msgbox(reader.item("item").ToString())

 

... got it working :)

Posted

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 :-)

Posted
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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...