1

Simply i have a textbox that my users will enter in a Fan rpm. I need my program to take the entered fanrpm and compare it with a database item called classrpm. My query from the database Will only return 1 item ever.

I need fanrpm to be > classrpm, and if fanrpm >= class rpm a msg box to pop-up and state "Fan RPM exceeds ClassRPM(which is x), please enter a value under ClassRPM.

The code below was my test with fake variable names. I attempted to take and convert the user entered text and the database retrieved item, then using the converted values to do the above if statement, and then output a msg box to inform me if it succeeded.

  private void textBox1_TextChanged(object sender, EventArgs e)
   {
        txfanrpm.MaxLength = 4
        string classrpm;
        string fanrpm;
        using (Fanrpm ds = new Fanrpm(cbdesigntype.SelectedValue.ToString(), cbfansize.SelectedValue.ToString(), cbfanclass.SelectedValue.ToString()))
        {
            DataTable dt = ds.dataset.Tables[0];
            List<string> coolList = new List<string>();

            foreach (DataRow row in dt.Rows)
            {
                coolList.Add(row[0].ToString());
            }

            classrpm = coolList.ToString();
            fanrpm = txfanrpm.Text.ToString();
            int classrpmInt;
            int fanrpmInt;
            classrpmInt = Convert.ToInt32(classrpm);
            fanrpmInt = Convert.ToInt32(fanrpm);
            if (fanrpmInt >= classrpmInt)
            {
                MessageBox.Show(this, "user entered fanrpm higher then class rpm which is yadda yadda");
            }
            else if (fanrpmInt < classrpmInt)
            {
                MessageBox.Show(this, "Fanrpm is less then Classrpm");
            }
        }


    }

I believe my problem stems in converting my database item into something usable to compare too. Also i think that if my setup works as intended it may tell a user entering 500 at each entry the textbox will refresh and redo the query leading to potential wasted processing power?

-Edit my sql statement works, Though putting the data into a list may be one of my problems.

2
  • 3
    Choose better variable names. dog, cat, bark, and meow are not descriptive or helpful. Have you used your debugger to step through the code? Commented Aug 5, 2014 at 20:21
  • changed it to make sense i was thinking about that right as you posted. I used the debugger, the sql statement is pulling the data and its going into the list. From there the first issue is its not moving into my classrpm string Commented Aug 5, 2014 at 20:24

2 Answers 2

1

No way that you could convert a List(Of String) to a single string in that way.
And what if your user types not a number in your textbox?

However, if, as you have said, your DataTable contains always only one row then you could simplify your code to something like this

private void textBox1_TextChanged(object sender, EventArgs e)
{
    int classRPM;
    int fanRPM;
    using (Fanrpm ds = new Fanrpm(....)
    {
        DataTable dt = ds.dataset.Tables[0];

        // Get and convert the value in the field named ClassRPM (assuming is a string)
        classRPM = Convert.ToInt32(dt.Rows[0].Field<string>("ClassRPM"));

        // Now check if the textbox contains a valid number
        if(!Int32.TryParse(txfanrpm.Text, out fanRPM))
        {
            MessageBox.Show("Not a number....");
            return;
        }

        // Start your logic
        if (fanRPM >= classRPM)
        {
            MessageBox.Show(.....);
        }
        else if (fanRPM < classRPM)
        {
            MessageBox.Show(.......);
        }
    }
}

I think that a TextChanged event is not the proper place for this kind of code. This will trigger everytime your user presses a key and result in a trip to the database for every key press. So if your user wants to type 500 you end asking the db first for "5", then for "50" and finally for "500".

I would prefer to let the user types freely its values and then check when a verify button is pressed or, if this is not possible, to check in the Control.Validating event.

Sign up to request clarification or add additional context in comments.

6 Comments

other selections are made before any button pressing after this i could tie it into the combobox below this textbox that way the user can enter his data then move on before it checks and informs them, I also have code already to makesure user input is only numbers just did not include it.
i tried your code, tweeked it a bit, and i'm having a problem where either the Classrpm = convert.ToInt32(dt.rows...) is not working correctly, OR/and it then skips the logic section and moves on. This may just be what you brought up with control.validating and ill look into that as well, but if you have any insight on this id be greatful :D
What error did you get in the line that set the ClassRpm variable? Perhaps if you post a new question with the updated code we can check the new situation. Post a link to the new question here
basically stepping through it hits that classrpm=convert line does it then skips the if statements of the logic. Ill create a new question, i added some stuff to filter out characters.
Just check to be sure that your field is named ClassRPM and that the line contains a numeric value that can be converted to an integer
|
1

Calling List.ToString() just returns "System.Collections.Generic.List`1[System.String]" rather than any items in the string. (The "ToString()" method is actually inherited from Object, and just returns a string that represents what type of object the object is, except in special cases like StringBuilder.)

I would avoid the TextChanged event because that event will cause your code to execute ever keystroke, which might cause errors when a value is partially entered, or when a user accidentally enters and invalid value and has to backspace.

1 Comment

I dont have the option to let this work through a click event, but my program has a series of comboboxes 5 before this text box and 2 after, and i could tie this into the combobox event.

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.