12 Replies - 593 Views - Last Post: 28 January 2013 - 01:35 PM Rate Topic: -----

#1 Tizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-January 13

Help guys, Only else statement runs in if else statement

Posted 27 January 2013 - 04:33 PM

Hi guys, (i am new to c#), the underneath code has been cracking my head for 2 days now, could some help.
When i debug this code, once it gets to the first and second if statements, it will jump to else statements, but the if statement for the boolean works fine (I dont seems to get why). Thanks guys

 private void DisplayCustomer(string Customer)
        {
            ClsSqlServer obj = new ClsSqlServer();
            DataSet objdataset = obj.getCustomer(Customer);

            // Bind the data to UI
            string strCustomerName = objdataset.Tables[0].Rows[0][0].ToString();
            string strCountryName = objdataset.Tables[0].Rows[0][1].ToString();
            string strgender = objdataset.Tables[0].Rows[0][2].ToString();
            string strHobbies = objdataset.Tables[0].Rows[0][3].ToString();
            bool Married = Convert.ToBoolean(objdataset.Tables[0].Rows[0][4]);
          
            // if (objdataset.Tables[0].Rows[0][4] != DBNull.Value)
            //{
            //    Married = Convert.ToBoolean(objdataset.Tables[0].Rows[0][4]);
            //}

            TxtCustomerName.Text = strCustomerName;
            drpCountry.Text = strCountryName;

            if ((strgender.Length == 0) || (strgender == "Female"))
            {
                radioFemale.Checked = true;
                
            }
            else
            {
                radioMale.Checked = true;
            }

            if (strHobbies == "Swimming")
            { 
                chkbSwimming.Checked = true;
                
            }
            else 
            {
                chkCoding.Checked = true;    
            }
           
            if (Married == false)
            {
                radioSingle.Checked = true;
            }
            else
            {
                radioMarried.Checked = true;
            }
         
        }


Is This A Good Question/Topic? 0
  • +

Replies To: Help guys, Only else statement runs in if else statement

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3646
  • View blog
  • Posts: 12,657
  • Joined: 12-December 12

Re: Help guys, Only else statement runs in if else statement

Posted 27 January 2013 - 05:15 PM

If you are debugging and stepping through the code you will be able to check the values of the various values and see why it jumps certain parts of the code. If you are not doing this already then I suggest you should spend some time learning basic debugging techniques. There are tutorials on this forum.

Alternatively, produce MessageBoxes at various points to display current values of variables. Please then report back stating what these values are, and what behaviour you are expecting that doesn't happen.

I suspect, however, that it might be that you are setting, for example, radioFemale.Checked = true; but not (at the same time) setting radioMale.Checked = false;.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3632
  • View blog
  • Posts: 11,330
  • Joined: 05-May 12

Re: Help guys, Only else statement runs in if else statement

Posted 27 January 2013 - 05:19 PM

If you've been stepping through with a debugger, what is the values of strGender, strHobbies, and Married? For the strings, remember that case matters.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5595
  • View blog
  • Posts: 11,971
  • Joined: 02-June 10

Re: Help guys, Only else statement runs in if else statement

Posted 27 January 2013 - 06:48 PM

As the guys have said "debugging" line by line. Links and tutorials below.

But if all the radioButtons are in the same group only one can be checked. That's the nature of RadioButtons. If you are unfamiliar with radio buttons looks them on MSDN.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Jan 2013
Spoiler



This post has been edited by tlhIn`toq: 27 January 2013 - 06:50 PM

Was This Post Helpful? 0
  • +
  • -

#5 Tizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-January 13

Re: Help guys, Only else statement runs in if else statement

Posted 28 January 2013 - 02:43 AM

Thanks guys, I have debugged earlier and I have tried again without headway. I put a breakpoint and step in on the line one after the other, I can see the values assigned to string strgender and strHobbies from the top, once it gets to the 1st and 2nd statement (with condition being true), it will jump to the else statement, I know C# is case sensitive, I have checked and checked . My question why is the if statement not running (even with the condition being true or false).
Many thanks..
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3646
  • View blog
  • Posts: 12,657
  • Joined: 12-December 12

Re: Help guys, Only else statement runs in if else statement

Posted 28 January 2013 - 03:02 AM

Set a breakpoint on this line

if ((strgender.Length == 0) || (strgender == "Female"))


then, when it stops, point the mouse at the word strgender. After a brief delay it should pop-up telling you what value it currently contains: what is this value?

At this point choose Debug, Windows, Immediate (if it is not already open) and type in there

?strgender.Length


and it will print the current length. (The question mark means 'print')
Was This Post Helpful? 2
  • +
  • -

#7 Tizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-January 13

Re: Help guys, Only else statement runs in if else statement

Posted 28 January 2013 - 03:25 AM

View Postandrewsw, on 28 January 2013 - 03:02 AM, said:

Set a breakpoint on this line

if ((strgender.Length == 0) || (strgender == "Female"))


then, when it stops, point the mouse at the word strgender. After a brief delay it should pop-up telling you what value it currently contains: what is this value?

At this point choose Debug, Windows, Immediate (if it is not already open) and type in there

?strgender.Length


and it will print the current length. (The question mark means 'print')


Thanks andrewsw, DB was bringing strgender and strHobbies to be 10 characters, (My bad in data type setting up) e.g Male was returned as "Male ".
Thanks guys, you all rock :)
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3646
  • View blog
  • Posts: 12,657
  • Joined: 12-December 12

Re: Help guys, Only else statement runs in if else statement

Posted 28 January 2013 - 03:32 AM

Use

strgender.Trim();

to remove beginning and ending spaces.

BTW change your variable name to strGender (camel-case).
Was This Post Helpful? 1
  • +
  • -

#9 Tizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-January 13

Re: Help guys, Only else statement runs in if else statement

Posted 28 January 2013 - 03:53 AM

View Postandrewsw, on 28 January 2013 - 03:32 AM, said:

Use

strgender.Trim();

to remove beginning and ending spaces.

BTW change your variable name to strGender (camel-case).


WHAO, that was also very HELPFUL :bananaman: , learning every minute from you guys.
@andrewsw :^: & @Everyone : Keep the good work going!
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3632
  • View blog
  • Posts: 11,330
  • Joined: 05-May 12

Re: Help guys, Only else statement runs in if else statement

Posted 28 January 2013 - 05:51 AM

BTW, .NET recommends not using Hungarian notation for your variables. tlhIn`toq had made a very good case for using Hungarian in .NET with regards to UI controls. I actually also use Hungarian for controls as well.

Anyway, if you are going to use Hungarian, you should be consistent about using Hungarian. Remember to be kind to people who read your code by giving them consistency. For example if you are going to use:
string strGender;


then you should also use:
bool bMarried;
ClsSqlServer clssqlserver;
DataSet dataset;


This post has been edited by Skydiver: 28 January 2013 - 06:25 AM
Reason for edit:: Fixed typos

Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3646
  • View blog
  • Posts: 12,657
  • Joined: 12-December 12

Re: Help guys, Only else statement runs in if else statement

Posted 28 January 2013 - 06:21 AM

FWIW
I continue to use prefixes for controls (btn, txt, etc.). Worse than whether to use prefixes or not is failing to rename controls, bleugh!? Form1, TextBox1! We see this constantly. You would think it was just in the (sample) code posted here, but it is not!

I used to use the fuller Hungarian str, dbl, etc., for variables, but I've dropped to s (sGender - string), dt (DateTime), b (Boolean), d (Double), i (Integer), by (Byte), c (Char), l (Long).

Added: If you were working in a group then you would have to use the house-style anyway, and adapt.

This post has been edited by andrewsw: 28 January 2013 - 06:41 AM

Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4539
  • View blog
  • Posts: 7,917
  • Joined: 08-June 10

Re: Help guys, Only else statement runs in if else statement

Posted 28 January 2013 - 12:53 PM

See, I'm the opposite. I try to make my variable names meaningful without attaching information about their type to the identifier. For one thing, it makes refactoring easier if I need to change something, and for another, I always work in an IDE, so I can always gather more information about a variable if I need it. That's also why I religiously use var instead of explicitly typing the type.

However, for UI controls, I always use more complete notation. Like nameTextBox. I find that keeping that level of separation of my UI controls makes sense.
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3646
  • View blog
  • Posts: 12,657
  • Joined: 12-December 12

Re: Help guys, Only else statement runs in if else statement

Posted 28 January 2013 - 01:35 PM

View PostCurtis Rutland, on 28 January 2013 - 12:53 PM, said:

See, I'm the opposite. I try to make my variable names meaningful without attaching information about their type to the identifier. For one thing, it makes refactoring easier if I need to change something, and for another, I always work in an IDE, so I can always gather more information about a variable if I need it. That's also why I religiously use var instead of explicitly typing the type.

However, for UI controls, I always use more complete notation. Like nameTextBox. I find that keeping that level of separation of my UI controls makes sense.

I studied all the advice about dropping the type prefix but I/we end up with lots of similarly named things. It's tricky to keep thinking up different names for, essentially, the same thing.

If I can see
sFirstName - I know it's a string, and
txtFirstName - is a TextBox,

and I don't fancy waving my mouse around to discover this :)/>. I did drop the 'tr' though :)/>

Anyway, I'm sure many people have there own take on this subject - or perhaps everyone just follows the (MS) rules: but there are no rules!

And refactoring is a breeze anyway(?)regardless of the naming.

This post has been edited by andrewsw: 28 January 2013 - 01:43 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1