Reputation: 23
I have to use SqlDataAdapter in this issue. Why this code doesn't want to delete a specified row?
There are no errors, script is entering to if statement.
id_book is correct, table name: Book
public static void DeleteBook(int id_book)
{
using (var connection = new SqlConnection(DbCon.ConnectionString))
{
connection.Open();
var adapter = new SqlDataAdapter("SELECT * FROM Book", connection);
var builder = new SqlCommandBuilder(adapter);
var table = new DataTable();
adapter.Fill(table);
var rowToDelete = table.Select($"id_book = {id_book}").FirstOrDefault();
if (rowToDelete != null)
{
table.Rows.Remove(rowToDelete);
adapter.Update(table);
}
}
}
DELETE FROM Book WHERE id_book = 20; -> works correctly in DB
Upvotes: 1
Views: 47