• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1312
  • Last Modified:

Control in gridview edititemtemplate not recognized. Visual Studio 2005

Control in gridview template not recognized.

I have a dropdown and the dropdown datasource in a column edititemtemplate.

I am using the RowEditing event to set the datasource.

The 'Object reference not set to an instance of an object' error is triggered.

What am I missing?

Thanks,

ASPX File:
==========================

<Columns>
                <asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
                <asp:TemplateField HeaderText="Broker Acct" SortExpression="BrokerAccount">
                    <EditItemTemplate>
                        &nbsp;<asp:DropDownList ID="cboBrokerAccount" runat="server" DataSourceID="dscBrokerAccount"
                            DataTextField="AccountName" DataValueField="AccountID" Width="127px">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="dscBrokerAccount" runat="server" ConnectionString="<%$ ConnectionStrings:StockSelectConnectionString %>"
                            SelectCommand="SELECT [AccountName], [AccountID] FROM [Account] WHERE ([UserID] = 'abb98893-b800-40de-a493-97190adc44bc') ORDER BY [AccountName]">
                            <SelectParameters>
                                <asp:ControlParameter ControlID="txtGridParam" Name="UserID" PropertyName="Text"
                                    Type="Object" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                    </EditItemTemplate>
                   
                </asp:TemplateField>

=================
Code Behind:

   Protected Sub grdPurchases_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles grdPurchases.RowEditing

        Dim intRowIndex As Integer

        intRowIndex = e.NewEditIndex 'Row being edited
        'Set selected row to the row being edited
        grdPurchases.SelectedIndex = intRowIndex

        Dim cboBrokerAccount As DropDownList = CType(grdPurchases.Rows(intRowIndex).FindControl("cboBrokerAccount"), DropDownList)
        Dim dscBrokerAccount As SqlDataSource = CType(grdPurchases.Rows(intRowIndex).FindControl("dscBrokerAccount"), SqlDataSource)
        dscBrokerAccount.SelectCommand = _
        "SELECT [AccountName], [AccountID] FROM [Account] " _
        & "WHERE ([UserID] = @UserID) ORDER BY [AccountName]"

    End Sub

Error on line 130

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.

Source Error:


Line 128:        Dim dscBrokerAccount As New SqlDataSource
Line 129:        dscBrokerAccount = CType(grdPurchases.Rows(intRowIndex).FindControl("dscBrokerAccount"), SqlDataSource)
Line 130:        dscBrokerAccount.SelectCommand = _
Line 131:        "SELECT [AccountName], [AccountID] FROM [Account] " _
Line 132:        & "WHERE ([UserID] = @UserID) ORDER BY [AccountName]"
 
Source File: C:\Inetpub\wwwroot\StockSelector\BuyStocks.aspx.vb    Line: 130
0
Dovberman
Asked:
Dovberman
  • 3
1 Solution
 
samtran0331Commented:
the sqldatasource doesn't exist for the row...so when you do this:
grdPurchases.Rows(intRowIndex).FindControl("dscBrokerAccount")
it fails....

just use a function that returns a dataset/datatable to bind your edit dropdown...or try adding another sqldatasource and bind the dropdown to it...but don't use FindControl
0
 
DovbermanAuthor Commented:
The dropdown and sqldatasource are both in the edititemtemplate.

Dow this mean that I can put the sqlDataSource on the page outside of the datagrid and bind the dropdown to it?
0
 
DovbermanAuthor Commented:
Now the only control in the edititemtemplate is the dropdown.

The dropdown is not being recognized.

     Dim intRowIndex As Integer

        intRowIndex = e.NewEditIndex 'Row being edited
        'Set selected row to the row being edited
        grdPurchases.SelectedIndex = intRowIndex

        Dim cboBrokerAccount As DropDownList = CType(grdPurchases.Rows(intRowIndex).FindControl("cboBrokerAccount"), DropDownList)

        dscBrokerAccount.SelectCommand = _
        "SELECT [AccountName], [AccountID] FROM [Account] " _
        & "WHERE ([UserID] = @UserID) ORDER BY [AccountName]"

        Me.txtSymbolName.Text = cboBrokerAccount.DataSourceID
        dscBrokerAccount.DataBind()

============

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.

Source Error:


Line 128:        & "WHERE ([UserID] = @UserID) ORDER BY [AccountName]"
Line 129:
Line 130:        Me.txtSymbolName.Text = cboBrokerAccount.DataSourceID
Line 131:        dscBrokerAccount.DataBind()
Line 132
 

0
 
DovbermanAuthor Commented:
I scrapped the EditItemTemplate
Using the ItemTemplate works.

 <Columns>
                <asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
                <asp:TemplateField HeaderText="Broker Acct" SortExpression="BrokerAccount">
                    <ItemTemplate>
                        &nbsp;<asp:DropDownList ID="cboBrokerAccount" runat="server" DataSourceID="dscBrokerAccount"
                            DataTextField="AccountName" DataValueField="AccountID" Width="127px">
                        </asp:DropDownList>&nbsp;
                    </ItemTemplate>
                   
                </asp:TemplateField>

Thanks,
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now