Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to delete a particular row from datalist through delete button click.
Plz help me
Thanks in advnc
Posted

Have a look at this link...

Delete row from datalist^]
 
Share this answer
 
aspx:
XML
<asp:DataList ID="DataList1" runat="server" DataKeyField="ID" OnDeleteCommand="DataList1_DeleteCommand">
        <ItemTemplate>
            <asp:LinkButton ID="lnkDelete" runat="server" CommandName="delete">Delete</asp:LinkButton>
        </ItemTemplate>
    </asp:DataList>

code behind:
C#
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
   {
       int ID = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
       SqlCommand mySqlCommand = new SqlCommand("delete from tab2 where ID=@ID", con);
       mySqlCommand.Parameters.Add("@ID", SqlDbType.Int).Value = ID;    
       if (con.State == ConnectionState.Closed)
           con.Open();
       mySqlCommand.ExecuteNonQuery();       
       if (con.State == ConnectionState.Open)
           con.Close();
       DataList1.DataBind();
   }
 
Share this answer
 
 
Share this answer
 

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