1

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();
    }
Sanjeevakumar Hiremath
  • 10,985
  • 3
  • 41
  • 46
6TTW014
  • 627
  • 2
  • 11
  • 24
  • 7
    Can you post the stack trace? Do you know which line is throwing the error? – Andy Mar 25 '11 at 01:16
  • 1
    @Jon, how is the compiler going to help in finding the cause of a runtime exception? – Kirk Woll Mar 25 '11 at 01:19
  • Have edited post to show where the error occurs. – 6TTW014 Mar 25 '11 at 01:21
  • @Kirk: My bad. For some reason was thinking exception message (=stack trace) and converted that to compiler error message. Sorry :) – Jon Mar 25 '11 at 01:21
  • 2
    @Kimmy, that line could not possibly have been the direct cause of this, since the only candidate for an NRE (`employeesBindingSource`) was dereferenced earlier. Thus, what is `employeesBindingSource` to begin with? Possibly the implementation of `Count` is throwing the error. Can you please post the entire stack trace? – Kirk Woll Mar 25 '11 at 01:25
  • @Kimmy25 Can you please post how you define/initialise the variable "employeesBindingSource". – Jamie Keeling Mar 25 '11 at 01:32
  • This may sounds strange but nothing appears in the Call Stack window :S – 6TTW014 Mar 25 '11 at 01:34
  • I just added it to the Windows Form and connected it etc in the properties window. – 6TTW014 Mar 25 '11 at 01:36
  • @Kimmy25 So are you saying the employeesBindingSource is a component? (Dragged from the Toolbox onto the Design window?) – Jamie Keeling Mar 25 '11 at 01:39
  • Can't you put a breakpoint at the first line of that method and then single-step to see exactly where the error occurs? Before executing each line, use the debugger to examine every variable that will be used. I think you'll find the error fairly quickly that way. – Jim Mischel Mar 25 '11 at 02:25
  • which line you are getting an exception? – Anuraj Mar 25 '11 at 02:49

1 Answers1

2

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.

6TTW014
  • 627
  • 2
  • 11
  • 24