Solved

case statement, if then else if else in C#

Posted on 2009-04-02
14
1,444 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

705 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

17 Experts available now in Live!

Get 1:1 Help Now