C# .NET - InsertCommand Object reference not set to an instance of an object.

Asked By six fourty on 31-Oct-14 12:00 PM

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);
         da.InsertCommand.Parameters.Add("@TimeOut", SqlDbType.NVarChar, 50).Value = currenttime.Text;
        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();
       }

Web Star replied to six fourty on 04-Apr-10 02:32 PM
u change these three line
da.UpdateCommand = new SqlCommand("Update Attendancedetail Set TimeOut=@TimeOut where ID=@ID", conn);
                 da.InsertCommand.Parameters.Add("@TimeOut", SqlDbType.NVarChar, 50).Value = currenttime.Text;
                da.InsertCommand.Parameters.Add("@ID", SqlDbType.Int).Value = employee_InformationDataGridView.Rows[i].Cells[0].Value.ToString();

as folllows

da.UpdateCommand = new SqlCommand("Update Attendancedetail Set TimeOut=@TimeOut where ID=@ID", conn);
                 da.UpdateCommand .Parameters.Add("@TimeOut", SqlDbType.NVarChar, 50).Value = currenttime.Text;
                da.UpdateCommand .Parameters.Add("@ID", SqlDbType.Int).Value = employee_InformationDataGridView.Rows[i].Cells[0].Value.ToString();
Santhosh N replied to six fourty on 05-Apr-10 01:19 AM
you created object for update statement and trying to add parameter to the insert command which is not instantiated..
Change both of them to the similar one as per your requirement
                 da.UpdateCommand = new SqlCommand("Update Attendancedetail Set TimeOut=@TimeOut where ID=@ID", conn);
                 da.InsertCommand.Parameters.Add("@TimeOut", SqlDbType.NVarChar, 50).Value = currenttime.Text;