Link to home
Start Free TrialLog in
Avatar of Dovberman
DovbermanFlag for United States of America

asked on

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">
                    <EditItemTemplate>
                        &nbsp;<asp:DropDownList ID="cboBrokerAccount" runat="server" DataSourceID="dscBrokerAccount"
                            DataTextField="AccountID" DataValueField="AccountKey" SelectedValue='<%# Bind("AccountID", "{0}") %>'
                            Width="138px">
                        </asp:DropDownList>&nbsp;
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblBrokerAccount" runat="server" Text='<%# Bind("AccountID") %>' Width="80px"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
 
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)
        conStockSelector.Open()

        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
            dscPurchases.Insert()
        Next intRows

        'Set source for grid

        dscPurchases.DataBind()
        'grdPurchases.DataBind()
        txtRowCount.Focus()

        grdPurchases.Visible = True

        conStockSelector.Close()
    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?
Thanks,
ASKER CERTIFIED SOLUTION
Avatar of surajguptha
surajguptha
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ..

Rejo
Avatar of Dovberman

ASKER

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 :-) ..

Rejo
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.

Thanks,