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">
                   <ItemTemplate>
                       <asp:Button ID="btnedit" runat="server" Text="Edit" />
                       <asp:Button ID="btnDelete" runat="server" Text="Detete" />
                   </ItemTemplate>
                   <EditItemTemplate>
                        <asp:Button ID="btnupdate" CommandName="update" runat="server" Text="Edit" />
                       <asp:Button ID="btncancel" CommandName="cancel" runat="server" Text="Detete" />
                   </EditItemTemplate>


;


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();
con.Open();

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


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

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

cmd.ExecuteNonQuery();
con.Close();
GridView1.DataBind();

}


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
Posted

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:

http://msdn.microsoft.com/en-us/library/system.web.ui.control.id%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.clientidmode(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.inamingcontainer%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.control.namingcontainer%28v=vs.110%29.aspx[^].

—SA
   

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