C# Error 'Object Reference Not Set To An Instance Of An Object'

こ雲淡風輕ζ 提交于 2019-12-24 22:18:07

问题


I've read other questions about this but don't see where or why my code is throwing this error. The code I have is below, if anyone can see where i'm going wrong.

    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();
    }

回答1:


Manage to solve this problem. I changed this:

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();

To this:

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();

It was just a case of putting the "birthDate = ..." after the if statement.



来源:https://stackoverflow.com/questions/5427509/c-sharp-error-object-reference-not-set-to-an-instance-of-an-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!