I have a database, I think when I initially generated it, I didn’t bother to select the “Generate Delete function” option and now I find myself wanting to add that in.
Currently, I have a gridview bound to the database with a delete button for each row. It seems like I should be able to just run a custom delete function with the following code;
protected void gvUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
using (SqlConnection conn = new SqlConnection(database.ConnectionString))
{
SqlCommand command = conn.CreateCommand();
command.CommandText = "DELETE FROM userlist WHERE Username='@insUsername'";
command.CommandType = System.Data.CommandType.Text;
command.Parameters.Add(new SqlParameter("insUsername", gvUsers.Rows[e.RowIndex].Cells[1].Text));
conn.Open();
command.ExecuteNonQuery();
}
}
I had no issues when using the above code as an insert function…I think it has something to do with the fact that the button is part of the GridView? At the moment I get; Deleting is not supported by data source ‘database’ unless DeleteCommand is specified.
Any help on this would be great, thanks.
You can put SQL into the DeleteCommand and not just a procedure name…
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.deletecommand.aspx shows how this is done but the key line is:
This should get around the problem of it not allowing delete without a deletecommand. 🙂
Edit to add:
One other alternative that I found suggested that that error is thrown after that function is run when it comes to pass the delete on. You can use
e.Cancel=trueto cancel the delete event after you have done the delete yourself. Source