1

我已经阅读了有关此的其他问题,但看不到我的代码在哪里或为什么抛出此错误。我的代码如下,如果有人能看到我哪里出错了。

    private void btnSignIn_Click(object sender, EventArgs e)
    {
        sqlConnectionNW.ConnectionString = "Data Source=" + server + ";Initial Catalog=Northwind;Integrated Security=True";
        try
        {
            sqlConnectionNW.Open();
            String enteredDate = cmbDay.SelectedItem.ToString() + "/" + cmbMonth.SelectedItem.ToString() + "/" + cmbYear.SelectedItem.ToString();

            if ((txtEmployeeID.TextLength != 0) && (enteredDate.Length != 0))
            {
                int ID = Convert.ToInt32(txtEmployeeID.Text);
                employeesBindingSource.Filter = "EmployeeID ='" + txtEmployeeID.Text + "'";
                String birthDate;
                birthDate = dsEmployees.Employees.FindByEmployeeID(ID).BirthDate.ToShortDateString();

                if (employeesBindingSource.Count > 0)   //GETS TO HERE AND THEN GOES TO CATCH EX
                {
                    sqlConnectionNW.Close();
                    if (enteredDate.ToString() == birthDate.ToString())
                    {
                        frmMenu frmMainMenu = new frmMenu();
                        frmMainMenu.server = server;
                        frmMainMenu.employeeID = txtEmployeeID.Text;
                        MessageBox.Show("Welcome to the Northwind Ordering System");
                        this.Hide();
                        frmMainMenu.Show();
                    }
                    else
                    {
                        MessageBox.Show("The birth date you have entered is incorrect");
                    }
                }
                else
                {
                    MessageBox.Show("The Employee ID you have entered does not exist");
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        sqlConnectionNW.Close();
    }
4

1 回答 1

2

设法解决这个问题。我改变了这个:

if ((txtEmployeeID.TextLength != 0) && (enteredDate.Length != 0))
            {

                int ID = Convert.ToInt32(txtEmployeeID.Text);

                employeesBindingSource.Filter = "EmployeeID ='" + txtEmployeeID.Text + "'";

                String birthDate;

                birthDate = dsEmployees.Employees.FindByEmployeeID(ID).BirthDate.ToShortDateString();    // FROM HERE

                if (employeesBindingSource.Count != 0)
                {

                    sqlConnectionNW.Close();

对此:

if ((txtEmployeeID.TextLength != 0) && (enteredDate.Length != 0)) {

                int ID = Convert.ToInt32(txtEmployeeID.Text);

                employeesBindingSource.Filter = "EmployeeID ='" + txtEmployeeID.Text + "'";

                String birthDate;

                if (employeesBindingSource.Count != 0)
                {
                    birthDate = dsEmployees.Employees.FindByEmployeeID(ID).BirthDate.ToShortDateString();    //TO HERE

                    sqlConnectionNW.Close();

这只是将“birthDate = ...”放在 if 语句之后的情况。

于 2011-03-25T12:18:06.443 回答