[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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

Dropdownlist in GridView EditItemTemplate Error: Visual Studio 2005

Dropdownlist in GridView EditItemTemplate Error:

'cboBrokerAccount' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

I have a DropDownList in the EditItemTemplate and a Label in the ItemTemplate.

The label text is supposed to the display column of the DropDownList (DataTextField="AccountID" )

The dropdownlist datasource SQL is simply
SelectCommand="SELECT [AccountID], [AccountKey] FROM [Account] WHERE ([UserID] = @UserID) ORDER BY [AccountID]  ">

The DroppDownList and Label code is:

    <asp:CommandField ShowEditButton="True"  ButtonType=Button />
                <asp:TemplateField HeaderText="AccountID" SortExpression="AccountID">
                        &nbsp;<asp:DropDownList ID="cboBrokerAccount" runat="server" DataSourceID="dscBrokerAccount"
                            DataTextField="AccountID" DataValueField="AccountKey" SelectedValue='<%# Bind("AccountID", "{0}") %>'
                        <asp:Label ID="lblBrokerAccount" runat="server" Text='<%# Bind("AccountID") %>' Width="80px"></asp:Label>
There is a StartDetails button that refreshes and rebuilds the gridview.

   Protected Sub btnStartDetails_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStartDetails.Click

        'Build new rows in the purchases maintenance grid  
        Dim strConnection As String = "Data Source=BURCEL05;Initial Catalog=StockSelectSQL;" _
& "Integrated Security=SSPI"

        Dim conStockSelector As SqlConnection
        conStockSelector = New SqlConnection(strConnection)

        Dim strInsertSQL As String = ""
        'Make sure symbolname and purchase date entered
        'Get the SymbolID
        Dim strParam As String = ""
        Dim intRows As Integer = 0

        Dim intSymbolID As Integer = GetSymbolID(txtSymbolName.Text, conStockSelector)
        txtSymbolID.Text = intSymbolID
        Dim strSecName As String = GetSecName(intSymbolID, conStockSelector)
        txtSecName.Text = strSecName
        Dim strUserId As String = Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString()
        Me.dscPurchases.SelectCommand = _
        "SELECT Pur.SymbolID, Acc.AccountID , Pur.BrokerAccountKey,Pur.BuyQty, " _
        & "CAST(Pur.BuyPrice AS Decimal(5,2)) As BuyPrice, " _
        & "Pur.UserID, CONVERT(VARCHAR,(Pur.BuyDate),1) As BuyDate, " _
        & "CAST(Pur.BuyComm AS Decimal(5,2))As BuyComm, Pur.BuyCompleted, Pur.BuyTransID " _
        & "FROM Purchases As Pur " _
        & "INNER JOIN Account as Acc ON Pur.BrokerAccountKey = Acc.AccountKey " _
        & "WHERE (Pur.UserID = @UserID) AND Pur.SymbolID=@SymbolID " _
        & "AND Pur.BuyDate=@BuyDate"

        strInsertSQL = "INSERT INTO dbo.Purchases " _
        & "(UserID,BuyDate,BuyCompleted,SymbolID,BuyQty,BuyPrice,BuyComm,BrokerAccountKey ) "

        strParam = "VALUES ( '" & strUserId & "','" & calBuyDate.SelectedDate & "'," _
        & "0," & intSymbolID & ",0,0,0,1)"
        dscPurchases.InsertCommand = strInsertSQL & strParam
        For intRows = 1 To txtRowCount.Text
        Next intRows

        'Set source for grid


        grdPurchases.Visible = True

    End Sub

I have been working on this issue for 5 days and am now completely confused.

Can anyone figure out why my code does not work?
  • 2
  • 2
1 Solution
HAve you tried removing SelectedValue='<%# Bind("AccountID", "{0}") % ??
the account id that you are trying to bind might not be available in the list of account ids list that you have bound to the dropdownlist .. pls check what is returned by the "select" query  .. and see what value is been shown before you hit the edit button ..

DovbermanAuthor Commented:
Yes, that worked.

I had done that a few days ago. However, other changes kept it from working consistently.
Thank you.

you have accepted the solution .. but removing the binding code will not default the dropdown to the current value .. i.e when the user clicks on the edit button .. anyway, if that is what you want, I have no issues with it .. but the functionality looks a bit odd :-) ..

DovbermanAuthor Commented:
I finally got the EditItemTemplate binding expression to work.

However, the FindControl method in  the RowEditing event fails.

See "Error defining an instance of a control in the EditItemTemplate of a DataGrid" for details.


Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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