开发者

Try Catch, number in a text field

I made a winform that draws data from a database in SQL, to search I have Combo Boxes - cboField and cboOperator and I am using txtValue. The form is about cars, when I put in (cboField) Payment (cboOperator) = (txtValue) Ford it crashes. How do i put something in to stop it from doing this and the other way round, e.g. Make of car = 5

    private void btnRun_Click(object sender, EventArgs e)
    {
        if (cboField.SelectedIndex == -1)
        {
          开发者_如何学运维  return;
        }

        if (cboOperation.SelectedIndex == -1)
        {
            return;
        }

        if (txtValue.TextLength == 0)
        {
            return;
        }

        string filter;
        filter = "[" + cboField.Text + "]";

        filter += cboOperation.Text + "'" + txtValue.Text + "'";

        tblCarBindingSource.Filter = filter;

This is the click event for the Run button

it says Evaluation Exception was Unhandled on the last line of my code i pasted


I think you're trying to prevent the user from putting something like "5" in your text fields because it causes your data binding source filter to throw an EvaluateException?

Hard to tell exactly what you're looking for but if that's the case, try something like this:

try
{
    tblCarBindingSource.Filter = filter;
}
catch
{
    MessageBox.Show("Please enter valid values in your text fields.");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜