473,326 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

dbnull error with mysql....

Hello,

I am writing a vb.net app.Using MySql db with the corelabs connector.

I am getting the following error when attempting to read values from a
datareader:
Cast from type 'DBNull' to type 'String' is not valid.

This error occurs on a field that DOES have data in it, but I can't
figure out why! Here is a sample of the code I am using:

strSql = ("Select * from pt_info where MRN='" & Form3.intNewMRN & "'")
myCmd = New MySqlCommand(strSql, MyCon)
myReader = myCmd.ExecuteReader()
While myReader.Read()
txtFname.Text = myReader("fname")
txtLname.Text = myReader("lname")
txtMiddle.Text = myReader("middle")
txtAddress.Text = myReader("address")
txtAddress2.Text = myReader("address2")
txtCity.Text = myReader("citycode")
txtZip.Text = myReader("zip") <--DBnull error here
.....more of the same
....
end while
Looking at the db, there data present in the zip field, but I cannot
seem to pull the info out.
Can anyone help with this problem????

TIA,
Daniel
Nov 21 '05 #1
8 3303
Hi, You can use the MySQL ifnull() function.

more doc about ifnull()
http://dev.mysql.com/doc/mysql/en/co...functions.html

example:

strSQL = "SELECT
ifnull(fname, ""),
ifnull(lname, ""),
ifnull(address, ""),
ifnull(address2, "") .....
from pt_info where MRN='" & Form3.intNewMRN

hope that helps.

Nov 21 '05 #2
Thanks for the response.

Yes, I do what to handle null values, but my problem is that the
field(s) in question are NOT null. Somehow, for some reason beyond my
feeble brain, I can't get the data out. Just keep getting the dbnull
error. If I comment out the line of code that gets the error, I then
get the dbnull error on the one before it. I am totally lost. Using a
datareader. Maybe it has something to do with forward only reading or
some odd thing like that??!!?

Any Help would be greatly apprectiated,

Daniel

On 25 Aug 2005 12:26:46 -0700, "Adael" <ad*****@gmail.com> wrote:
Hi, You can use the MySQL ifnull() function.

more doc about ifnull()
http://dev.mysql.com/doc/mysql/en/co...functions.html

example:

strSQL = "SELECT
ifnull(fname, ""),
ifnull(lname, ""),
ifnull(address, ""),
ifnull(address2, "") .....
from pt_info where MRN='" & Form3.intNewMRN

hope that helps.


Nov 21 '05 #3
Perhaps your first record being read contains all DBNull's. You might try
using a DataSet and see if the first record is in fact all Nulls.
--
Dennis in Houston
"wink martindale" wrote:
Thanks for the response.

Yes, I do what to handle null values, but my problem is that the
field(s) in question are NOT null. Somehow, for some reason beyond my
feeble brain, I can't get the data out. Just keep getting the dbnull
error. If I comment out the line of code that gets the error, I then
get the dbnull error on the one before it. I am totally lost. Using a
datareader. Maybe it has something to do with forward only reading or
some odd thing like that??!!?

Any Help would be greatly apprectiated,

Daniel

On 25 Aug 2005 12:26:46 -0700, "Adael" <ad*****@gmail.com> wrote:
Hi, You can use the MySQL ifnull() function.

more doc about ifnull()
http://dev.mysql.com/doc/mysql/en/co...functions.html

example:

strSQL = "SELECT
ifnull(fname, ""),
ifnull(lname, ""),
ifnull(address, ""),
ifnull(address2, "") .....
from pt_info where MRN='" & Form3.intNewMRN

hope that helps.


Nov 21 '05 #4
Hi Daniel,

Are you sure that the cases of the datanames are completly right, in
windowforms these names are case sensitive.

I hope this helps,

Cor
Nov 21 '05 #5
Dennis,

Thanks for the response.
I am only returning one record with this query. I am able to get some
values, usually the first three or four, from the reader, but then I
start getting dbnull errors. I am certain the fields contain data. I
even ran the query in the mySQL query analyzer, and it fails there as
well. Help!!

On Thu, 25 Aug 2005 17:04:03 -0700, Dennis
<De****@discussions.microsoft.com> wrote:
Perhaps your first record being read contains all DBNull's. You might try
using a DataSet and see if the first record is in fact all Nulls.


Nov 21 '05 #6
Cor,

Thanks for the repy. Yes, I am certain the case used is correct. Still
cannot figure out why I am getting the "cast from DBNull to String"
error. There is data in the fields! Help!

On Fri, 26 Aug 2005 08:02:02 +0200, "Cor Ligthert [MVP]"
<no************@planet.nl> wrote:
Hi Daniel,

Are you sure that the cases of the datanames are completly right, in
windowforms these names are case sensitive.

I hope this helps,

Cor


Nov 21 '05 #7
Okay,

I am no longer getting dbnull errors (for some odd reason)
But now, I have a new problem:

Here is a code snippet:

strSql = ("Select * from pt_info where MRN ='" & Form3.intNewMRN &
"'")
myCmd = New MySqlCommand(strSql, myCon3)
myReader3 = myCmd.ExecuteReader
Do While myReader3.Read()
txtTest.Text &= myReader3("fname")
txtTest.Text &= myReader3("lname")
txtTest.Text &= myReader3("middle")
txtTest.Text &= myReader3("address") <---stops here
txtTest.Text &= myReader3("address2")
txtTest.Text &= myReader3("citycode")
txtTest.Text &= myReader3("zipcode")
txtTest.Text &= myReader3("zip4")
txtTest.Text &= myReader3("state_code")
Loop

Now, I cannot get any value from the db after address!?! These fields
contain data, but I can't get them out! I have deleted and rebuilt the
table to no avail. I am completely baffeled!

Help, oh wise ones!!
Wink
Nov 21 '05 #8
Wink,

Did you try it with a
\\\
If Not x Is Nothing then
move
Else
y = ""
End if
//

In your program,

Cor
"wink martindale" <wi**@martindale.com> schreef in bericht
news:hv********************************@4ax.com...
Cor,

Thanks for the repy. Yes, I am certain the case used is correct. Still
cannot figure out why I am getting the "cast from DBNull to String"
error. There is data in the fields! Help!

On Fri, 26 Aug 2005 08:02:02 +0200, "Cor Ligthert [MVP]"
<no************@planet.nl> wrote:
Hi Daniel,

Are you sure that the cases of the datanames are completly right, in
windowforms these names are case sensitive.

I hope this helps,

Cor

Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: jim | last post by:
This line of code returns error 13, cast from 'DBNull' to type 'String' is not valid If IsDBNull(Clinics.Clinics.Item(A).Workphone) The <other code End I Clinics.Clinics is a dataset that...
5
by: DraguVaso | last post by:
Hi, Something I don't understand about a Typed DataSet: When a value in the DataSet is DBNull, it throws this error: "Cannot get value because it is DBNull". But aren't Typed DataSets...
2
by: Filipe Cristóvão | last post by:
Hi, I know that this is a newbie's question, but I need to know how i do to know if a field in a table is DBNull. If I try to write (with response.write) the field, it gives me an error: "Cast...
11
by: Patrick.O.Ige | last post by:
When i try and use this (Where Unit is a column in my Table):- If Unit Is DBNull.Value Then Return "1" Else Return "2" End If I always have 2 returned! Even when Unit is NULL! I want a...
8
by: MattB | last post by:
Hello. I have a vb.net (asp.net) application that uses ado.net datasets. At one point, I need to check a text field in a DataTable to see if there's any text in it before performing text operations...
6
by: tshad | last post by:
I have a value coming from my Object (or it could also be from a SqlDbReader) where I need to test for DBNull and 0. I tried to do it in one call: if (not (newPosition.ReportsTo is...
19
by: Dave | last post by:
If Iwant to check if dataset1.SelectQuery1.column1 == System.DBNull.Value. How do I do this? What I wrote above will give an error. -- L. A. Jones
6
by: scott ocamb | last post by:
Hello, I have a function that expects the following parms. How can I handle null values. For example, if FirstName comes through as null, how can i propogate that null value to the stored...
2
seshu
by: seshu | last post by:
Hy Everybody how to use Dbnull in vb.net yes guys just as i know where are all the placeses there could be a null value i am finding out them first and then instead of writing one command strin i am...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.