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

[Solved] Object reference not set to an instance of an object. findcontrol

2 Answers 392 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Julia Helton
Top achievements
Rank 1
Julia Helton asked on 12 Nov 2009, 05:11 PM

I am trying to use the Insert Command to save while using a custom Template form.  I copied the code from a sample, but I cannot get it to work.  Here is a copy of my code.

aspx page

       <telerik:RadGrid ID="rgEntityFinancials" runat="server" AutoGenerateColumns="False" 
            GridLines="None" Skin="Web20" Width="320px">  
            <AlternatingItemStyle BackColor="LemonChiffon" /> 
            <MasterTableView CommandItemDisplay="Top" DataKeyNames="ID" NoMasterRecordsText="No financials available.">  
                <NoRecordsTemplate> 
                    <strong><span style="font-size: 14pt; background-color: #ff0033">No Financials!</span></strong>  
                </NoRecordsTemplate> 
                <RowIndicatorColumn> 
                    <HeaderStyle Width="20px" /> 
                </RowIndicatorColumn> 
                <ExpandCollapseColumn> 
                    <HeaderStyle Width="20px" /> 
                </ExpandCollapseColumn> 
                <Columns> 
                    <telerik:GridBoundColumn DataField="TaxYear" HeaderText="Tax Year" SortExpression="TaxYear" 
                        UniqueName="TaxYear">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="StatementDescr" HeaderText="Stmt Type" SortExpression="StatementDescr" 
                        UniqueName="StatementDescr">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="DateReceived" DataFormatString="{0:d}" HeaderText="Date Received" 
                        SortExpression="DateReceived" UniqueName="DateReceived">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridEditCommandColumn> 
                    </telerik:GridEditCommandColumn> 
                    <telerik:GridBoundColumn DataField="StatementType" HeaderText="StatementType" UniqueName="StatementType" 
                        Visible="False">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="ID" HeaderText="ID" ReadOnly="True" UniqueName="ID">  
                    </telerik:GridBoundColumn> 
                </Columns> 
                <EditFormSettings EditFormType="Template">  
                    <EditColumn UniqueName="EditCommandColumn1">  
                    </EditColumn> 
                    <FormTemplate> 
                        <table> 
                            <tr> 
                                <td style="width: 100px">  
                                    </td> 
                                <td style="width: 403px">  
                                </td> 
                            </tr> 
                            <tr> 
                                <td style="width: 100px">  
                                    <asp:Label ID="Label5" runat="server" Text="Tax Year" Width="80px"></asp:Label></td>  
                                <td style="width: 403px">  
                                    <asp:TextBox ID="TaxYear" runat="server" Text='<%# eval("taxyear") %>'></asp:TextBox> 
                                    <%--                                            <telerik:RadTextBox ID="rtxtTaxYear" runat="server" InvalidStyleDuration="100" Text='<%# Eval("TaxYear") %>' 
                                                Width="125px" Skin="Vista">  
                                            </telerik:RadTextBox>--%> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td style="width: 100px; height: 42px">  
                                    <asp:Label ID="Label6" runat="server" Text="Statement Type" Width="128px"></asp:Label></td>  
                                <td style="width: 403px; height: 42px">  
<%--                                    <telerik:RadComboBox ID="rcbStatementType" runat="server" DataSourceID="sqlStmtTypes" 
                                        DataTextField="StatementDescr" DataValueField="StatementType" Font-Names="Arial" 
                                        SelectedValue='<%# Eval("StatementType") %>' Skin="WebBlue">  
                                        <CollapseAnimation Duration="200" Type="OutQuint" /> 
                                    </telerik:RadComboBox>--%> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td style="width: 100px; height: 44px">  
                                    <asp:Label ID="Label7" runat="server" Text="Date Received" Width="128px"></asp:Label></td>  
                                <td style="width: 403px; height: 44px">  
<%--                                    <telerik:RadDatePicker ID="rdpDateReceived" runat="server" Culture="English (United States)" 
                                        dbselecteddate='<%# Eval("DateReceived") %>' Skin="WebBlue">  
                                        <DateInput InvalidStyleDuration="100" LabelCssClass="radLabelCss_WebBlue" Skin="WebBlue">  
                                        </DateInput> 
                                        <Calendar Skin="WebBlue">  
                                        </Calendar> 
                                    </telerik:RadDatePicker>--%> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td style="width: 100px">  
                                    <asp:LinkButton ID="btnUpdate" runat="server" CommandName='<%# IIf( DataBinder.Eval(Container, "OwnerTableView.IsItemInserted"), "PerformInsert", "Update") %>' 
                                        Text='<%# IIf( DataBinder.Eval(Container, "OwnerTableView.IsItemInserted"), "Insert", "Update") %>'>  
                                            </asp:LinkButton> 
                                </td> 
                                <td style="width: 403px">  
                                    <asp:LinkButton ID="btnCancel" runat="server" CausesValidation="False" CommandName="Cancel" 
                                        Text="Cancel"></asp:LinkButton> 
                                </td> 
                            </tr> 
                        </table> 
                    </FormTemplate> 
                </EditFormSettings> 
            </MasterTableView> 
            <HeaderStyle ForeColor="White" /> 
            <ClientSettings EnableRowHoverStyle="True">  
                <Selecting AllowRowSelect="True" /> 
            </ClientSettings> 
        </telerik:RadGrid> 
 code behing apsx.vb file
    Public ReadOnly Property EntityFinancials() As DataTable  
        Get  
            Dim obj As Object = Me.Session("EntFinancials")  
            If (Not obj Is Nothing) Then  
                Return CType(obj, DataTable)  
            End If  
            Dim myDataTable As DataTable = New DataTable("EntityFinancials")  
            Dim row1 As DataRow  
            Try  
                Dim ID As DataColumn = New DataColumn("ID")  
                ID.DataType = System.Type.GetType("System.String")  
                myDataTable.Columns.Add(ID)  
                Dim portfolio As DataColumn = New DataColumn("Portfolio")  
                portfolio.DataType = System.Type.GetType("System.String")  
                myDataTable.Columns.Add(portfolio)  
                Dim taxYear As DataColumn = New DataColumn("TaxYear")  
                taxYear.DataType = System.Type.GetType("System.String")  
                myDataTable.Columns.Add(taxYear)  
                Dim stmtType As DataColumn = New DataColumn("stmtType")  
                stmtType.DataType = System.Type.GetType("System.String")  
                myDataTable.Columns.Add(stmtType)  
                Dim dateRcvd As DataColumn = New DataColumn("dateRcvd")  
                dateRcvd.DataType = System.Type.GetType("System.String")  
                myDataTable.Columns.Add(dateRcvd)  
                row1 = myDataTable.NewRow  
                row1.Item("ID") = 1  
                row1.Item("portfolio") = "616"  
                row1.Item("taxyear") = "2008"  
                row1.Item("stmttype") = "tr"  
                row1.Item("datercvd") = "04/15/2009"  
 
                myDataTable.Rows.Add(row1)  
 
            Catch ex As Exception  
                Me.lblMessage.Text = ex.Message  
            End Try  
            Me.Session("EntFinancials") = myDataTable  
            Return myDataTable  
        End Get  
    End Property  
 
    Protected Sub rgEntityFinancials_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles rgEntityFinancials.NeedDataSource  
        MeMe.rgEntityFinancials.DataSource = Me.EntityFinancials  
        Me.EntityFinancials.PrimaryKey = New DataColumn() {Me.EntityFinancials.Columns("ID")}  
    End Sub  
 
    Protected Sub rgEntityFinancials_InsertCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles rgEntityFinancials.InsertCommand  
        Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)  
        Dim userControl As UserControl = CType(e.Item.FindControl(GridEditFormItem.EditFormUserControlID), UserControl)  
 
        'Prepare new row to add it in the DataSource  
        Dim newRow As DataRow = Me.EntityFinancials.NewRow  
 
        'Insert new values  
        Dim newValues As Hashtable = New Hashtable  
 
        newValues("Portfolio") = Me.rtxtPortfolio.Text  
        newValues("taxyear") = CType(userControl.FindControl("taxyear"), TextBox).Text  
        'make sure that unique primary key value is generated for the inserted row   
        newValues("ID") = (CType(Me.EntityFinancials.Rows((Me.EntityFinancials.Rows.Count - 1))("ID"), Integer) + 1)  
        Try  
            For Each entry As DictionaryEntry In newValues  
                newRow(CType(entry.Key, String)) = entry.Value  
            Next  
            Me.EntityFinancials.Rows.Add(newRow)  
            Me.EntityFinancials.AcceptChanges()  
        Catch ex As Exception  
            Me.rgEntityFinancials.Controls.Add(New LiteralControl(("Unable to update/insert Financials. Reason: " + ex.Message)))  
            e.Canceled = True 
        End Try  
    End Sub 
  The problem is with the FindControl stmt.  I need help getting the values from my template in order to save the records to the database.  Any help will be greatly appreciated!

2 Answers, 1 is accepted

Sort by
0
Accepted
Veli
Telerik team
answered on 16 Nov 2009, 03:34 PM
Hi Julia,

Note that you are using template edit form defined in the FormTemplate of your EditFormSettings. But you are also trying to find a user control in your edit form. This will not work, as you do not instantiate any user controls in the edit form. Use e.Item.FindControl() to find your textboxes in the edit form.

For more information on template edit forms and  user control edit forms:

Custom edit forms

Sincerely yours,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Julia Helton
Top achievements
Rank 1
answered on 18 Nov 2009, 06:37 PM

Thanks for the help.

I was able to fix my problem by using the code below.

strTaxYear =

CType(e.Item.FindControl("txtTaxYear"), TextBox).Text

 

 This allowed me to get the value of each control in my template.

Tags
Grid
Asked by
Julia Helton
Top achievements
Rank 1
Answers by
Veli
Telerik team
Julia Helton
Top achievements
Rank 1
Share this question
or