 |
| .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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 4th, 2004, 10:12 AM
|
|
Authorized User
|
|
Join Date: Aug 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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..
|
|

August 4th, 2004, 11:20 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Hey,
That is the update statement, I thought you were having problems with the delete?
Brian
|
|

August 4th, 2004, 11:33 AM
|
|
Authorized User
|
|
Join Date: Aug 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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)
|
|

August 4th, 2004, 12:01 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
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
|
|

August 4th, 2004, 12:23 PM
|
|
Authorized User
|
|
Join Date: Aug 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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!
|
|

August 4th, 2004, 03:19 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
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
|
|

August 5th, 2004, 07:51 AM
|
|
Authorized User
|
|
Join Date: Aug 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

August 5th, 2004, 09:27 AM
|
|
Authorized User
|
|
Join Date: Aug 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I figured it out...I needed this code:
objAdap.Fill(ds)
Grid.DataSource = ds
|
|
 |