mercredi 4 février 2015

Checkboxes in DataGrid

I have a GridView containing a column with checkboxes. The header template has a a checkbox to select all the other checkboxes in the column.


When I tick and do an event, I am getting this error:



Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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


on this line:



ifchecked = DirectCast(item.FindControl("ckTransferItem"), CheckBox).Checked


GridView Code:



<asp:DataGrid id="Dgcabinet" runat="server" CssClass="datagrids psLink" Width="100%" BorderStyle="None"
ForeColor="Transparent" GridLines="Horizontal" PagerStyle-Mode="NumericPages" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" PageSize="200">
<ItemStyle CssClass="datagrids psLink"></ItemStyle>
<HeaderStyle ForeColor="White" CssClass="datagridsHeader"></HeaderStyle>
<Columns>
<asp:BoundColumn Visible="False" DataField="file_pk"></asp:BoundColumn>
<asp:BoundColumn DataField="file_pk" SortExpression="file_pk" HeaderText="&amp;nbsp;File Number">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="file_subject"></asp:BoundColumn>
<asp:BoundColumn DataField="file_subject" SortExpression="file_subject" HeaderText="&amp;nbsp;File Name">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="cab_pk"></asp:BoundColumn>
<asp:BoundColumn DataField="cab_pk" SortExpression="cab_pk" HeaderText="&amp;nbsp;Storage Number">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="comp_part"></asp:BoundColumn>
<asp:BoundColumn DataField="comp_part" SortExpression="comp_part" HeaderText="&amp;nbsp;Compartment No.">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="dept_pk"></asp:BoundColumn>
<asp:BoundColumn DataField="dept_code" SortExpression="dept_code" HeaderText="&amp;nbsp;Department Code">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="locationno" SortExpression="locationno" HeaderText="&amp;nbsp;Storage Location">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Tick to Transfer">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:CheckBox ID="ckTransferItem" Runat="server" AutoPostBack="false"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle CssClass="datagridsFooter psLink" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>


Button Event Handler:



Private Sub btnbulktransfer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbulktransfer.Click
'get number of rows in datagrid
Dim rowcount As Integer = Dgcabinet.Items.Count
Dim itemrow As Integer = 0
Dim item As DataGridItem
Dim ifchecked As Boolean = False
Dim getcompartment As Integer
Dim getnewstate As Integer
Dim getlocation As Integer
Dim statetocomment As String
Dim lbresult As Boolean = False
Dim liUserRoleId As Integer
Dim liUserId As Integer
Dim switch As Boolean = False
Dim linewdepartment As Integer
Dim licurrentdepartment As Integer
Dim lioldstates As Integer
Dim listateid As Integer
Dim lsImriehelRepository As String
lioldstates = ddloldstates.SelectedValue


licurrentdepartment = ddldepts.SelectedValue

liUserRoleId = Convert.ToInt64(lcPrimitives.GetSessionCache("_USERROLE_ID"))
liUserId = Convert.ToInt64(lcPrimitives.GetSessionCache("_USER_ID"))


getlocation = ddllocation.SelectedValue
getcompartment = ddlstorage.SelectedValue
getnewstate = ddlnewstates.SelectedValue
statetocomment = ddlnewstates.Items(ddlnewstates.SelectedIndex).Text
linewdepartment = BLeRecords.getFileDepartment(getcompartment)
listateid = ddldepts.SelectedValue

'check if storage is in the imriehel repository
lsImriehelRepository = [Global]._eRecordsPortalConstants.eRecordsCONST_ToImriehelRepository
If linewdepartment = lsImriehelRepository Then
linewdepartment = licurrentdepartment
End If

'check that all 'transfer to' dropdown options are selected
If (getlocation = 0) Then
lblError.Visible = True
lblError.Text = "!Error. Please check location dropdown."
ElseIf (getcompartment = 0) Then
lblError.Visible = True
lblError.Text = "!Error. Please check Storage dropdown."
ElseIf (getnewstate = 0) Then
lblError.Visible = True
lblError.Text = "!Error. Please check Status dropdown."
Else
Dim fileIDs As ArrayList = New ArrayList
Dim department As Integer
ClearMessages()
For itemrow = 0 To rowcount - 1
' read first row to check if the tickbox is checked
item = Dgcabinet.Items(itemrow)
ifchecked = DirectCast(item.FindControl("ckTransferItem"), CheckBox).Checked
department = Integer.Parse(item.Cells(8).Text())

If ifchecked Then
Dim filenumber As String = (item.Cells(0).Text())
fileIDs.Add(filenumber)
switch = True
End If
Next


'check if tickbox are ticked. If not returns error.
If switch = False Then
lblError.Visible = True
lblError.Text = "!Error. Please check tick box"
Else
lbresult = BLeRecords.updateBulkTransferBatch(fileIDs, getcompartment, linewdepartment, getnewstate, statetocomment, liUserId, listateid)

If lbresult = True Then
lblmessage.Text = "File transfer succesful"
lblmessage.Visible = True
DisplaySortedData("", lioldstates, licurrentdepartment, False)
Else
lblError.Visible = True
lblError.Text = "!Error. File transfer Failed. Contact Administrator"
End If
End If

End If
End Sub


Can anyone explain why?


If the checkbox is checked it should be set to True no?





Aucun commentaire:

Enregistrer un commentaire