This is a migrated thread and some comments may be shown as answers.

FindControl is NULL - VB (Throwing a Null reference)

5 Answers 775 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jason Jones
Top achievements
Rank 1
Jason Jones asked on 12 Feb 2015, 07:54 PM

I am trying to take a RadGrid results with a GridClientSelectColumn and a Button that when clicked Loops through the selected records and then pulls the ID so that I can run a routine on the record (Actually plan to copy it to another table)

I can't for the life of me figure out what I am doing wrong.  I get a Null Exception error anytime I try to get and assign the variable the Value of the ID field.  

I am a little rusty with VB/.Net so any help would be appreciated....  if we can figure this out the next thing will be opening up the database source and copying the data from the loop through to the other table! 



ASPX page 
<telerik:RadGrid ID="RadGrid1" runat="server" AllowMultiRowSelection="True" AllowFilteringByColumn="True" AllowPaging="True"
               AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" OnItemCommand="RadGrid1_ItemCommand" OnPreRender="RadGrid1_PreRender"
               PageSize="100" Skin="Metro">
               <exportsettings hidestructurecolumns="true">
               </exportsettings>
               <clientsettings>
                   <Selecting AllowRowSelect="True" />
               </clientsettings>
               <mastertableview datakeynames="ID" width="100%">
                   <PagerStyle Mode="NextPrevNumericAndAdvanced" />
                   <Columns>
 
                      <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn">
              </telerik:GridClientSelectColumn>
 
 
 
 
 
                       <telerik:GridBoundColumn DataField="ID" DataType="System.Int32" FilterControlAltText="Filter ID column" HeaderText="ID" ReadOnly="True" SortExpression="ID" UniqueName="ID">
                           <columnvalidationsettings>
                               <ModelErrorMessage Text="" />
                           </columnvalidationsettings>
                       </telerik:GridBoundColumn>
                       <telerik:GridBoundColumn DataField="ContactServiceID" FilterControlAltText="Filter ContactServiceID column" HeaderText="ContactServiceID" SortExpression="ContactServiceID" UniqueName="ContactServiceID" DataType="System.Int32">
                           <columnvalidationsettings>
                               <ModelErrorMessage Text="" />
                           </columnvalidationsettings>
                       </telerik:GridBoundColumn>
                       <telerik:GridBoundColumn DataField="CategoryName" FilterControlAltText="Filter CategoryName column" HeaderText="CategoryName" SortExpression="CategoryName" UniqueName="CategoryName">
                           <columnvalidationsettings>
                               <ModelErrorMessage Text="" />
                           </columnvalidationsettings>
                       </telerik:GridBoundColumn>
                       <telerik:GridBoundColumn DataField="DateCreated" FilterControlAltText="Filter DateCreated column" HeaderText="DateCreated" SortExpression="DateCreated" UniqueName="DateCreated" DataType="System.DateTime">
                           <columnvalidationsettings>
                               <ModelErrorMessage Text="" />
                           </columnvalidationsettings>
                       </telerik:GridBoundColumn>
                       <telerik:GridBoundColumn DataField="DateDeleted" FilterControlAltText="Filter DateDeleted column" HeaderText="DateDeleted" SortExpression="DateDeleted" UniqueName="DateDeleted" DataType="System.DateTime">
                           <columnvalidationsettings>
                               <ModelErrorMessage Text="" />
                           </columnvalidationsettings>
                       </telerik:GridBoundColumn>
                       <telerik:GridBoundColumn DataField="UserCreated" FilterControlAltText="Filter UserCreated column" HeaderText="UserCreated" SortExpression="UserCreated" UniqueName="UserCreated">
                           <columnvalidationsettings>
                               <ModelErrorMessage Text="" />
                           </columnvalidationsettings>
                       </telerik:GridBoundColumn>
                       <telerik:GridBoundColumn DataField="UserDeleted" FilterControlAltText="Filter UserDeleted column" HeaderText="UserDeleted" SortExpression="UserDeleted" UniqueName="UserDeleted">
                           <columnvalidationsettings>
                               <ModelErrorMessage Text="" />
                           </columnvalidationsettings>
                       </telerik:GridBoundColumn>
                       <telerik:GridBoundColumn DataField="FirstName" FilterControlAltText="Filter FirstName column" HeaderText="FirstName" SortExpression="FirstName" UniqueName="FirstName">
                           <columnvalidationsettings>
                               <ModelErrorMessage Text="" />
                           </columnvalidationsettings>
                       </telerik:GridBoundColumn>
                       <telerik:GridBoundColumn DataField="LastName" FilterControlAltText="Filter LastName column" HeaderText="LastName" SortExpression="LastName" UniqueName="LastName">
                           <columnvalidationsettings>
                               <ModelErrorMessage Text="" />
                           </columnvalidationsettings>
                       </telerik:GridBoundColumn>
                   </Columns>
                   <ItemStyle BackColor="#DFDFDF" />
                   <HeaderStyle BackColor="#FFFFFF" ForeColor="#767676" />
                   <AlternatingItemStyle BackColor="#FFFFFF" />
               </mastertableview>
 
                       <ClientSettings EnableRowHoverStyle="true">
                         <Selecting AllowRowSelect="True"></Selecting>
                         
                       </ClientSettings>
 
 
           </telerik:RadGrid>




Code behind:   **** Note I have Remarked out all of my attempted tries.  I know for a fact the ID is NOT null and is an integer.


Protected Sub BTNrestore_Click(sender As Object, e As EventArgs) Handles BTNrestore.Click
 
 
      
    ' If IDs = String.Empty Then
    ' IDs = CType(dataItem.FindControl("Id"), TextBox).Text
     'IDs = IDs & "," & CType(dataItem.FindControl("Id"), TextBox).Text
 
    Dim strAmount As String
 
    For Each item As GridDataItem In RadGrid1.SelectedItems
        ' Dim lblID As Integer = CType(CType(item.FindControl("ID"), Label).Text, Integer)
        ' Dim lblID As Integer = CType(item.FindControl("ID"), Integer) 
        ' Dim txtBox As TextBox = DirectCast(item("ID").FindControl("ID"), TextBox)
        ' strAmount = txtBox.Text
        ' MsgBox(strAmount)
 
        'Dim itemID As GridDataItem = TryCast(e.ItemID, GridDataItem)
        'Dim itemValue As String = itemID("ID").Text
 
 
        Response.Write(strAmount & " ")
 
 
    Next
 
 
 
End Sub






5 Answers, 1 is accepted

Sort by
0
Jason Jones
Top achievements
Rank 1
answered on 13 Feb 2015, 01:08 PM


I am adding the Button code from the ASPX file in case the problem is here

<asp:Button ID="BTNrestore" runat="server" Text="Restore" />

I also cleaned up the VB but the results trying to grab any of the Grid items is NULL/Empty




 Protected Sub BTNrestore_Click(sender As Object, e As EventArgs) Handles BTNrestore.Click

        For Each item As GridDataItem In RadGrid1.SelectedItems        

            If IsNothing(item.FindControl("CategoryName")) Then    'Also tried  FindControl("ID") which also has data
                Response.Write("NOTHING    ")
            Else
                Response.Write(item.FindControl("ID"))
            End If

        Next

    End Sub




The result here is that I get a bunch of "NOTHING" for every item I check in the grid view.  So for some reason it is not getting the values from the GridDataItems...  I've tried a few different items both integer and text and they are all null
0
Eyup
Telerik team
answered on 17 Feb 2015, 02:46 PM
Hello Jason,

The FindControl method is suitable for template columns. For bound columns you can access the TableCell and use its Text property - dataItem("ColumnUniqueName").Text:
http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html

Hope this helps. If you have different requirements or further instructions, please elaborate on your specific scenario and send us sample screenshots or video demonstrating the problematic behavior.

Regards,
Eyup
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Jason Jones
Top achievements
Rank 1
answered on 17 Feb 2015, 03:06 PM
Yes this is correct I was going to post my corrected code this morning, however I still have a problem with the DataItem and conditional statement for Null value Query results (Null Field/Column)  I need to flag a row with a null value, however none of these work.  

Dim myCellCat As TableCell = dataItem("CurrentCategory")

            If String.IsNullOrWhiteSpace(myCellCat.Text.ToString()) Then
            If String.IsNullOrEmpty(myCellCat.Text.ToString()) Then
            If String.IsNull(myCellCat.Text.ToString()) Then
            If Len(myCellCat.Text) >= 4 Then



0
Jason Jones
Top achievements
Rank 1
answered on 17 Feb 2015, 03:22 PM
Oddly this works....  does it look at the HTML code?  

If (myCellCat.Text) = "&nbsp;" Then


0
Eyup
Telerik team
answered on 20 Feb 2015, 12:26 PM
Hello Jason,

RadGrid automatically generates "&nbsp;" for empty cells when there is null or empty value. This is intentionally designed to keep compatibility with older versions of IE, which have complications with empty cells.

I hope the clarification was helpful.

Regards,
Eyup
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Jason Jones
Top achievements
Rank 1
Answers by
Jason Jones
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or