Last post Jul 10, 2019 05:53 AM by samwu
Jul 09, 2019 01:49 PM|E.RU|LINK
I have the followingcode which deletes row from repeater, however it doesnt delete row from database. any help/sample code as to how I would code to delete row from database would be great
'------CHECKBOX DELETE SELECTED
Dim PKs = ""
For Each item As RepeaterItem In Repeater1.Items
Dim chb As CheckBox = TryCast(item.FindControl("CheckBox1"), CheckBox)
Dim txtid As TextBox = TryCast(item.FindControl("txtbx"), TextBox)
If chb.Checked Then
PKs += txtid.Text & ","
Dim dt As DataTable = GetComItms()
Dim results = (From myRow In dt.AsEnumerable() Where PKs.IndexOf(myRow.Field(Of String)("x")) > -1 Select myRow).ToList()
For Each row As DataRow In results
'-- rebind after delete
Me.Repeater1.DataSource = dt
Jul 09, 2019 02:02 PM|mgebhard|LINK
There's no SQL DELETE command shown. The common pattern is to delete a row, query the table, then rebind server control. Anyway, this pattern has been around a long time in Web Forms and there is lot of sample code on the internet.
Please try reading the openly published documentation.
Jul 09, 2019 03:13 PM|PatriceSc|LINK
It's not that easy. You can use Delete to mark a row as being deleted and then use AcceptChanges to send back changes to your DataTable to the server...Details are found at https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/updating-data-sources-with-dataadapters
It is not widely discussed as it is few years old compared with the 20 year old Framework but if you want to use Web Forms and are new I would suggest to check https://docs.microsoft.com/en-us/aspnet/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data
IIt should be cleaner, quite simpler while you get past the initial learning curve and it is similar to what you'll find in MVC for example....
Jul 10, 2019 05:53 AM|samwu|LINK
According to your description, I wrote a demo for you as reference.
In my demo, I find the CustomerId of the row to delete, and then use the sql delete statement to delete the data in the database based on this CustomerId.
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<table id="tblContacts" border="1" >
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="DeleteRow" CommandArgument='<%# Bind("CustomerId") %>'>Delete</asp:LinkButton>
<asp:CheckBox ID="CheckBox1" runat="server" />
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
Private str As String = ConfigurationManager.ConnectionStrings("constr527").ConnectionString
Public Sub Bind()
Using con As SqlConnection = New SqlConnection(str)
Dim sql As String = "Select * from Customer"
Using adapter As SqlDataAdapter = New SqlDataAdapter(sql, con)
Dim dt As DataTable = New DataTable()
Repeater1.DataSource = dt
Protected Sub Repeater1_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs)
For item As Integer = 0 To Repeater1.Items.Count - 1
Dim chb As CheckBox = TryCast(Repeater1.Items(item).FindControl("CheckBox1"), CheckBox)
If chb.Checked Then
If e.CommandName = "DeleteRow" Then
Dim CustomerId As Integer = Convert.ToInt32(e.CommandArgument)
Dim con As SqlConnection = New SqlConnection(str)
Dim cmdText As String = "DELETE FROM Customer WHERE CustomerId=@CustomerId"
Dim cmd As SqlCommand = New SqlCommand(cmdText, con)