Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.55/5 (4 votes)
I'm new to C#.
I've been tasked to create this application and I felt like I was doing well. Do to proprietary information I cannot give too many details but I do need some direction.

I'm using Silverlight 5 with C# to build a business application. Here's the deal:

I have a value brought in from my DB based on a LINQ query that populates a text block to display the value so I can see it. To this point it all works, I change the value in the DB the value changes in the text block. The value in the DB is a bit which translates to text in my text block (True,False)...great...works!

When I attempt to take that value and convert it to Bool however, it fails...every time. All my searching and research has resulted in great ideas that seem to work for others. I was pretty sure I was using it correctly, obviously not however.

Here's the code where my primary issue is located.

C#
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    flag.Text = bool.TryParse(hiddenSts.Text, out allowed).ToString();
            
    bool isAllowed;
            
    if(!bool.TryParse(hiddenSts.Text, out isAllowed))
    {
        String Name = WebContext.Current.User.DisplayName.ToString().Substring(8);
        String Auth = WebContext.Current.User.IsAuthenticated.ToString();

        userNotAllowed accessDenied = new userNotAllowed();
        accessDenied.userIdentity.Text = Name;
        accessDenied.authentication.Text = string.Concat(Auth, " | Win_Auth");
        accessDenied.Show();
    }
}


Upon navigating to this page my code takes the value stored in my text block (hiddenSts), which is True or False depending on my bit value in the DB, and attempts the Boolean conversion on the string in the text block. So when the value is false I have a child window pop up and a redirect between pages...all working when I hard code any values. TryParse is the only one not acting as I thought it would.

-flag is a text box to see the return of the TryParse (always False)
-hiddenSts is a text block to see the value from the DB based on a query

Any direction would be greatly appreciated.
Posted
Comments
Richard C Bishop 11-Mar-13 15:22pm    
Well, a bit in SQL Server is a 0 or 1, a bool in C# is true or false. You might have a conversion problem with your try parse. What is the value of "hiddenSts.Text" when you are assigning it to flag.Text while debugging?
JustDaveL 12-Mar-13 9:11am    
The flag displays "True" or "False" as text in the text block.
Richard C Bishop 12-Mar-13 9:46am    
This may be insignificant, but can you return your "True" and "False" without capital letters? They are usually lowercase in C#.
ZurdoDev 11-Mar-13 15:27pm    
What is the value of hiddenSts.Text? Also, you say tryparse is giving an error? It shouldn't give an error. What is the error?
JustDaveL 12-Mar-13 11:34am    
My title may have been named carelessly...There is no error with the function. But it doesn't work as expected. The values in hiddenSts are either "True" or "False" as (I believe) are stored as text that way.

You need to understand that bool.TryParse can return false, which is not an "error" (what is it?), but an indication that input string contains something unexpected or is null. Expected are the capitalized strings "True" or "False".

—SA
 
Share this answer
 
Comments
Yvan Rodrigues 11-Mar-13 17:48pm    
I'm curious if Boolean.TrueString and Boolean.FalseString might default to other values if the current culture is set to something other than English. Any idea?
Sergey Alexandrovich Kryukov 11-Mar-13 17:50pm    
Good point. This is easy to check up. Do you want to try out? I know some cases when existing cultures are not that "cultural", for example, in Arabic date formats...
—SA
JustDaveL 12-Mar-13 9:29am    
I understand the function returns False, I need it to on occasion. But I also need to return something other than false which is the problem. I've seen the "Culture" settings in some of my searches. I would love to try it out, anything that may work. How would I go about using that functionality?
Sergey Alexandrovich Kryukov 12-Mar-13 11:14am    
Why would you be interested in culture? I already explained what's matter: on input, you have "True" and "False", and do yourself a favor, always use neutral culture for such things. I hope your user won't need to type "True" or "False" manually in whatever language, so keep to neutral culture.

Your problem is already solved, did you notice it? So, please use what we recommended and accept the solutions formally (green button).
If you still did not get it, your follow-up questions are welcome.

—SA
To check what's going on, use bool.Parse instead of bool.TryParse. If a FormatException is thrown, the value you are supplying is not equal to bool.TrueString or bool.FalseString (the test is case-insensitive).

If ArgumentNullException is thrown, your value is null.

Otherwise it should work.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Mar-13 17:43pm    
All correct, and yes, calling a exception-throwing variant of a method is a clear case. I only want to know that it's not a must, especially in this simple case. It's enough to see what's the input string or check if with "if" (not in a working code, of course). My 5.
—SA
JustDaveL 12-Mar-13 9:18am    
Ok. So I switched some things around and used the bool.Parse function which threw the "System.FormatException was unhandled by user code" error and stated that the string was not recognized as a valid Boolean. However the hiddenSts text block I have included to show my value displays the text "True" or "False" based on what I have done to the DB and to the Code.
Yvan Rodrigues 12-Mar-13 9:54am    
Try showing the value (either as output or in the debugger) of System.Boolean.TrueString and System.Boolean.FalseString and report back.

Also I'm wondering if there are any hidden characters involved. Try something like

foreach(char c in hiddenSts.Text) Console.Write(((byte)c).ToString("x "));(
JustDaveL 12-Mar-13 11:29am    
System.Boolean.TrueString returns "True" and System.Boolean.FalseString returns "False." I can't seem to get the loop to work. This is not a console application and that functionality works but, nothing happens.
Yvan Rodrigues 12-Mar-13 12:27pm    
Well, there's really only one test:
If bool.Parse("True"); doesn't throw an exception; and
if bool.Parse("False"); doesn't throw an exception; and
If bool.Parse(hiddenSts.Text); does throw an exception; then
hiddenSts.Text is not equal to "True" or "False". Maybe there is a hidden character that got in there somehow, like "True\x00" or something like that.

If it looks like a duck and it quacks like a duck... it might be a chicken in a duck costume.

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