locked
'DropDownList2' has a SelectedValue which is invalid because it does not exist in the list of items. RRS feed

  • Question

  • User624134129 posted
    Hi. I'm newbie on asp.net
    When load this page it gaves the error:

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

       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.ArgumentOutOfRangeException: 'DropDownList2' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value

    <%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="editarcarro.aspx.vb" Inherits="_Default" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <script type="text/javascript"> function teste() { alert('Teste') } </script> <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> </asp:ToolkitScriptManager> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <asp:LoginView ID="LoginView2" runat="server"> <AnonymousTemplate> <p> Seja bem-vindo ao Stand Manager. Para aceder ao sistema precisa de fazer login.</p> <p> &nbsp;</p> </AnonymousTemplate> <RoleGroups> <asp:RoleGroup Roles="Administrador"> <ContentTemplate> <asp:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0" Height="461px" Width="770px"> <asp:TabPanel runat="server" HeaderText="Estudos" ID="tbEstudos" Height="100%"> <HeaderTemplate> Estudos </HeaderTemplate> <ContentTemplate> <table> <tr> <td> <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem>Nome</asp:ListItem> <asp:ListItem>Marca</asp:ListItem> </asp:DropDownList> <asp:TextBox ID="TextBox1" runat="server" Height="14px"></asp:TextBox> <asp:ImageButton ID="ImageButton2" runat="server" Height="14px" ImageUrl="~/images/lupa.png" Width="14px" OnClick="ImageButton2_Click" /> </td> </tr> <tr> <td> <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="SqlDataSource1" DefaultMode="Edit" Height="50px" Width="354px" DataKeyNames="Cod_Carro" ondatabound="DetailsView1_DataBound"> <Fields> <asp:TemplateField HeaderText="Cod_Carro" SortExpression="Cod_Carro"> <EditItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("Cod_Carro") %>'></asp:Label> </EditItemTemplate> <InsertItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Cod_Carro") %>'></asp:TextBox> </InsertItemTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("Cod_Carro") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Marca" HeaderText="Marca" SortExpression="Marca" /> <asp:BoundField DataField="Preco_Base" HeaderText="Preco_Base" SortExpression="Preco_Base" /> <asp:TemplateField HeaderText="Cod_ISV" SortExpression="Cod_ISV"> <EditItemTemplate> <%--<asp:TextBox ID="txtEditISV" runat="server" Text='<%# Bind("Cod_ISV") %>' Visible="False"></asp:TextBox>--%> <asp:DropDownList ID="DropDownList2" AutoPostBack="True" runat="server" DataSourceID="ISVedit" DataTextField="Nome_ISV" DataValueField="id" SelectedValue='<%# Bind("Cod_ISV") %>' AppendDataBoundItems="True"> </asp:DropDownList> <asp:SqlDataSource ID="ISVedit" runat="server" ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString %>" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [ISV]"> </asp:SqlDataSource> </EditItemTemplate> <InsertItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Cod_ISV") %>'></asp:TextBox> </InsertItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("Cod_ISV") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="CC" HeaderText="CC" SortExpression="CC" /> <asp:BoundField DataField="Potencia" HeaderText="Potencia" SortExpression="Potencia" /> <asp:BoundField DataField="binario" HeaderText="binario" SortExpression="binario" /> <asp:BoundField DataField="transmissao" HeaderText="transmissao" SortExpression="transmissao" /> <asp:BoundField DataField="consumo" HeaderText="consumo" SortExpression="consumo" /> <asp:BoundField DataField="Nome" HeaderText="Nome" SortExpression="Nome" /> <asp:CommandField ShowEditButton="True" /> </Fields> </asp:DetailsView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString %>" InsertCommand="INSERT INTO [Carro] ([Cod_Carro], [Marca], [Preco_Base], [Cod_ISV], [CC], [Potencia], [binario], [transmissao], [consumo], [Nome]) VALUES (@Cod_Carro, @Marca, @Preco_Base, @Cod_ISV, @CC, @Potencia, @binario, @transmissao, @consumo, @Nome)" SelectCommand="SELECT * FROM [Carro]" UpdateCommand="UPDATE Carro SET Marca = @Marca, Preco_Base = @Preco_Base, Cod_ISV = @Cod_ISV, CC = @CC, Potencia = @Potencia, binario = @binario, transmissao = @transmissao, consumo = @consumo, Nome = @Nome WHERE (Cod_Carro = @original_Cod_Carro)" ConflictDetection="CompareAllValues" DeleteCommand="DELETE FROM [Carro] WHERE [Cod_Carro] = @original_Cod_Carro AND (([Marca] = @original_Marca) OR ([Marca] IS NULL AND @original_Marca IS NULL)) AND (([Preco_Base] = @original_Preco_Base) OR ([Preco_Base] IS NULL AND @original_Preco_Base IS NULL)) AND (([Cod_ISV] = @original_Cod_ISV) OR ([Cod_ISV] IS NULL AND @original_Cod_ISV IS NULL)) AND (([CC] = @original_CC) OR ([CC] IS NULL AND @original_CC IS NULL)) AND (([Potencia] = @original_Potencia) OR ([Potencia] IS NULL AND @original_Potencia IS NULL)) AND (([binario] = @original_binario) OR ([binario] IS NULL AND @original_binario IS NULL)) AND (([transmissao] = @original_transmissao) OR ([transmissao] IS NULL AND @original_transmissao IS NULL)) AND (([consumo] = @original_consumo) OR ([consumo] IS NULL AND @original_consumo IS NULL)) AND (([Nome] = @original_Nome) OR ([Nome] IS NULL AND @original_Nome IS NULL))" OldValuesParameterFormatString="original_{0}"> <DeleteParameters> <asp:Parameter Name="original_Cod_Carro" Type="String" /> <asp:Parameter Name="original_Marca" Type="String" /> <asp:Parameter Name="original_Preco_Base" Type="Decimal" /> <asp:Parameter Name="original_Cod_ISV" Type="String" /> <asp:Parameter Name="original_CC" Type="Int32" /> <asp:Parameter Name="original_Potencia" Type="Int32" /> <asp:Parameter Name="original_binario" Type="Int32" /> <asp:Parameter Name="original_transmissao" Type="Int32" /> <asp:Parameter Name="original_consumo" Type="Int32" /> <asp:Parameter Name="original_Nome" Type="String" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="Cod_Carro" Type="String" /> <asp:Parameter Name="Marca" Type="String" /> <asp:Parameter Name="Preco_Base" Type="Decimal" /> <asp:Parameter Name="Cod_ISV" Type="String" /> <asp:Parameter Name="CC" Type="Int32" /> <asp:Parameter Name="Potencia" Type="Int32" /> <asp:Parameter Name="binario" Type="Int32" /> <asp:Parameter Name="transmissao" Type="Int32" /> <asp:Parameter Name="consumo" Type="Int32" /> <asp:Parameter Name="Nome" Type="String" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="Marca" Type="String" /> <asp:Parameter Name="Preco_Base" Type="Decimal" /> <asp:Parameter Name="Cod_ISV" Type="String" /> <asp:Parameter Name="CC" Type="Int32" /> <asp:Parameter Name="Potencia" Type="Int32" /> <asp:Parameter Name="binario" Type="Int32" /> <asp:Parameter Name="transmissao" Type="Int32" /> <asp:Parameter Name="consumo" Type="Int32" /> <asp:Parameter Name="Nome" Type="String" /> <asp:Parameter Name="original_Cod_Carro" Type="String" /> </UpdateParameters> </asp:SqlDataSource> </td> </tr> </table> </ContentTemplate> </asp:TabPanel> <asp:TabPanel ID="tbConfig" runat="server" HeaderText="Configurações" Height="100%"> <ContentTemplate> 2 </ContentTemplate> </asp:TabPanel> </asp:TabContainer></ContentTemplate> </asp:RoleGroup> </RoleGroups> </asp:LoginView> <br /> <br /> <br /> <br /> <br /> </asp:Content>
    
    

    Wednesday, July 6, 2011 7:46 AM

Answers

  • User-1635004338 posted

    Hi,

    Several points you need to check.

    Make sure in the data source of DetailsView you have a field called "id".
    This field is not null.
    Every record in this "id" field is in the options of the data source of DropDownList.

    Thanks,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 8, 2011 3:42 AM

All replies

  • User-1177472553 posted

    i guess ur problem is this:

    if ur dropdown has the following items

    <asp:dropdownlist id="ddl" runat="server">

    <asp:ListItem text="A" value="A" />

    <asp:ListItem text="B" value="B" />

    <asp:ListItem text="C" value="C" />

    </asp:dropdownlist>

    but what u r doing in ur code is setting dropdownlist to a value which never existed for example

    ddl.SelectedValue="D"; which is wrong as a Listitem with value "D" does not exist in ur scheme of things ..

    Happy Coding

    Wednesday, July 6, 2011 8:48 AM
  • User624134129 posted

    My problem isn't what you said.

    IN the table definition i definided diferently way from the field that are primary key in table ISV the field that are external key in table "Carro".

    So this fields need be equal(Including name).

     So the rigth code is this:

                                                                <asp:DropDownList ID="DropDownList2" AutoPostBack="True" runat="server" DataSourceID="ISVedit"
                                                                    DataTextField="Nome_ISV" DataValueField="id" SelectedValue='<%# Bind("id") %>'
                                                                    AppendDataBoundItems="True">
                                                                </asp:DropDownList>
                                                                <asp:SqlDataSource ID="ISVedit" runat="server" ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString %>"
                                                                    OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [ISV]">
                                                                </asp:SqlDataSource>
    Wednesday, July 6, 2011 9:47 AM
  • User-1635004338 posted

    Hi,

    Several points you need to check.

    Make sure in the data source of DetailsView you have a field called "id".
    This field is not null.
    Every record in this "id" field is in the options of the data source of DropDownList.

    Thanks,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 8, 2011 3:42 AM