Solved

If statement to exclude 2 values

Posted on 2014-03-11
17
179 Views
Last Modified: 2014-03-18
It seems like a simple logic, but apparently this isn't working correctly.  
I have 2 accounts that I want the procedure to not run on, but with the way it is written now.  Every time I log in with my account "111111111", it still goes through the procedure although it shouldn't and just skip ahead.

 if (Session["Account"].ToString() != ("111111111") || Session["Account"].ToString() != ("000000000"))
                {
                    ds_mbr_Pen.SelectParameters["AccountType"].DefaultValue = "AX21";
                    ds_mbr_Pen.SelectParameters["Account"].DefaultValue = Session["Account"].ToString();
                    ds_Pen_Totals.SelectParameters["AccountType"].DefaultValue = "AX22";
                    
                }

Open in new window

0
Comment
Question by:Shade22
  • 8
  • 6
  • 3
17 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 39921566
it might be better to check them as integers
0
 

Author Comment

by:Shade22
ID: 39921573
If I write it out as
if (Session["Account"].ToString() != ("111111111")
{
}

Open in new window

It has no issues, but when I start using the or statement "||" that is when it stops working.  Can you please provide examples of how you would write it?
0
 
LVL 22

Expert Comment

by:p_davis
ID: 39921580
if (Convert.ToInt32(Session["Account"]) != 111111111)
{
}
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:Shade22
ID: 39921616
It works when I just use the !=111111111, but when I try to put in the or statement "||" I am unable to get it to work.  I need it to say that if it does not equal "111111111" or "000000000".
0
 
LVL 22

Expert Comment

by:p_davis
ID: 39921621
OK what's the error?
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 39921710
if (Session["Account"].ToString() != ("111111111") || Session["Account"].ToString() != ("000000000"))
               
If the session account is 111111111 then you have (false or true) which is true - so the code will run

if (Session["Account"].ToString() != ("111111111")

If the session account is 111111111 then you have false - so the code does not run

what I think you need is the following logic:
NOT (account == "111111111" OR account == "00000000")
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39921724
sorry, make that
NOT (account != "111111111" AND account != "00000000")

(my brain is going to sleep)
0
 

Author Comment

by:Shade22
ID: 39926049
That is the logic I am looking for, but how would I write it for C#?

NOT (account != "111111111" AND account != "00000000")
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39926135
well NOT is ! and AND is &&
(I do see that you do understand not equals is != and the OR statement in C# is || )
0
 

Author Comment

by:Shade22
ID: 39926165
Yes I do, and that is why I think this code should work but it doesn't.
if (Session["Account"].ToString() != ("111111111") || Session["Account"].ToString() != ("000000000"))

Open in new window

I just want it to skip the steps within the procedure if it matches "111111111" or "000000000".  If it doesn't match, I want it to step into the procedure.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39926433
To repeat myself about why your initial attempt does not work:

If the session account is 111111111 then you have (false or true) which is true - so the code will run
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39926435
00000000 is NOT 11111111
0
 

Author Comment

by:Shade22
ID: 39926545
I am using the != which I believe is "Not Equal" to in C#.  So, I am telling it to run any account number besides "111111111" and "000000000".  Am I getting this wrong?
0
 

Assisted Solution

by:Shade22
Shade22 earned 0 total points
ID: 39926616
I had a friend help me out with what you were saying Andy, and was able to get it to work with this.  I didn't try to use the "&" rather than the "||" because in the past I have had issues of it wanting two criteria's to hit but in this case it worked.

Thanks for your help.

if (Session["Account"].ToString() != ("111111111") & Session["Account"].ToString() !=("000000000"))

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39926688
I'm still not convinced you have grasped why.  (Yes != is not equals)

You had if (X or Y) then run the code.  I'll use 11 and 00 as shorthand for your values.

eg. try with 42 as the session ID
if (42 != 11 OR 42 != 00) then your code will run because 42 is not equals to 11

Now try with 11 as the session ID
if (11 != 11 OR 11 != 00) then the code will still run.  Yes 11 is equal to 11 BUT 11 is not equal to 00.


ps.
The single & that you have is a bitwise operator - that is extremely dangerous code you have come up with.
http://msdn.microsoft.com/en-us/library/sbf85k1c.aspx
http://msdn.microsoft.com/en-us/library/2a723cdk.aspx
0
 

Author Comment

by:Shade22
ID: 39926971
Thanks Andy.  I was not aware of the difference between "&" and "&&".
0
 

Author Closing Comment

by:Shade22
ID: 39936362
Andy's assistance help me come to a simple logic.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question