Im getting an "unknown selection" error on this code. Can anyone help me. I don't know what is wrong. My two text boxes both have English selected. blankl and blanlkw have both been initialized as decimals.

if (bunitsCB.Text == "English")
                {                
                    switch (dieunitsCB.Text)
                    {
                        case "English":
                            dielTB.Text = Convert.ToString(blankl + Convert.ToDecimal(24));
                            diewTB.Text = Convert.ToString(blankw + Convert.ToDecimal(24)); break;
                        case "Metric":
                            dielTB.Text = Convert.ToString(Math.Round((blankl * Convert.ToDecimal(25.4)), 0) + Convert.ToDecimal(588));
                            diewTB.Text = Convert.ToString(Math.Round((blankw * Convert.ToDecimal(25.4)), 0) + Convert.ToDecimal(588)); break;
                        default:
                            throw new Exception("Unknown selection");
                    }
                 }
                else
                {
                    switch (dieunitsCB.Text)
                    {
                        case "English":
                            dielTB.Text = Convert.ToString(Math.Round((blankl / Convert.ToDecimal(25.4)), 0) + Convert.ToDecimal(24));
                            diewTB.Text = Convert.ToString(Math.Round((blankw / Convert.ToDecimal(25.4)), 0) + Convert.ToDecimal(24)); break;                           
                        case "Metric":                            
                            dielTB.Text = Convert.ToString(blankl + Convert.ToDecimal(588));
                            diewTB.Text = Convert.ToString(blankw + Convert.ToDecimal(588)); break;
                        default:
                            throw new Exception("Unknown selection");
                    }
                }

Dani AI

Generated

Short summary: fixed the immediate problem by rebuilding the solution. That usually points to an out-of-date build or a designer/code mismatch, but the code also depends on fragile string comparisons. Below are focused, practical checks and a safer pattern to avoid the same failure later.

First, reproduce the exact runtime values before the switch — the single best diagnostic is to inspect the combo Text/SelectedItem at the moment the switch runs (use the debugger or a short log). Look for empty strings, leading/trailing spaces, different casing, or a bound object whose ToString() isn’t "English" or "Metric". ’s question about blankl/blankw is important too: validate those values with decimal.TryParse so arithmetic can’t fail silently.

A more robust approach: normalize the combo text, parse it into a small enum, and use decimal literals for constants (avoid repeated calls to Convert.ToDecimal). Example pattern:

enum Units { English, Metric }

Units ParseUnits(string s)
{
    if (string.IsNullOrWhiteSpace(s)) throw new InvalidOperationException("Unit not selected");
    s = s.Trim();
    if (s.Equals("English", StringComparison.OrdinalIgnoreCase)) return Units.English;
    if (s.Equals("Metric", StringComparison.OrdinalIgnoreCase)) return Units.Metric;
    throw new InvalidOperationException("Unknown unit: " + s);
}

decimal l, w;
if (!decimal.TryParse(lengthTB.Text, out l) || !decimal.TryParse(widthTB.Text, out w))
    throw new InvalidOperationException("Invalid numeric input");

Then switch on the Units value and use 25.4m, 24m, 588m and decimal.Round(...) for consistent decimal arithmetic. Finally, if a rebuild fixes a problem once, also try Clean + Rebuild, delete bin/obj if needed, and verify the event handler is wired to the control you expect. ’s note about using code tags is useful for future posts, and @sksnake’s consolidation suggestion helps keep related debugging details in one place.

Recommended Answers

All 4 Replies

What types are blankl and blankw ?
As integers, I do not get any errors, or the thrown error.

Side comment - with 18 post under your belt, you should have known enough to wrap your code in code tags.

[code] ... your code here ... [/code] at least, but for this forum, preferably [code=c#] ... [/code]

I rebuilt the solution and it worked.

http://www.daniweb.com/forums/thread176030.html -- "switch problem"
http://www.daniweb.com/forums/thread177858.html -- "case statement not working"
http://www.daniweb.com/forums/thread177818.html -- "case statement default"
http://www.daniweb.com/forums/thread177436.html -- "Unknown Error"

You need to consolodate your threads. In the 4 threads you have 2 problems, one with a switch, one with reading the value from a combo box.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.