Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

case statement, if then else if else in C#

Posted on 2009-04-02
14
Medium Priority
?
1,460 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

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

Assisted Solution

by:Gorkem Yuksel
Gorkem Yuksel earned 120 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 255 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

636 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