hi Guy's i got error Object reference not set to an instance of an object and it show me the Highlighted area

private void time_in_Click(object sender, EventArgs e)
        {
            employee_InformationDataGridView.SelectedCells[0].Style.ForeColor = Color.Green;

                time_in.Enabled = false;
                time_out.Enabled = true;
                DateTime today = DateTime.Today;
                int i;
                i = employee_InformationDataGridView.SelectedCells[0].RowIndex;
                SqlConnection conn = new SqlConnection("Data Source=Goldfish-pc\\sqlexpress;Database=StaffAttendance;Integrated Security=True;" + GlobalClass.hostIP + "");
                SqlDataAdapter da = new SqlDataAdapter();
                da.InsertCommand = new SqlCommand("Insert into AttendanceDetail Values(@EmployeeID,@FullName,@CurrentDay,@TimeIn,@TimeOut)", conn);
                da.InsertCommand.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = employee_IdTextBox.Text;
                da.InsertCommand.Parameters.Add("@FullName", SqlDbType.NVarChar,50).Value = employee_InformationDataGridView.Rows[i].Cells[0].Value.ToString();
                da.InsertCommand.Parameters.Add("@CurrentDay", SqlDbType.NVarChar,50).Value = today;
                da.InsertCommand.Parameters.Add("@TimeIn", SqlDbType.NVarChar,50).Value = currenttime.Text;
                da.InsertCommand.Parameters.Add("@TimeOut", SqlDbType.NVarChar,50).Value = "0";
                conn.Open();
                if (employee_IdTextBox.Text == "")
                {
                    MessageBox.Show("Plaese Enter Employee ID");
                    employee_IdTextBox.Focus();
                }

                int j = da.InsertCommand.ExecuteNonQuery();
                MessageBox.Show(j.ToString()+ " record inserted");
                employee_IdTextBox.Text= "";

        }

        private void time_out_Click(object sender, EventArgs e)
        {
           employee_InformationDataGridView.SelectedCells[0].Style.ForeColor = Color.Red;

                time_out.Enabled = false;
                time_in.Enabled = true;
                int i;
                i = employee_InformationDataGridView.SelectedCells[0].RowIndex;
                SqlConnection conn = new SqlConnection("Data Source=Goldfish-pc\\sqlexpress;Database=StaffAttendance;Integrated Security=True;" + GlobalClass.hostIP + "");
                SqlDataAdapter da = new SqlDataAdapter();
               da.UpdateCommand = new SqlCommand("Update Attendancedetail Set TimeOut=@TimeOut where ID=@ID", conn);
                 [COLOR="Green"]da.InsertCommand.Parameters.Add("@TimeOut", SqlDbType.NVarChar, 50).Value = currenttime.Text;[/COLOR]
                da.InsertCommand.Parameters.Add("@ID", SqlDbType.Int).Value = employee_InformationDataGridView.Rows[i].Cells[0].Value.ToString();
                conn.Open();
                int j = da.UpdateCommand.ExecuteNonQuery();
                MessageBox.Show(j.ToString() + " record inserted");
                 conn.Close();
         }

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Looking at this code I would say this issue might be caused by currenttime.Text, If you run this code in debug mode you should be able to view the object's state and backtrack the issue from there

Have you created a variable called currenttime? IF so, have you instantiated and assigned a value to it.
If you were trying to add the current time without using a variable you need to check out DateTime.Now and its various members.

This article has been dead for over six months. Start a new discussion instead.