Solved

case statement, if then else if else in C#

Posted on 2009-04-02
14
1,448 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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
 
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

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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