Click here to Skip to main content
15,118,573 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
string offerID = Request.QueryString["selectedoffers"];
                DataTable dtOffer = OfferDB.GetDisplayOffer(offerID);

C#
if (dtOffer.Rows.Count > 0)
                {
                    DataRow dr = dtOffer.Rows[0];
                    if (Convert.ToBoolean(dr["TrustedForm"]))
                    {


What I have tried:

Hi,

The column TrustedForm is varchar type stores 0 and 1. And I'am getting above error in 'if' block. I checked through google converting 0 and 1 throws format exception. So I need you guys to share what can be put in 'if' block to go control inside?
Posted
Updated 23-Nov-16 4:32am

When converting from string the only literals recognized by ToBoolean are 'true' and 'false' (and null that yields false) - case insensitive...
'0' and '1' are not recognized...
If you are sure that 0 and 1 are the only values you have two options:
1. Change the database type to bit
2. Convert the value first to integer and then to boolean
   
v2
Comments
Amith 12807185 28-Nov-16 0:08am
   
Hi, thanks for the deep insight on this error. Actually I tried to change database type to bit but there is dependencies on more store procs. But this answer also works.
Change

C#
if (Convert.ToBoolean(dr["TrustedForm"]))


to

C#
if ((string)dr["TrustedForm"] == "1")


However you should use the appropriate field types in your database so if this is intended as a true\false field then change it to "bit".
   
Comments
Karthik_Mahalingam 24-Nov-16 0:49am
   
5
Amith 12807185 28-Nov-16 0:04am
   
Hi, it worked. I checked the code once more and confirmed database column values are '0','1' and null due to not null constraint.And in code I only want to check if value is '1'. So converting to string does the job for me.

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