无法将字符串识别为有效的布尔值. [英] String was not recognized as a valid Boolean.?

查看:135
本文介绍了无法将字符串识别为有效的布尔值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.. !!

早安.. !!

我的代码在这里,代码中有错误字符串未被识别为有效的布尔值".以粗线显示

任何人都可以帮助我.



Hello..!!

Good Morning..!!

My code is here there is Error in Code "String was not recognized as a valid Boolean." which displayed in bold line

anybody Help me what can i do for it.



protected void Page_Load(object sender, EventArgs e)
   {
       lblReqMsg.Text = GlobleVariable.strReqMsg;
       ltrheading.Text = "Add Gallery";
       Title = "Add Gallery" + ConfigurationManager.AppSettings["AdminTitle"];
       if (Page.IsPostBack == false) {
       if (!string.IsNullOrEmpty(Request["Id"]))
       {
           int res;
           if (int.TryParse(Request["Id"],out res))
           {
               if (Request["Flag"] == "Edit") {
                   Title = "Edit Gallery" + ConfigurationManager.AppSettings["AdminTitle"];
                   ltrheading.Text = "Edit Gallery";
                   DataSet dssubcat = new DataSet();
                   objgallery.id = Convert.ToInt32(Request["id"]);
                   dssubcat = objgallery.SelectSingleItem();
                   if (dssubcat.Tables[0].Rows.Count > 0) {
                       txtTitle.Text = (string)dssubcat.Tables[0].Rows[0]["gallery_name"];
                       chkactive.Checked = Convert.ToBoolean(dssubcat.Tables[0].Rows[0]["activestatus"]);
                       if (Convert.ToBoolean((dssubcat.Tables[0].Rows[0]["gallery_img"]) == System.DBNull.Value ? "" : dssubcat.Tables[0].Rows[0]["gallery_img"]))
                       {
                           ViewState["gallery_img"] = "";
                           lblimgmsg.Visible = true;
                       }
                       else {
                           ViewState["gallery_img"] = ((string)dssubcat.Tables[0].Rows[0]["gallery_img"]).Replace("''", "'");
                           aimg.Visible = true;
                           //lblseperator.Visible = True
                           //lbdelete.Visible = True
                           aimg.Attributes.Add("onclick", "javascript:return winopenaddnews('" + Request["id"] + ViewState["gallery_img"] + "','img');");
                       }
                   }
               }
               else {
                   Response.Redirect("home.aspx");
               }
           }
           else {
               Response.Redirect("home.aspx");
           }
       }
       else {
       }
       //rbEmbed.Checked = True
     }
   }
   //txtTitle.Attributes.Add("onkeypress", "javascript:return checkKey('" & imgbtnSave.ClientID & "',event);")
   //imgbtnSave.Attributes.Add("onclick", "javascript:return valid_news(" & txtTitle.ClientID & "," & File4.ClientID & ");")




谢谢..




Thank you..

推荐答案

作为一般规则,最好在此处使用 bool.TryParse ,因为这在内部包裹了检查代码中的异常处理程序中,这样您就不必自己进行管理,代码也不会崩溃.
As a general rule, it''s better to use bool.TryParse here as this internally wraps the check code in an exception handler so that you don''t have to manage this yourself and your code won''t blow up.


在您提出的最后几个问题中,您是.NET的初学者,并且刚刚开始学习它.我建议您买一本书并先阅读或阅读在线教程.

其次,您提出的错误非常直接.您尝试解决它吗?
昨天您遇到了类似的问题: http ://www.codeproject.com/Questions/83024/Unable-to-cast-object-of-type-System-Web-UI-HtmlCo.aspx [不包含布尔值.您如何/为什么尝试将其转换为布尔值?
From last few questions that you had asked, you are a beginner in .NET and had just started learning it. I would suggest you to buy a book and read first or go through online tutorials.

Secondly, the errors you ask are pretty direct. Do you try resolving it?
Yesterday you had a similar issue: http://www.codeproject.com/Questions/83024/Unable-to-cast-object-of-type-System-Web-UI-HtmlCo.aspx[^]
We explained you why so. Today you report something similar at another place.

It looks like first thing you need to learn is DEBUGGING!

As about error: From your rest of the code, it is very clear that ''dssubcat.Tables[0].Rows[0]["gallery_img"'' does not contains a Boolean value. How/Why are you trying to convert it into a Boolean value?


narendrarathod写道:
narendrarathod wrote:

if( Convert.ToBoolean((dssubcat.Tables [0] .Rows [0] ["gallery_img"])== System.DBNull.Value?":dssubcat.Tables [0] .Rows [0] ["gallery_img"]) )

if (Convert.ToBoolean((dssubcat.Tables[0].Rows[0]["gallery_img"]) == System.DBNull.Value ? "" : dssubcat.Tables[0].Rows[0]["gallery_img"]))




您实际上是在返回"并将其转换为布尔值.
您注定会出错.您需要返回truefalse而不是".




You are actually returning an "" and trying to convert that to a boolean.
You are bound to get errors. You need to return a true or a false instead of an "".


这篇关于无法将字符串识别为有效的布尔值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆