Dovberman
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>
<asp:DropDownList ID="cboBrokerAccount" runat="server" DataSourceID="dscBrokerAcc ount"
DataTextField="AccountID" DataValueField="AccountKey " SelectedValue='<%# Bind("AccountID", "{0}") %>'
Width="138px">
</asp:DropDownList>
</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(ByVa l 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(strConnectio n)
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.Id entity.Nam e).Provide rUserKey.T oString()
Me.dscPurchases.SelectComm and = _
"SELECT Pur.SymbolID, Acc.AccountID , Pur.BrokerAccountKey,Pur.B uyQty, " _
& "CAST(Pur.BuyPrice AS Decimal(5,2)) As BuyPrice, " _
& "Pur.UserID, CONVERT(VARCHAR,(Pur.BuyDa te),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,BuyComple ted,Symbol ID,BuyQty, BuyPrice,B uyComm,Bro kerAccount Key ) "
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,
'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>
<asp:DropDownList ID="cboBrokerAccount" runat="server" DataSourceID="dscBrokerAcc
DataTextField="AccountID" DataValueField="AccountKey
Width="138px">
</asp:DropDownList>
</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(ByVa
'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(strConnectio
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.
txtSymbolID.Text = intSymbolID
Dim strSecName As String = GetSecName(intSymbolID, conStockSelector)
txtSecName.Text = strSecName
Dim strUserId As String = Membership.GetUser(User.Id
Me.dscPurchases.SelectComm
"SELECT Pur.SymbolID, Acc.AccountID , Pur.BrokerAccountKey,Pur.B
& "CAST(Pur.BuyPrice AS Decimal(5,2)) As BuyPrice, " _
& "Pur.UserID, CONVERT(VARCHAR,(Pur.BuyDa
& "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,BuyComple
strParam = "VALUES ( '" & strUserId & "','" & calBuyDate.SelectedDate & "'," _
& "0," & intSymbolID & ",0,0,0,1)"
dscPurchases.InsertCommand
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, that worked.
I had done that a few days ago. However, other changes kept it from working consistently.
Thank you.
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
Rejo
ASKER
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,
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,
Rejo