1

I am using C# on Winforms to populate a datagridview from a class in a library. I have been able to get the datagridview to populate with all of my SQL Server data using a stored procedure.

I wish to allow the end user to modify the cells in the datagridview, which will update the SQL Server table through a second stored procedure. However, I cannot seem to get the checkbox value to be recognized once the update procedure is called.

My update query is as follows:

public void UpdateEmployee(int empID, string initials, string firstName, string lastName, int active)
{
    using (SqlConnection conn = new SqlConnection(DatabaseConnection.Database))
    {
        conn.Open();

        using (SqlCommand cmd = conn.CreateCommand())
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "UpdateEmployee";

            cmd.Parameters.AddWithValue("@EmpID", empID);
            cmd.Parameters.AddWithValue("@Initials", initials);
            cmd.Parameters.AddWithValue("@FirstName", firstName);
            cmd.Parameters.AddWithValue("@LastName", lastName);
            cmd.Parameters.AddWithValue("@Acvite", active);

            cmd.ExecuteNonQuery();
        }
    }
}

After creating an instance of the library class, I was trying to call the Update method with this line of code:

empData.UpdateEmployee(Convert.ToInt32(dataGridView1[0, e.RowIndex].Value),  // EmpID
        dataGridView1[1, e.RowIndex].Value.ToString(), //Initials
        dataGridView1[2, e.RowIndex].Value.ToString(), //FirstName
        dataGridView1[3, e.RowIndex].Value.ToString(), //LastName
        Convert.ToInt16(dataGridView1[4, e.RowIndex].EditedFormattedValue)); // Active checkbox 

I am getting an error stating that @Active has not been provided.

Can anyone help me resolve this issue?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Wurm
  • 13
  • 8
  • 2
    You're getting the error because you misspelled `@Active`. But more importantly, [can we please stop using `AddWithValue()`, already?](http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/) – Siyual Apr 04 '17 at 18:23
  • 1
    @Siyual I agree but in this case it shouldn't be a big deal. The datatypes are specified in the parameters because they are using a stored procedure. – Sean Lange Apr 04 '17 at 18:24
  • Is there a disadvantage to using AddWithValue()? – Wurm Apr 04 '17 at 18:29
  • @Siyual - please feel free to send me a link to a web site on alternatives to AddWithValue(). I was just taught this method and I welcome better ways. – Wurm Apr 04 '17 at 18:39
  • @Wurm The link in my first comment explains the downfalls and alternatives. – Siyual Apr 04 '17 at 18:40
  • I have edited my code, just so you wouldn't lose sleep tonight, @Siyual. Thank you for the link. – Wurm Apr 04 '17 at 19:00

1 Answers1

1
cmd.Parameters.AddWithValue("@Acvite", active);

Spelling error? Active not Acvite?

devzero
  • 2,510
  • 4
  • 35
  • 55
  • That did get rid of that error, but the update isn't working :( – Wurm Apr 04 '17 at 18:22
  • And thank you so much for seeing that! I cannot believe i didn't see that before. – Wurm Apr 04 '17 at 18:23
  • 1
    I have it working, now! Thank you all for the fast replies! I ended up calling the update method from the CellEndEdit event. :0) – Wurm Apr 04 '17 at 18:38