Click here to Skip to main content
14,773,207 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
need help . i am deleting particular row in my grid
there is a delete button in my edititem template

<asp:TemplateField HeaderText="Edit">
                       <asp:Button ID="btnedit" runat="server" Text="Edit" />
                       <asp:Button ID="btnDelete" runat="server" Text="Detete" />
                        <asp:Button ID="btnupdate" CommandName="update" runat="server" Text="Edit" />
                       <asp:Button ID="btncancel" CommandName="cancel" runat="server" Text="Detete" />


and my c# codes are

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
SqlCommand cmd=new SqlCommand();

int idd = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["id"].ToString());
Button delete = (Button)GridView1.Rows[e.RowIndex].FindControl("btncancel");

// int idd = Int32.Parse(GridView1.DataKeys[e.RowIndex].Values["id"].ToString());

cmd = new SqlCommand("delete from food where id="+idd, con);



and i am getting this error
Exception Details: System.Web.HttpException: Multiple controls with the same ID 'lname' were found. FindControl requires that controls have unique IDs.

sir i want to know the reason why this error is coming and what i am missing in my codes. this is a learning experience so i request to you sir please help me i am a new student

1 solution

Your error is unrelated to your question as formulated. You just need to understand that HTML requires that id attributes should always have values unique on the page. Not only you get this exception now, but, in general case, the browser behavior is unpredictable if you violate it. It would be just not valid HTML.

For relationships between HTML id and ASP.NET Control.ID or Control.ClientID, please learn the following topics:[^],[^],[^],[^],[^].


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900