I have a gridview with some data from my database.
However, when you point to a delete link, the link refers to an invalid identifier.
I want it to use the identifier from the database.
This question is very much related to this - but it never received an answer.
My GridView code looks like this:
<asp:GridView ID="GridView1" runat="server" OnRowDeleting="GridView1_RowDeleting1" AutoGenerateColumns="false" DataKeyNames="id"> <Columns> <asp:BoundField HeaderText="Name" DataField="name" /> <asp:BoundField HeaderText="Email" DataField="email" /> <asp:BoundField HeaderText="Comment" DataField="comment" /> <asp:CommandField ShowDeleteButton="True" /> </Columns> </asp:GridView>
How to link DeleteButton link with the correct ID?
Update
The code for loading a page that puts data in a datatable:
protected void Page_Load(object sender, EventArgs e) { DataTable commentsTable = null; commentsTable = new DataTable("Comments"); using (SqlDataReader reader = studentManager.getCommentsFromDB()) { commentsTable.Load(reader); GridView1.DataSource = commentsTable; GridView1.DataBind(); } }
getCommentsFromDB:
public SqlDataReader getCommentsFromDB() { SqlConnection conn = dal.connectDatabase(); conn.Open(); cmd = new SqlCommand(@"SELECT id, name, email, comment FROM GuestBook", conn); rdr = cmd.ExecuteReader(); return rdr; }
source share