Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > .NET Framework 2.0
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 4th, 2004, 10:12 AM
Authorized User
 
Join Date: Aug 2004
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to fitchic77
Default Desperate!!!! DataGrid w/checkbox for Delete

Collection was modified; enumeration operation may not execute

I'm getting this error once I've done the delete? It can't keep looping for some reason!!!

Here is the code....please help. I've been at this darn checkbox/delete thing for 2 days now and I have to get it working!!!

Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
Dim myDataGridItem As DataGridItem
Dim chkSelected As System.Web.UI.WebControls.CheckBox
Dim txtDocumentID As String
Dim i As Integer = 0
Dim gridSelections As StringBuilder = New StringBuilder()

For Each myDataGridItem In dgPopularFAQs.Items
chkSelected = myDataGridItem.FindControl("chkSelected")
If chkSelected.Checked Then
i += 1
txtDocumentID = CType(myDataGridItem.FindControl("txtDocumentID"), Label).Text
Call PopulatePartDocumentGrid(dgPopularFAQs, Me.Session("PartID"), "Y", txtDocumentID)
End If
Next
lblStatus.Text += "The document has been removed from Parts.<br>" & txtDocumentID & "<br>"
Response.Redirect("index.aspx?action=resources_par ts&form=resources_parts_documents&partid='" + ID + "'")
End Sub



.aspx code
======================================
<div align="center">
<asp:Label ID="lblStatus" Font-Name="verdana" Font-Size="12px" ForeColor="#ff0000" Runat="server" />
</div>
<br>
<asp:datagrid id="dgPopularFAQs" style="FONT-SIZE: 8pt; TEXT-ALIGN: center" OnUpdateCommand="dgPopFAQs_Update" OnCancelCommand="dgPopFAQs_Cancel" OnEditCommand="dgPopFAQs_Edit" ItemStyle-Font-Name="Verdana" AlternatingItemStyle-BackColor="#DEDEDE" HeaderStyle-Font-Size="7pt" HeaderStyle-Font-Name="Verdana" HeaderStyle-Font-Bold="True" HeaderStyle-ForeColor="#082083" HeaderStyle-BackColor="#afd9e9" HeaderStyle-HorizontalAlign="Center" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:TemplateColumn HeaderStyle-HorizontalAlign="Center" HeaderText="Document" HeaderStyle-Width="200px">
<ItemTemplate>
<asp:DropDownList runat="server" style="TEXT-ALIGN: center; font-size: 8pt" id="lstCategories" DataValueField="Document" DataTextField="Document" DataSource='<%# PopulatePartDocumentDDL() %>' SelectedIndex='<%# GetSelIndex(Container.DataItem("Document")) %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderStyle-HorizontalAlign="Center" HeaderText="Description" HeaderStyle-Width="350px">
<ItemTemplate>
<asp:TextBox id="Description" style="TEXT-ALIGN: left; font-size: 8pt" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' Width="350px" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderStyle-HorizontalAlign="Center" HeaderText="Remove" HeaderStyle-Width="350px">
<ItemTemplate>
<asp:Checkbox ID="chkSelected" Runat="server" />
<asp:Label ID="txtDocumentID" Visible="True" Text='<%# DataBinder.Eval(Container.DataItem, "Document") %>' Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
<br>
<br>
<br>
<br>
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="right" colspan="4">
<table width="10%" id="Table3" cellSpacing="2" cellPadding="2" border="0">
<tr>
<td align="right"><asp:Button id="cmdUpdate" runat="server" Text="Update" class="button"></asp:Button></td>
<td align="right"><asp:Button id="cmdAdd" runat="server" width="47px" Text="Add" class="button"></asp:Button></td>
<td align="right"><asp:Button id="cmdRemove" runat="server" Text="Remove" class="button"></asp:Button></td>
</tr>
</table>
</td>
</tr>
</table>

Last edited by fitchic77; October 16th, 2010 at 11:21 AM..
Reply With Quote
  #2 (permalink)  
Old August 4th, 2004, 11:20 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

That is the update statement, I thought you were having problems with the delete?

Brian
Reply With Quote
  #3 (permalink)  
Old August 4th, 2004, 11:33 AM
Authorized User
 
Join Date: Aug 2004
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to fitchic77
Default

For Each myDataGridItem In dgPopularFAQs.Items
chkSelected = myDataGridItem.FindControl("chkSelected")
If chkSelected.Checked Then
i += 1
txtDocumentID = CType(myDataGridItem.FindControl("txtDocumentID"), Label).Text
Call PopulatePartDocumentGrid(dgPopularFAQs, Me.Session("PartID"), "Y", txtDocumentID)
End If
Next
lblStatus.Text += "The document has been removed from Parts.<br>" & txtDocumentID & "<br>"
Response.Redirect("index.aspx?action=resources_par ts&form=resources_parts_documents&partid='" + ID + "'")
End Sub


I get the error right on the NEXT in debug mode..I've been told on other posts that if I reverse the FOR loop it will work...but I'm not sure how to do that.

This procedure that handles the delete and no...I'm not updating.
Call PopulatePartDocumentGrid(dgPopularFAQs, Me.Session("PartID"), "Y", txtDocumentID)

Reply With Quote
  #4 (permalink)  
Old August 4th, 2004, 12:01 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

If that is the same code as the previous post, this code is part of the cmdUpdate_Click event. So if the update button removes some data, what does the remove button do?

You should reverse it, because when you delete, you are removing a row index that now doesn't exist. If you do:

for intI As Integer = dgrPopularFAQs.Items.Count To 0 step -1
  myDataGridItem = dgrPopularFAQs.Items(intI)
  'Do whatever to check it and delete it
next

Brian
Reply With Quote
  #5 (permalink)  
Old August 4th, 2004, 12:23 PM
Authorized User
 
Join Date: Aug 2004
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to fitchic77
Default

Sorry. I'm updating..It has been a long day already

Ok. When I do this now I get an error, which I can't understand why it would be a null on "chkSelected = myDataGridItem.FindControl("chkSelected")" Error is below. I don't get this error if I'm in the original loop listed: "For Each myDataGridItem In dgPopularFAQs.Items"

I need to see if the checkbox is checked or not in order to do my delete...how can I do this?

System.NullReferenceException: Object reference not set to an instance of an object.

        For intI = dgPopularFAQs.Items.Count To 0 Step -1
            chkSelected = myDataGridItem.FindControl("chkSelected")
            myDataGridItem = dgPopularFAQs.Items(intI)
            If chkSelected.Checked Then
                i += 1
                txtDocumentID = CType(myDataGridItem.FindControl("txtDocumentID"), Label).Text
                Call PopulatePartDocumentGrid(dgPopularFAQs, Me.Session("PartID"), "Y", txtDocumentID)

            End If
        Next
Thanks a million!

Reply With Quote
  #6 (permalink)  
Old August 4th, 2004, 03:19 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

Do this instead for the for intI line:

For intI = dgPopularFAQs.Items.Count - 1 To 0 Step -1

Next, you get the reference to the DataGridItem object after you try to use it, that will always give you errors. Swap the two as shown below:

myDataGridItem = dgPopularFAQs.Items(intI)
chkSelected = myDataGridItem.FindControl("chkSelected")

I don't know if you can do the FindControl at the item level. You may have an issue with that. You may have to do alternatively:

chkSelected = myDataGridItem.Cells(<index>).FindControl("chkSelected")

If you don't have issues with the way you did this before, let me know. I didn't know you could do that if that is possible.

Brian
Reply With Quote
  #7 (permalink)  
Old August 5th, 2004, 07:51 AM
Authorized User
 
Join Date: Aug 2004
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to fitchic77
Default

got it working...BUT...the delete statement is correctly statisfied, but never deletes in the database...is there an execute command I need to initiate upon using a dataset?

here is the code for the delete:
    Public Function FindDA(ByVal where As String, Optional ByVal DeleteKey As String = "N") As SqlDataAdapter
        Dim objConn As SqlConnection
        Dim objAdap As SqlDataAdapter
        Dim ds As DataSet = New DataSet()
        Dim strSQL As String
        Try
            If (DeleteKey = "Y") Then
                strSQL = "Delete from tblPartDocument" & where
            Else
                strSQL = "Select distinct a.document, a3.description FROM tblPartDocument a, tblPart a2, tblDocument a3 WHERE a.part=a2.part AND a3.document = a.document" & where
            End If
            objConn = New SqlConnection(ConnString)
            objAdap = New SqlDataAdapter(strSQL, objConn)

        Catch ex As SqlException
        Finally
            objConn.Close()
        End Try
        Return objAdap
    End Function



'Code that is working for the checkbox delete within datagrid
        For i = 0 To dgPopularFAQs.Items.Count - 1
            blnIsChecked = DirectCast(dgPopularFAQs.Items(i).Cells(2).FindCon trol("chkSelected"), Web.UI.WebControls.CheckBox).Checked
            If blnIsChecked = True Then
                txtDocumentID = DirectCast(dgPopularFAQs.Items(i).Cells(2).FindCon trol("txtDocumentID"), Web.UI.WebControls.Label).Text
                Call PopulatePartDocumentGrid(dgPopularFAQs, Me.Session("PartID"), "Y", txtDocumentID)
            End If
        Next
        dgPopularFAQs.EditItemIndex = -1

Reply With Quote
  #8 (permalink)  
Old August 5th, 2004, 09:27 AM
Authorized User
 
Join Date: Aug 2004
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to fitchic77
Default

I figured it out...I needed this code:

            objAdap.Fill(ds)
            Grid.DataSource = ds

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How i can delete with checkbox maz_alaameri ASP.NET 1.0 and 1.1 Basics 1 December 5th, 2006 05:14 AM
In datagrid when checkbox true delete in btn click ashish2001mca ASP.NET Espanol 1 January 9th, 2006 08:45 AM
how delete row in datagrid which check checkbox ashish2001mca Forum and Wrox.com Feedback 1 September 18th, 2005 03:43 AM
checkbox in datagrid and delete saman VS.NET 2002/2003 2 May 21st, 2004 12:00 AM



All times are GMT -4. The time now is 08:39 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.