string未被识别为有效的布尔值。 asp.net c# [英] string was not recognized as a valid boolean. asp.net c#
问题描述
嗨......
我在匹配数据库中的用户电子邮件和密码时遇到错误。实际上我想检查用户的电子邮件soo我从数据库中获取数据表,现在检查包含1或2的字段user_typeid,但当我转换为布尔类型时,错误发生字符串未被识别为有效布尔。代码如...
Hi...
i am facing an error while matching the user email and password from database. Actually i want to check the velidation of users email soo i fetch the datatable from database and now checking the field "user_typeid" which contains 1 or 2 but when i convert into boolean type veriable the error occured as "string was not recognized as a valid boolean". the code is as...
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt = getdata();
if (dt.Rows.Count > 0)
{
//in this statment error occures-->// Boolean bol=Convert.ToBoolean( dt.Rows[0]["activated"]);
if(bol==true)
{
bool utype=Convert.ToBoolean( dt.Rows[0]["user_typeid"]);
if(utype==true)
{
bool delate=Convert.ToBoolean( dt.Rows[0]["delated"]);
if(delate==true || delate==null)
{
string user=Convert.ToString(dt.Rows[0]["user_name"]);
Session["username"] = user;
Response.Redirect("view.aspx");
}
else
{
Response.Write("You are blocked by admin");
}
}
else
{
Response.Write("You are not authorized to login from hare");
}
}
else
{
Response.Write("Yor are not a verified user");
}
}
else
{
Response.Write("User not exists");
}
}
plz任何人帮助我在asp新手。净。我将感谢你
plz anybody help me m new in asp.net. i will be thankfull to you
推荐答案
Convert.ToBoolean不能将随机值转换为bool
- 只有值它理解。
在这种情况下,你想转换一个整数 - 所以给它一个整数:
Convert.ToBoolean cannot convert "random" values tobool
- only values it understands.
In this case, you want to convert an integer - so give it an integer:
bool utype=Convert.ToBoolean((int) dt.Rows[0]["user_typeid"]);
Zero将转换为false,其他所有都为true。
http: //msdn.microsoft.com/en-us/library/h0b8dwce.aspx [ ^ ]
我将boolean的类型更改为int32 soo我的问题解决了..
i changed the type of boolean into int32 soo my problem is solved..
这篇关于string未被识别为有效的布尔值。 asp.net c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!