Formatexception:字符串未被识别为有效的布尔值。 [英] Formatexception: string was not recognized as a valid boolean.
本文介绍了Formatexception:字符串未被识别为有效的布尔值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
string offerID = Request.QueryString["selectedoffers"];
DataTable dtOffer = OfferDB.GetDisplayOffer(offerID);
if (dtOffer.Rows.Count > 0)
{
DataRow dr = dtOffer.Rows[0];
if (Convert.ToBoolean(dr["TrustedForm"]))
{
我尝试了什么:
列TrustedForm是varchar类型存储0和1.而且我得到了以上'if'块中的错误。我检查了谷歌转换0和1抛出格式异常。所以我需要你们分享一下if块可以放入控制内部的内容吗?
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?
推荐答案
从字符串转换时,ToBoolean识别的唯一文字是' true'和'false'(以及产生false的null) - 不区分大小写...
'0'和'1'无法识别...
如果你确保0和1是唯一的值,你有两个选项:
1.将数据库类型更改为位
2.首先将值转换为整数然后再转换为boolean
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
更改
Change
if (Convert.ToBoolean(dr["TrustedForm"]))
to
to
if ((string)dr["TrustedForm"] == "1")
但是你应该在数据库中使用适当的字段类型,所以如果这是一个true \ false字段,那么将它改为bit。
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".
这篇关于Formatexception:字符串未被识别为有效的布尔值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文