0

Im validating user entered text, entered into a textbox, to filter out non-numbers,and then compare >= / < to an object pulled from a database. My issue is happening in 2 places though it may be just 1 issue. I have a conversion of a data table item into a string, then into an int. This int is then passed into a series of if /else statements that should validate the users input. After stepping through the program it hits my conversion statement appears to do the conversion, then proceeds to skip my if /else statements.

   int classRPM;
        int fanRPM;
        string actualdata = string.Empty;
        char[] entereddata = txfanrpm.Text.ToCharArray();
        foreach (char aChar in entereddata.AsEnumerable())
        {
            if (Char.IsDigit(aChar))
            {
                actualdata = actualdata + aChar;

                using (Fanrpm ds = new Fanrpm(cbdesigntype.SelectedValue.ToString(), cbfansize.SelectedValue.ToString(), cbfanclass.SelectedValue.ToString()))
                {
                    DataTable dt = ds.dataset.Tables[0];
                    classRPM = Convert.ToInt32(dt.Rows[0].Field<string>("ClassRPM"));

                   // MessageBox.Show(aChar.ToString());
                    fanRPM = Convert.ToInt32(actualdata);
                    if (fanRPM >= classRPM)
                    {
                        MessageBox.Show("hi");
                    }
                    else if (fanRPM < classRPM)
                    {
                        MessageBox.Show("Hide");
                    }
                }
            }
            else
            {
                MessageBox.Show(aChar + " is not numeric");
                actualdata.Replace(aChar, ' ');
                actualdata.Trim();
            }
        }
        txfanrpm.Text = actualdata;
11
  • What do you mean by "proceeds to skip my if /else statements."? Commented Aug 6, 2014 at 19:58
  • as in the program does not step through it to either show the messagebox of hi or hide. IT does do the first and last if/else as any non number will prompt the * is not numeric else. Commented Aug 6, 2014 at 20:00
  • 1
    Do you have this code wrapped in an exception handler? Are you stepping through the code? Did you see a message when you had your MessageBox.Show(aChar.ToString()); code there? Commented Aug 6, 2014 at 20:06
  • 1
    Use the debugger. Set a breakpoint on the for line (F9) then step through your code with F10. Be sure to have checked DEBUG->Exceptions->Common Language Runtime Exceptions for User Unhandled. When the error hits you should be able to see the reason in exception message property. Commented Aug 6, 2014 at 20:09
  • That piece of code (MessageBox.Show(aChar.ToString())) was a mistake that it got uncommented, while it was uncommented it did nothing and was also skiped, there is no error handling yet. Commented Aug 6, 2014 at 20:11

1 Answer 1

1

After the comments above I think you could change your code in this way

// If the reading from the database gives always the same value is not correct to 
// exexute this code inside the foreach. Just do it one time here and go on....

float classRPM = 0.0f;
using (Fanrpm ds = new Fanrpm(cbdesigntype.SelectedValue.ToString(),
                              cbfansize.SelectedValue.ToString(), 
                              cbfanclass.SelectedValue.ToString()))
{
     DataTable dt = ds.dataset.Tables[0];
     classRPM = dt.Rows[0].Field<float>("ClassRPM");
}

float fanRPM;
string actualdata = string.Empty;

// No need to use AsEnumerable.... 
// And also this code could be easily replaced by single line float.TryParse 
// if you don't need to show a message box for every wrong char....
foreach (char aChar in txfanrpm.Text.ToCharArray())
{
    if (Char.IsDigit(aChar))
        actualdata = actualdata + aChar;
    else
        MessageBox.Show(aChar + " is not numeric");
}
// Now you could start your comparisons....
fanRPM = Convert.ToSingle(actualdata);
if (fanRPM >= classRPM)
   MessageBox.Show("hi");
else
   MessageBox.Show("Hide");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.