quick fix nulls

G

Guest

I know the reason why I get an error when the fields are set to null, but I'm working with old data that contains them. Any quick fixes? was hoping to avoid writing out every field in sql statement for ISNULL and avoid the long check on the make equal to value. Ideas?

Sql statement (I know I could use parameter):
"SELECT * FROM COMPANY_INFO WHERE COMPANY_ID = '" & usercompanyid & "'"
Setting value example:
txtcompanyid.Text = "" & memberdata("COMPANY_ID")

Thanx!
 
D

Dale

How are you retrieving the data? Are you using a DataSet, a reader? or
other means?

Maybe it doesn't matter much because you're really just stuck with using
some conditional checking. A common method I use for strings is, assuming
dr is a DataRow from my DataSet.Table

myString = dr.IsNull("myColumn") ? String.Empty : dr["myColumn"];

and for int:

myInt = dr.IsNull("myColumn2") ? 0 : dr["myColumn2"];

There are a lot of reasons for using nulls in data. It's not just a
leftover condition of "old data". For instance, dates that haven't been
assigned. You don't want to have to compare every date against January 1,
1753.

It's best to test for null on every field that allows nulls because someone
somewhere and somehow will get a null in even if you try to code them out.

Hope this helps,

Dale Preston
MCAD, MCSE, MCDBA


Chris said:
I know the reason why I get an error when the fields are set to null, but
I'm working with old data that contains them. Any quick fixes? was hoping to
avoid writing out every field in sql statement for ISNULL and avoid the long
check on the make equal to value. Ideas?
 
H

Hans Kesting

Chris said:
I know the reason why I get an error when the fields are set to null, but I'm working with old data that contains them. Any quick
fixes? was hoping to avoid writing out every field in sql statement for ISNULL and avoid the long check on the make equal to value.
Ideas?
Sql statement (I know I could use parameter):
"SELECT * FROM COMPANY_INFO WHERE COMPANY_ID = '" & usercompanyid & "'"
Setting value example:
txtcompanyid.Text = "" & memberdata("COMPANY_ID")

Thanx!

If you use
txtcompanyid.Text = memberdata("COMPANY_ID").ToString()
you should have less 'problems' with a DbNull value: a "ToString()" of DbNull
(or DbNull.Value to be exact) gives an empty string.

If the value is "null" ("Nothing" in vb) this will not work, but a database "null"
is signalled by DbNull.Value.

Hans Kesting
 
G

Guest

Do you know the VB equivelant?

Dale said:
How are you retrieving the data? Are you using a DataSet, a reader? or
other means?

Maybe it doesn't matter much because you're really just stuck with using
some conditional checking. A common method I use for strings is, assuming
dr is a DataRow from my DataSet.Table

myString = dr.IsNull("myColumn") ? String.Empty : dr["myColumn"];

and for int:

myInt = dr.IsNull("myColumn2") ? 0 : dr["myColumn2"];

There are a lot of reasons for using nulls in data. It's not just a
leftover condition of "old data". For instance, dates that haven't been
assigned. You don't want to have to compare every date against January 1,
1753.

It's best to test for null on every field that allows nulls because someone
somewhere and somehow will get a null in even if you try to code them out.

Hope this helps,

Dale Preston
MCAD, MCSE, MCDBA


Chris said:
I know the reason why I get an error when the fields are set to null, but
I'm working with old data that contains them. Any quick fixes? was hoping to
avoid writing out every field in sql statement for ISNULL and avoid the long
check on the make equal to value. Ideas?
Sql statement (I know I could use parameter):
"SELECT * FROM COMPANY_INFO WHERE COMPANY_ID = '" & usercompanyid & "'"
Setting value example:
txtcompanyid.Text = "" & memberdata("COMPANY_ID")

Thanx!
 
H

Hans Kesting

Chris said:
I've tried your suggestion but stil get:
Error in: http://....../default.aspx
Error Message: Cast from type 'DBNull' to type 'String' is not valid.

Any thoughts?

:

What code did you use?
The ToString method does no "casting", so that shouldn't cause this error.

Vb.net might handle things a bit different from C#. I don't know, I use
only C#, so I can't help you much there.

Hans Kesting
 
G

Guest

thanx. I only know VB at the moment. I ended up going to the data and replacing the Null values with either data or blanks. This fixed the issue in the temp. The data moving forward uses a different set of validation and is more stable. But I'd like to work on this since I'm sure it will come back up again.

Thanx.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top