Reputation: 451
I have a problem deleting the selected row from my database,In fact,I have a Form in C# that contains a dataGridView connected to a database and a button "Delete",when I clic on the button this should delete the informations from the selected row (cell[0] and cell [1]) in the dataGridView and the database.Now,I face a problem deleting the selected row from the database this is my code:
private void button4_Click(object sender, EventArgs e)
{
if (journalDataGridView.SelectedRows.Count == 1)
{
DataGridViewRow row = journalDataGridView.SelectedRows[0];
journalDataGridView.Rows.Remove(row);
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand sql = new SqlCommand("delete from journal where code_journal='" + journalDataGridView.CurrentRow.Cells[0].Value.ToString() + "'AND intitule='" + journalDataGridView.CurrentRow.Cells[1].Value.ToString() + "';", connection);
connection.Close();
}
}
the dataGridView contains two columns "code_journal and initule" thanks for an Help
Upvotes: 0
Views: 77
Reputation: 5636
In addition to the answer provided by sorton9999, another issue is you aren't doing anything with your SqlCommand
object.
After creating it, you need to execute it:
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand sql = new SqlCommand("delete from journal where code_journal='" + journalDataGridView.CurrentRow.Cells[0].Value.ToString() + "'AND intitule='" + journalDataGridView.CurrentRow.Cells[1].Value.ToString() + "';", connection);
sql.ExecuteNonQuery();
connection.Close();
You are opening yourself up to possible Sql injection by doing string concatenation, use a parameterized query instead. Additionally, you should be wrapping your SqlConnection
and SqlCommand
in using
statements to ensure they are properly disposed. Something like this:
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand sql = new SqlCommand("delete from journal where code_journal=@codeJournal AND initule=@inituleVal", connection))
{
cmd.Parameters.AddWithValue("@codeJournal", journalDataGridView.CurrentRow.Cells[0].Value.ToString());
cmd.Parameters.AddWithValue("@inituleVal", journalDataGridView.CurrentRow.Cells[1].Value.ToString());
connection.Open();
sql.ExecuteNonQuery();
}
}
Upvotes: 2
Reputation: 81610
You are removing the row and then referencing the wrong row with the CurrentRow property.
You are also not using parameters to avoid sql injection.
You are also not executing the command:
DataGridViewRow row = journalDataGridView.SelectedRows[0];
connection.Open();
using (SqlCommand sql = new SqlCommand("delete from journal where code_journal=@codeJournal..", connection)) {
sql.Parameters.AddWithValue("@codeJournal", row.Cells[0].Value.ToString());
sql.ExecuteNonQuery();
}
connection.Close();
journalDataGridView.Rows.Remove(row);
Upvotes: 5
Reputation: 182
Could it be as easy as, you don't have a space between your single-quote (') and the word AND in your SQL statement?
worth a try...
Upvotes: -1