ARTICLE

Delete items in datalist using Radiobutton Checkbox in VB.NET

Posted by Satyapriya Nayak Articles | ASP.NET using VB.NET August 19, 2011
In this article we will delete items in a datalist control from the database by selecting radiobutton and checkbox using a delete button.
Reader Level:

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))

datalist1.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) Handles Me.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) Handles btndelete.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) Handles btndelete1.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

datalist2.gif

Thanks for reading.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor