Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to run the following code but get a conversion error.

How can I rewrite my code to achieve the same ?
C#
boolResult= (bool?)dataReader["BOOL_FLAG"] ?? true;
intResult= (int?)dataReader["INT_VALUE"] ?? 0;


Thanks
Posted
Updated 23-Mar-11 8:43am
v2

Values coming from database are not null if the data is null. You should check against System.DBNull.Value. Something like:
boolResult = dataReader["BOOL_FLAG"] == System.DBNull.Value ? null : (bool?)dataReader.GetBoolean(colNumber);
 
Share this answer
 
v2
You can try

C#
bool boolResult= dataReader.GetBoolean(dataReader.GetOrdinal("BOOL_FLAG"));
int intResult= dataReader.GetInt32(dataReader.GetOrdinal("INT_VALUE"));



I hope it will works. Have a look on this Link[^] also

Edit: Modified for independent of column location. See comments.
 
Share this answer
 
v3
Comments
elad2109 23-Mar-11 15:22pm    
I don't want depend on coulmn location. This is a bad practice.
TweakBird 23-Mar-11 15:30pm    
This not depend on column location.
bool boolResult= dataReader.GetBoolean(dataReader.GetOrdinal("BOOL_FLAG"));
int intResult= dataReader.GetInt32(dataReader.GetOrdinal("INT_VALUE"));
elad2109 23-Mar-11 15:38pm    
What happens if the returned value from the dataReader.GetOrdinal("BOOL_FLAG") is a DBnull value ?
TweakBird 23-Mar-11 23:53pm    
Simple thing dear, you can apply conditional operator. already Mike given.
C#
bool boolVar = false;
if (!bool.TryParse(dataReader["BOOL_FLAG"], out boolVar))
    throw new Exception("Didn't read a boolean!");

int intVar = int.MinValue;
if (!int.TryParse(dataReader["INT_VALUE"], out intVar))
    throw new Exception("Didn't read a boolean!");


If you need nullable values, you could set them in the if statement like
C#
bool boolVar = false;
bool? finalBool = false;
if (!bool.TryParse(dataReader["BOOL_FLAG"], out boolVar))
    finalBool = null;
else
    finalBool = (bool?) boolVar;

int intVar = int.MinValue;
int? finalInt = int.MinValue
if (!int.TryParse(dataReader["INT_VALUE"], out intVar))
    finalInt = null
else
    finalInt = (int?) intVar;


Hope this helps!

Hogan
 
Share this answer
 
Comments
elad2109 23-Mar-11 15:31pm    
The probelm wasn't the casting but catching a null value.
snorkie 24-Mar-11 14:07pm    
I agree that the problem is about catching a null value. However, the code I posted will catch a null via the .TryParse method and I understand it to be much safer if other invalid values are passed in. Sorry that you didn't like it! Good luck! --Hogan
elad2109 24-Mar-11 14:19pm    
Didn't mean to offend you. I should have used other words. sorry.
snorkie 24-Mar-11 14:29pm    
No offense taken. I wanted to clarify my answer.If I'm wrong, then I need to know so I stop making mistakes. This is a great way to validate/refute what I do every day. The .TryParse method has saved me lots of headache for unvalidated input. --Hogan
Check the blog here for a generic extension solution.
 
Share this answer
 

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