Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.



C#
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..
Posted
Comments
DaveAuld 22-May-10 2:56am    
What exactly are you trying to do in that statement? i cannot see why you are trying to Convert to Boolean? If your are testing the equality of 2 DB objects it will already return true or false.
narendrarathod 22-May-10 3:05am    
thanks for replying i actually trying it read DB table when i click on Edit button then i get this error
Johnny J. 24-May-10 4:19am    
Which line ist throwing the exception?

chkactive.Checked = Convert.ToBoolean(dssubcat.Tables[0].Rows[0]["activestatus"]);

or

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

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.
 
Share this answer
 
Comments
William Winner 22-May-10 21:45pm    
Totally agree, but it's also better to actually pass a boolean to it. Where do people come up with this stuff?
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?
 
Share this answer
 
Comments
narendrarathod 22-May-10 3:11am    
Thank you .. you are right but after asking i already corrected yesterday's question.
narendrarathod wrote:
if (Convert.ToBoolean((dssubcat.Tables[0].Rows[0]["gallery_img"]) == System.DBNull.Value ? "" : dssubcat.Tables[0].Rows[0]["gallery_img"]))



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 "".
 
Share this answer
 
The other answers did answer the question, but I wanted to find out
something and try to be a little more specific.

What is the value type within the gallery_image column? Are you using that to store an actual image? a path to an image? or a Boolean indicating that an image has been set?

First, you would need to look up what the type it is returning and then look up the documentation on the method that will be used here[^]. For instance, if it was a string that was being passed in, you would go here[^]. This method will throw an error if the value returned is not equal to "True" or "False".

So, at least for one of your conditions (the true condition), you will definitely get an error because you are passing an empty string to Convert.ToBoolean.

You have to make sure that what is being passed will work before you code it.
 
Share this answer
 
Hi
Where you have written this code

C#
chkactive.Checked = Convert.ToBoolean(dssubcat.Tables[0].Rows[0]["activestatus"]);


try this

C#
if (Convert.ToBoolean(dssubcat.Tables[0].Rows[0]["activestatus"]) == true)
{
    chkactive.Checked = true;
}
 
Share this answer
 
v2
Comments
DaveyM69 23-May-10 5:39am    
I have edited your post so it makes sense and formatted it - however, both the original and your suggestion will produce identical results (if true, but your 'improvement' will not set the Checked property to false when required).
Johnny J. 24-May-10 4:17am    
Reason for my vote of 1
This will not solve the problem. If Convert.ToBoolean generates an error because the value of the "activestatus" field is invalid, then it will still do so in this case

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