Click to See Complete Forum and Search --> : Delete multiple checkbox


TrustFly
02-24-2004, 08:27 AM
Hi,
i have try to use multiple checkbox to delete users by using datagrid.

Below is my codes:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

Dim conn As OleDbConnection
Dim objCmd = New OleDbCommand

conn = connectToDB()
objCmd.commandtext = "spGetMembers"
objCmd.CommandType = CommandType.StoredProcedure
objCmd.connection = conn

Dim myDataReader As OleDbDataReader
myDataReader = objCmd.ExecuteReader

dgDelMember.DataSource = myDataReader
dgDelMember.DataBind()
conn.close()
End Sub

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

Dim Item
For Each Item In Request.QueryString("chkMemDelete")
'SQL statement here

Dim conn As OleDbConnection
Dim objCmd = New OleDbCommand

conn = connectToDB()
objCmd.commandtext = "spDelMembers"
objCmd.CommandType = CommandType.StoredProcedure
objCmd.connection = conn

Dim myDataReader As OleDbDataReader
myDataReader = objCmd.ExecuteReader

dgDelMember.DataSource = myDataReader
dgDelMember.DataBind()
conn.Close()

Next

End Sub

But they can't find the "chkMemDelete" as the id for checkbox is in the datagrid.

Error:
Object reference not set to an instance of an object.

PeOfEo
02-24-2004, 04:47 PM
you are using asp.net correct?

TrustFly
02-24-2004, 08:40 PM
Im doing it the wrong way? Which part is wrong?

I am doing the sql at the store procedure. :confused:

PeOfEo
02-24-2004, 08:43 PM
Originally posted by TrustFly
Im doing it the wrong way? Which part is wrong?

I am doing the sql at the store procedure. :confused: I do not know what is wrong exactly... could you post the html portions as well, maybe that would shed some light.

TrustFly
02-24-2004, 08:54 PM
ok here's my html code:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="DeleteMembers.aspx.vb" Inherits="CyberCineAsia.DeleteMembers"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>DeleteMembers</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="dgDelMember" style="Z-INDEX: 101; LEFT: 52px; POSITION: absolute; TOP: 71px"
runat="server" AutoGenerateColumns="False" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px"
BackColor="#CCCCCC" CellPadding="4" CellSpacing="2" ForeColor="Black">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#000099"></SelectedItemStyle>
<ItemStyle BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="Black"></HeaderStyle>
<FooterStyle BackColor="#CCCCCC"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="CustID" HeaderText="Member ID"></asp:BoundColumn>
<asp:BoundColumn DataField="UserName" HeaderText="User Name"></asp:BoundColumn>
<asp:BoundColumn DataField="MemEmail" HeaderText="Email"></asp:BoundColumn>
<asp:BoundColumn DataField="memDOB" HeaderText="Date Of Birth"></asp:BoundColumn>
<asp:BoundColumn DataField="MemGender" HeaderText="Gender"></asp:BoundColumn>
<asp:BoundColumn DataField="MemJoinDate" HeaderText="Join Date"></asp:BoundColumn>
<asp:BoundColumn DataField="MemRewardPt" HeaderText="Reward Point"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Delete">
<ItemTemplate>
<asp:CheckBox id="chkMemDelete" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="Black" BackColor="#CCCCCC" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
<asp:Button id="btnDelete" style="Z-INDEX: 102; LEFT: 310px; POSITION: absolute; TOP: 36px"
runat="server" Text="Delete"></asp:Button>
</form>
</body>
</HTML>

really hope you can lead mi the way..

PeOfEo
02-24-2004, 09:10 PM
hum... to be honest with you I have never tried to output a check box like this. But why do we have Request.QueryString("chkMemDelete").
Where did the query string come from, isnt the delete code going to be on the same page as the grid, why are we requesting a query string? You might consider using the repeater control instead of the data grid... might not solve your problem but I just find it a lot easier to work with myself. It is more hands on and gives you more control over your output.

TrustFly
02-24-2004, 09:30 PM
Originally posted by PeOfEo
hum... to be honest with you I have never tried to output a check box like this. But why do we have Request.QueryString("chkMemDelete").
Where did the query string come from, isnt the delete code going to be on the same page as the grid, why are we requesting a query string? You might consider using the repeater control instead of the data grid... might not solve your problem but I just find it a lot easier to work with myself. It is more hands on and gives you more control over your output.

I realise that the way im doing for the above is wrong. Now im trying another method. it does not have error now but does not delete the users. Can point me my where is my mistake? Thankz...


here are my coding:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

Dim conn As OleDbConnection
Dim objCmd = New OleDbCommand

conn = connectToDB()
objCmd.commandtext = "spGetMembers"
objCmd.CommandType = CommandType.StoredProcedure
objCmd.connection = conn

Dim myDataReader As OleDbDataReader
myDataReader = objCmd.ExecuteReader

dgDelMember.DataSource = myDataReader
dgDelMember.DataBind()
conn.close()
End Sub

Private Sub dgDelMember_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgDelMember.ItemCommand
If CType(e.Item.FindControl("chkMemDelete"), CheckBox).Checked And e.CommandName = "DeleteButton" Then
Dim conn As OleDbConnection
Dim objCmd = New OleDbCommand

conn = connectToDB()
objCmd.commandtext = "spDelMembers"
objCmd.CommandType = CommandType.StoredProcedure
objCmd.connection = conn

Dim myDataReader As OleDbDataReader
myDataReader = objCmd.ExecuteReader

dgDelMember.DataSource = myDataReader
dgDelMember.DataBind()
conn.Close()
End If
End Sub
End Class

I have not use repeater before. Maybe you can teach me how to use it?

PeOfEo
02-24-2004, 10:02 PM
I do not see a delete sql statement in that code. The data source crud would be the same with a repeater as with a data grid

<asp:repeater
id="newscol"
runat="server"
EnableViewState="false"
>
<itemtemplate>
<div class="newsdiv" >
<span class="headerspan" ><%# DataBinder.Eval(Container.DataItem, "title") %></span>
<div class="newsmid">
<span><%# DataBinder.Eval(Container.DataItem, "posts") %></span>
</div>
<span>posted by:<span class="headerspan"><%# DataBinder.Eval(Container.DataItem, "postedby") %></span> | posted at:
<span class="headerspan"><%# DataBinder.Eval(Container.DataItem, "posttime") %></span> |
<span class="headerspan"><a href="/coments.aspx?id=<%# DataBinder.Eval(Container.DataItem, "id") %>">comments: <%# DataBinder.Eval(Container.DataItem, "comments") %></a></span>
</span>
</div>
</itemtemplate>
<separatortemplate>
</separatortemplate>
</asp:repeater>
is what I use for one of my repeaters on http://quasi-ke.servebeer.com/layout that news col is a repeater

TrustFly
02-25-2004, 09:29 AM
this is the code behind or html? :confused: juz write the code in it will do? i'm using visual studio.net 2003..

PeOfEo
02-25-2004, 05:42 PM
Originally posted by TrustFly
this is the code behind or html? :confused: juz write the code in it will do? i'm using visual studio.net 2003.. Thats the asp.net element you would put in the html section. The script section would look the same as a data grid. You still have data source and all. The difference is, you dictate how and where this stuff gets populated rather then it going into columns.