Delete Items in DataList using RadioButton CheckBox in VB.NET

In this article we will delete items in a DataList control from the database by selecting RadioButton and CheckBox using a delete Button.

Table Structure


Create table student (sid varchar(50) primary key,sname varchar(50),saddress   varchar(50),smarks int,year varchar(50))


Table-in-SQL-Server.gif

Default.aspx code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
    <asp:label id="Label1"  runat="server" Font-Bold="True"></asp:label>
    <asp:datalist id="DataList1" runat="server" DataKeyField="sid">
    <HeaderTemplate>
    <table>
<tr>
<
th><font color="red">ID</font></th>
<th><font color="red">Name</font></th>
<th><font color="red">Address</font></th>
<th><font color="red">Marks</font></th>
<th><font color="red">Year</font></th>
</tr>
</
HeaderTemplate>
    <ItemTemplate>
    <tr>
    <td><asp:RadioButton ID="r1" Text='<%#container.dataitem("sid")%>' Runat="server">
    </asp:RadioButton></td>
    <td><%#container.dataitem("sid")%></td>
    <td><%#container.dataitem("sname")%></td>
    <td><%#container.dataitem("saddress")%></td>
    <td><%#container.dataitem("smarks")%></td>
    <td><%#container.dataitem("year")%></td>
    </tr>
   </ItemTemplate>
    <HeaderStyle BackColor="#FFCC66" />
    <FooterTemplate>
</table>
</
FooterTemplate>
    </asp:datalist>
    <asp:button id="btndelete" runat="server" Text="Select Radiobutton Delete" Font-Bold="True">
    </asp:button>
    <hr />
    <asp:datalist id="DataList2" runat="server" DataKeyField="sid">
    <HeaderTemplate>
    <table>    <tr>
    <th><font color="red">ID</font></th>
    <th><font color="red">Name</font></th>
    <th><font color="red">Address</font></th>
    <th><font color="red">Marks</font></th>
    <th><font color="red">Year</font></th>
   </tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
   <td><asp:CheckBox ID="chkid" Text='<%#container.dataitem("sid")%>' Runat="server"/></td>
    <td><%#container.dataitem("sid")%></td>
    <td><%#container.dataitem("sname")%></td>
    <td><%#container.dataitem("saddress")%></td>
    <td><%#container.dataitem("smarks")%></td>
    <td><%#container.dataitem("year")%></td>
    </tr>
    </ItemTemplate>
    <HeaderStyle BackColor="#FFCC66" />
    <FooterTemplate>
</table>
</
FooterTemplate>
    </asp:datalist>
    <asp:button id="btndelete1" runat="server" Text="Select CheckBox Delete" Font-Bold="True">
    </asp:button>
    </div>
    </form>
</body>
</
html>

Default.aspx.vb code

Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
    Dim con As New SqlConnection(strConnString)
    Dim sqlda As SqlDataAdapter

    Dim com As SqlCommand
    Dim ds As DataSet
    Dim str As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) HandlesMe.Load
        If Not IsPostBack Then
            displayinlist()
            displayinlist1()

        End If
    End Sub
    Private Sub displayinlist()
        Try
            con.Open()
            str = "select * from student"
            com = New SqlCommand(str, con)
            sqlda = New SqlDataAdapter(com)
            ds = New DataSet
            sqlda.Fill(ds, "student")
            con.Close()
            DataList1.DataSource = ds
            DataList1.DataMember = "student"
            DataList1.DataBind()
        Catch ex As Exception
            Label1.Text = ex.Message
        End Try
    End Sub
    Private Sub displayinlist1()
        Try
            con.Open()
            str = "select * from student"
            com = New SqlCommand(str, con)
            sqlda = New SqlDataAdapter(com)
            ds = New DataSet
            sqlda.Fill(ds, "student")
            con.Close()
            DataList2.DataSource = ds
            DataList2.DataMember = "student"
            DataList2.DataBind()
        Catch ex As Exception
            Label1.Text = ex.Message
        End Try
    End Sub
    Protected Sub btndelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handlesbtndelete.Click
        Dim s, s1 As String
        Dim flag As Byte
        Dim dli As DataListItem
        For Each dli In DataList1.Items
            Dim rb As RadioButton = DirectCast(dli.FindControl("r1"), RadioButton)
            If rb.Checked Then
                flag = 1
                s += "'" & DataList1.DataKeys(dli.ItemIndex).ToString() & "'" + ","
            End If
        Next
        If flag = 1 Then
            btndelete.Enabled = True
            s1 = Mid(s, 1, Len(s) - 1)
            con.Open()
            str = "delete from student where sid in(" & s1 & ")"
            com = New SqlCommand(str, con)
            com.ExecuteNonQuery()
            con.Close()
            displayinlist()
            displayinlist1()
        End If
    End Sub
    Protected Sub btndelete1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handlesbtndelete1.Click
        Dim s2, s3 As String
        Dim flag As Byte
        Dim dli As DataListItem
        For Each dli In DataList2.Items
            Dim cb As CheckBox = DirectCast(dli.FindControl("chkid"), CheckBox)
            If cb.Checked Then
                flag = 1
                s2 += "'" & DataList2.DataKeys(dli.ItemIndex).ToString() & "'" + ","
            End If
        Next
        If flag = 1 Then
            btndelete.Enabled = True
            s3 = Mid(s2, 1, Len(s2) - 1)
            con.Open()
            str = "delete from student where sid in(" & s3 & ")"
            com = New SqlCommand(str, con)
            com.ExecuteNonQuery()
            con.Close()
            displayinlist()
            displayinlist1()
        End If
    End Sub
End Class

Output

 

Delete-datalist-item-in-VB.NET.gif


Similar Articles