<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Page Language="VB" %>
<script runat="server">
Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Sub Page_Load(sender As Object, e As EventArgs)
Dim strConnString As String
strConnString = "Server=DARKEDITION;Uid=sa;PASSWORD=sqlserver;database=testlogin;Max Pool Size=400;Connect Timeout=600;"
objConn = New SqlConnection(strConnString)
objConn.Open()
If Not Page.IsPostBack() Then
BindData()
End If
Me.btnDelete.Attributes.Add("OnClick", "return confirm('Delete Record?');")
End Sub
Sub BindData()
Dim strSQL As String
strSQL = "SELECT * FROM member"
Dim dtReader As SqlDataReader
objCmd = New SqlCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
'*** BindData to Repeater ***'
myRepeater.DataSource = dtReader
myRepeater.DataBind()
dtReader.Close()
dtReader = Nothing
End Sub
Sub Page_UnLoad()
objConn.Close()
objConn = Nothing
End Sub
Sub btnDelete_OnClick(ByVal sender As Object, ByVal e As EventArgs)
Dim chkDel As CheckBox
Dim lblID As Label
Dim strSQL As String
Dim i As Integer
For i = 0 To myRepeater.Items.Count - 1
chkDel = myRepeater.Items(i).FindControl("chkDel")
lblID = myRepeater.Items(i).FindControl("lblCustomerID")
If chkDel.Checked = True Then
strSQL = "DELETE FROM member WHERE CustomerID = '" & lblID.Text & "' "
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
End If
Next
BindData()
End Sub
Sub myRepeater_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles myRepeater.ItemDataBound
'*** CustomerID ***'
Dim lblCustomerID As Label = CType(e.Item.FindControl("lblCustomerID"), Label)
If Not IsNothing(lblCustomerID) Then
lblCustomerID.Text = e.Item.DataItem("CustomerID")
End If
'*** Name ***'
Dim lblName As Label = CType(e.Item.FindControl("lblName"),Label)
IF Not IsNothing(lblName) Then
lblName.Text = e.Item.DataItem("Username")
End IF
'*** Email ***'
Dim lblEmail As Label = CType(e.Item.FindControl("lblEmail"),Label)
IF Not IsNothing(lblEmail) Then
lblEmail.Text = e.Item.DataItem("Password")
End IF
'*** CountryCode ***'
Dim lblCountryCode As Label = CType(e.Item.FindControl("lblCountryCode"),Label)
IF Not IsNothing(lblCountryCode) Then
lblCountryCode.Text = e.Item.DataItem("Name")
End IF
'*** Budget ***'
Dim lblBudget As Label = CType(e.Item.FindControl("lblBudget"),Label)
IF Not IsNothing(lblBudget) Then
lblBudget.Text = e.Item.DataItem("Email")
End IF
Dim hplEdit As Hyperlink = CType(e.Item.FindControl("hplEdit"),Hyperlink)
IF Not IsNothing(hplEdit) Then
hplEdit.Text = "Edit"
hplEdit.NavigateUrl = "Form3.aspx?CustomerID=" & e.Item.DataItem("CustomerID")
End IF
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - Microsoft Access</title>
</head>
<body style="height: 179px">
<form id="form1" runat="server">
<asp:Repeater id="myRepeater" runat="server">
<HeaderTemplate>
<table border="1">
<tr>
<th>CustomerID</th>
<th>Username</th>
<th>Password</th>
<th>Name</th>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td align="center"><asp:Label id="lblCustomerID" runat="server"></asp:Label></td>
<td><asp:Label id="lblName" runat="server"></asp:Label></td>
<td><asp:Label id="lblEmail" runat="server"></asp:Label></td>
<td align="center"><asp:Label id="lblCountryCode" runat="server"></asp:Label></td>
<td align="right"><asp:Label id="lblBudget" runat="server"></asp:Label></td>
<td align="right"><asp:Hyperlink id="hplEdit" runat="server"></asp:Hyperlink></td>
<td align="center"><asp:CheckBox id="chkDel" runat="server"></asp:CheckBox></td>
</tr>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="btnDelete" runat="server" Text="Delete" />
</form>
</body>
</html>
Tag : ASP.NET, Ms SQL Server 2008, VS 2010 (.NET 4.x)