From the Delete code that I have, when I clicked on button to delete a particular data that in a gridview row, it deleted every data in that table, including data that belongs to other users. Leaving the whole table empty.
How can I have a code that will delete ONLY data that belongs to the admin that is logged in, and to the row that is selected, and not delete data of all users?
Example if I have 4 Admins and also have other users as user under the Admins account and I want to delete a particular record, and the record is based on the Admin that is logged in. I must say here that I am using a different table column as DataKeyNames, which is not the Primary Key (Id).
In some gridviews, I used Name as DataKeyNames. Then in some, I use Document as DataKeyNames. I don’t if it is a problem if I don’t use Primary Key (Id) as DataKeyNames
But this code that I have deletes all the entire data in that table.
Example if I login to my account as Admin and go to the gridview page, and select a gridview row and click on delete button, it deletes all data in that table including the data belonging to other Admin accounts. It is only Admin that has full control to delete a record.
When users login who are not Admin, they do not see the delete button and checkbox.
This code deletes entire record in that table including the ones for other accounts
protected void deletebtn_Click(object sender, EventArgs e)
{
int i = 0;
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chkdelete = (CheckBox)row.FindControl("checked");
if (chkdelete.Checked)
{
using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security = True"))
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM DocumentTable WHERE Id=@Id", con))
{
cmd.Parameters.AddWithValue("@Id", i);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
string message = "Document " + row.Cells[1].Text + " has been deleted.";
ClientScript.RegisterStartupScript(this.GetType(), "alert" + i, "alert('" + message + "');", true);
}
i++;
}
}