Click here to Skip to main content
16,015,481 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hey Peeps,

I have a small (possibly stupid) error happening in my code. Not sure why it is happening but I do hope someone on here can shed some light on this.

First things first, I have a dataset that requests a single value from the database:

SQL
SELECT [menu$IsActive] FROM [Menu$Main] WHERE ([menu$NavURL] = @NavigateURL) AND ([menu$Host] = @Host)


In my code, I have the following (on the Page_Load event handler):

C#
bool MenuIsActive = bool.Parse(taMenuBack.GetCurrentMenuState(HttpContext.Current.Request.Url.ToString(), HttpContext.Current.Request.URL.Host).ToString());

if (MenuIsActive)
{
    btnChangeStatus.Text = "Set to Inactive";
} else {
    btnChangeStatus.Text = "Set to Active";
}


When previewing the page, it gives me 'String was not recognized as a valid Boolean' . The SQL query in the dataset is set to 'Scalar' and the return result is definitely a boolean (the actual column type returning is 'bit'). When I do this:

C#
string testString = taMenuBack.GetCurrentMenuState(HttpContext.Current.Request.Url.ToString(), HttpContext.Current.Request.URL.Host).ToString()


It returns "True". Any ideas?
Posted
Updated 30-May-23 3:29am
v2

1 solution

Try using Boolean.Parse.
Infact Boolean.TryParse would be your best bet.

The difference between the two could be to do with CLS compliance.
AFAIK, 'True' and 'False' are traditionally VB.Net boolean types.
 
Share this answer
 
Comments
Juan Davel 9-Feb-12 7:25am    
The 'Boolean.Parse' and 'Boolean.TryParse' did not work however, I got it working by converting the string answer to lower case ('menuIsActive.ToLower()'). Wrapping that in the parse command (bool.Parse(menuIsActive.ToLower())) did the trick!
Sergey Alexandrovich Kryukov 9-Feb-12 18:57pm    
They do work in all combinations. What do you mean "they did not"? Your case fix is not related to it.
--SA
Espen Harlinn 9-Feb-12 8:32am    
5'ed!
Abhinav S 9-Feb-12 12:06pm    
Thank you Espen.
Sergey Alexandrovich Kryukov 9-Feb-12 18:56pm    
All correct, a 5.
--SA

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