Dropdownlist in GridView EditItemTemplate Error: Visual Studio 2005

Posted on 2007-10-21
Last Modified: 2013-11-26
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?
Question by:Dovberman
    LVL 21

    Accepted Solution

    HAve you tried removing SelectedValue='<%# Bind("AccountID", "{0}") % ??
    LVL 26

    Expert Comment

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


    Author Comment

    Yes, that worked.

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

    LVL 26

    Expert Comment

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


    Author Comment

    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

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now