?
Solved

case statement, if then else if else in C#

Posted on 2009-04-02
14
Medium Priority
?
1,455 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
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!

 
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

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.

Question has a verified solution.

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

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

770 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