Solved

case statement, if then else if else in C#

Posted on 2009-04-02
14
1,446 Views
Last Modified: 2012-05-06
I have this code which will write the status based on respective values. e.x. if AT should write Attended, CO confirmed and any other value should write Open. The problem is it always write Open even if the value of the row is AT. I also tried if then else if else statement and got the same results.

Thanks!
     
   for (int i = 0; i < rowcount; i++)
        {
            row = GridView1.Rows[i];
           phcd = ((Label)(GridView1.Rows[i].FindControl("lblDcode"))).Text;
            tCode = ((Label)(GridView1.Rows[i].FindControl("lblDcode")));
           
            switch (tcode.Trim())
            {            
                case "AT":                    
                     tCode.Text = "Attended";
                    break;
                case "CO":                    
                     tCode.Text = "Confirmed";
                    break;
                default:
                     tCode.Text = "Open";
                    break;
               
            }
0
Comment
Question by:jr_bautista
  • 5
  • 5
  • 2
  • +2
14 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 24049597
change this
switch (tcode.Trim())
to this:
switch (tcode.ToUpper().Trim())
0
 
LVL 22

Expert Comment

by:p_davis
ID: 24049602
try tcode.Trim().ToUpper();
0
 
LVL 23

Expert Comment

by:apresto
ID: 24049605
C# is case sensitive remember :)
0
 
LVL 22

Expert Comment

by:p_davis
ID: 24049607
well, i got beat.
0
 
LVL 9

Assisted Solution

by:Gorkem Yuksel
Gorkem Yuksel earned 40 total points
ID: 24049680
Hi all,

Just my 2 cents here.. but tcode is the label control.. you haven't checked the TEXT within the label control..  change your switch statement to this, it should work a lot better:

switch (tCode.Text.ToUpper().Trim())
{
    ....
}

Cheers,

G.
0
 
LVL 23

Expert Comment

by:apresto
ID: 24049700
Well spotted gyuksel!!
0
 

Author Comment

by:jr_bautista
ID: 24049703
I already tried those solutions even before to no avail. The thing is if I try

if tcode == "AT"
{
  write this
}

if tcode == "CO"
{
  write this
}

it works fine....but I will write a long code since I have lots of values
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:HarryNS
ID: 24049758
Put a Response.Write and check what value is there. Then copy the same and keep it in your Case/If statements.
0
 
LVL 23

Expert Comment

by:apresto
ID: 24049761
ok, for a start you have 2 different variables here:
tCode = ((Label)(GridView1.Rows[i].FindControl("lblDcode")));
...
switch (tcode.Trim())
tCode is not the same as tcode - c# is case sensitive. Have you declared tcode somewhere else, im surprised it didnt pick it up. are you using Visual studio?
 
0
 

Author Comment

by:jr_bautista
ID: 24050014
tCode is my variable and i used it in switch statement, just have a wrong type...yes Im using VS I just changed the name of the variable.
0
 

Author Comment

by:jr_bautista
ID: 24050028
I tried all the trim and Upper functions and it doesn't work...
0
 
LVL 23

Accepted Solution

by:
apresto earned 85 total points
ID: 24050354
Set a breakpoint on the switch(...) bit, and check the value in tcode, this may give you a better idea as to what is going on. Are you familiar with debugging in VS?Below is revised code, a few changed:
- you could just use phcd in switch as this is the text roperty of the label
- you can use "row" instead of Gridview1.Rows[i] when setting variables because row is already set to the current row

This should work fine:

...

for (int i = 0; i < rowcount; i++)

{

	row = GridView1.Rows[i];

    phcd = ((Label)(row.FindControl("lblDcode"))).Text;

    tcode = ((Label)(row.FindControl("lblDcode")));

    

    switch (phcd.ToUpper().Trim()) //<!--Set break point here and either hover, use quick watch or immediate qindow to check value

    {            

	    case "AT":                    

			tcode.Text = "Attended";

			break;

	    case "CO":                    

			tcode.Text = "Confirmed";

	        break;

		default:

			tcode.Text = "Open";

		break;

	}

}

...

Open in new window

0
 

Author Comment

by:jr_bautista
ID: 24051721
apresto I tried your code and still doesn't work, I write the value of phcd.ToUpper().Trim() and got the correct values. The thing is if I remove the default: condition if executes right, but when I bring it back it executes the default condition only(write "Open") even it should execute the other condition as some records has values of "AT" and "CO"
0
 

Author Comment

by:jr_bautista
ID: 24061844
Hey guys I got this one worked. It turned out that I need to add  another label control on runtime at set the existing lblDcode to not visible.

Thanks for all the help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syntax error 9 45
Data is not showing from images 15 38
Facial recognition to look through the whole database for a person 3 44
C# Comparing String Arrays. 5 30
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

864 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now