Click here to Skip to main content
14,325,045 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have SQL Table and in some rows I have null values for some columns. SQLDataReader breaks when it comes to the first row with some null values. Columns with null values are string and float type
How to solve this issue?

What I have tried:

I tried to insert "" in string column, but the problem is that I have Foreign Key relation on that field and SQL doesn't allow that.
Posted
Updated 19-Jun-19 21:58pm
Comments
Maciej Los 20-Jun-19 3:30am
   
What you mean by saying: "SQLDataReader breaks..."?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

We can't see your code, and we have no idea what "SQLDataReader breaks when it comes to the first row with some null values" actually means in the real world.

But ... the most likely reason for problems is that you aren't checking for null returns and are just blindly casting the values to string or whatever. That won't work: NULL values are essential in Foreign Keys, or it becomes very hard to insert rows in the first place - so you have to allow for them in your code by explicitly checking for DBNull.Value when you process DataReader values:
object o = myDateReader["MyColumn"]
if (o == DBNull.Value)
   {
   myString = "";
   }
else
   {
   myString = (string) o;
   }
   
Comments
Sinisa Janjetovic 20-Jun-19 6:41am
   
Thank you, I solved the issue by following your suggestion
OriginalGriff 20-Jun-19 6:41am
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100