Click here to Skip to main content
15,936,802 members
Please Sign up or sign in to vote.
1.00/5 (1 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.
Updated 19-Jun-19 21:58pm
Maciej Los 20-Jun-19 3:30am    
What you mean by saying: "SQLDataReader breaks..."?

1 solution

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 = "";
   myString = (string) o;
Share this answer
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900