locked
Gridview templatefield dropdownlist errors RRS feed

  • Question

  • User-12361734 posted

    Hello

     I hope someone can help. I have spent two days getting more and more frustrated....

     I am developing a web application in VWD. I have a lovely page where the user can add features to a product. I have two different Gridviews that work the same way but with different underlying data (except for one field (Price Code) which comes from the same SQLDataSource object).

     One of the Gridviews works perfectly and the other doesn't work at all. The error occurs on this Price Code field, which has me completely baffled because it works in the other one.

    The error I am getting is

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

    The value DOES exist and is valid.

     I have tried everything and am at my wits' end.

    I cannot appear to access the dropdownlist from code behind in order to manipulate the data manually.

    Thanks in advance.

     

    Thursday, August 9, 2007 6:49 AM

Answers

  • User-1952482173 posted

    Are you sure in your table Megasafty_PriceCodes the column PF_PriceCode has the values same to PC_Id? Are not there some NULL values in the column? The column PF_PriceCode in the two tables has same dataType? There are no reasons there the first works but the second not.

    I would like to create a method on code-behind to trace the value passed:

    protected string GetSelectedIndex(string code)

    {

            return code.Trim() != "" ? code : 1;

           //or, just return 1, or 2 for test

          //And, you can put breakPoint here to see want value of code passed 

    }

    On your web page, modify this line:

    <asp:DropDownList ID="DropDownList3" runat="server" AppendDataBoundItems="True"

    AutoPostBack="True" DataSourceID="sdsPriceCodes" DataTextField="PC_PriceCode" DataValueField="PC_ID"

    SelectedValue='<%# GetSelectedIndex( Bind("PF_PriceCode").ToString() ) %>'>

    </asp:DropDownList>

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 9, 2007 3:44 PM

All replies

  • User-1952482173 posted

    That error message means that your DropWodnList's values didn't match your DataSource's value. For example, your list has value 1,2,3, but your datasource has value 1,2,4. Here, 4 is not in the list.

    You need to check your datasource to make sure the value matchs your list value. Or, you need to add an empty item in your list on the top, if catch the error, set the selected index as 0 do that the first empty item will be selected.

    Hope it helps.

    Thursday, August 9, 2007 8:42 AM
  • User-12361734 posted

    Thanks for that, but the values ARE in the list. They are valid.

     I have also tried adding an empty item to the list and that made no difference.

    The data is the same in the gridview that works. That's what's so puzzling.

    Any other ideas would be very welcome!

    Best wishes

    MaidMarion

    Thursday, August 9, 2007 8:55 AM
  • User-1952482173 posted

    What is the data type of your List value? Integer or String? If string, any space exists? Anyway, your data value must not match your list value. Please post your code if you need further help.

    Thursday, August 9, 2007 11:59 AM
  • User-12361734 posted

    Thank you very much for your help.

     The data is integer. Here is the markup code.

    This is the one that works:

    <asp:GridView ID="gvwProdSize" runat="server" AllowPaging="True" AllowSorting="True"

    AutoGenerateColumns="False" DataKeyNames="PS_ID" DataSourceID="sdsProductSizes">

    <Columns>

    <asp:BoundField DataField="PS_SID" HeaderText="PS_SID" SortExpression="PS_SID" Visible="False" />

    <asp:BoundField DataField="S_ID" HeaderText="S_ID" InsertVisible="False" ReadOnly="True"

    SortExpression="S_ID" Visible="False" />

    <asp:TemplateField HeaderText="Size" SortExpression="S_Size">

    <EditItemTemplate>

    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="sdsSizes" DataTextField="S_Size"

    DataValueField="S_ID" SelectedValue='<%# Bind("PS_SID") %>'>

    </asp:DropDownList>

    </EditItemTemplate>

    <ItemTemplate>

    <asp:Label ID="Label1" runat="server" Text='<%# Eval("S_Size") %>'></asp:Label>

    </ItemTemplate>

    </asp:TemplateField>

    <asp:BoundField DataField="PS_PriceCode" HeaderText="PS_PriceCode" SortExpression="PS_PriceCode"

    Visible="False" />

    <asp:BoundField DataField="PC_ID" HeaderText="PC_ID" InsertVisible="False" ReadOnly="True"

    SortExpression="PC_ID" Visible="False" />

    <asp:TemplateField HeaderText="Price Code" SortExpression="PC_PriceCode">

    <EditItemTemplate>

    <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="sdsPriceCodes"

    DataTextField="PC_PriceCode" DataValueField="PC_ID" SelectedValue='<%# Bind("PS_PriceCode") %>'>

    </asp:DropDownList>

    </EditItemTemplate>

    <ItemTemplate>

    <asp:Label ID="Label2" runat="server" Text='<%# Bind("PC_PriceCode") %>'></asp:Label>

    </ItemTemplate>

    </asp:TemplateField>

    <asp:CommandField DeleteText="Remove" ShowDeleteButton="True" ShowEditButton="True" />

    </Columns>

    </asp:GridView>

    And this is the one that doesn't: 

    <asp:GridView ID="gvwProdFeat" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="sdsProductFeatures" DataKeyNames="PF_ID">

    <Columns>

    <asp:BoundField DataField="PF_ID" HeaderText="PF_ID" InsertVisible="False" SortExpression="PF_ID"

    Visible="False" />

    <asp:BoundField DataField="PF_PID" HeaderText="PF_PID" SortExpression="PF_PID" Visible="False" />

    <asp:TemplateField HeaderText="Feature" SortExpression="PF_Feature">

    <EditItemTemplate>

    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("PF_Feature") %>'></asp:TextBox>

    </EditItemTemplate>

    <ItemTemplate>

    <asp:Label ID="Label1" runat="server" Text='<%# Bind("PF_Feature") %>'></asp:Label>

    </ItemTemplate>

    </asp:TemplateField>

    <asp:BoundField DataField="PF_PriceCode" HeaderText="PF_PriceCode" SortExpression="PF_PriceCode" Visible="False" />

    <asp:BoundField DataField="PC_ID" HeaderText="PC_ID" InsertVisible="False" ReadOnly="True"

    SortExpression="PC_ID" Visible="False" />

    <asp:TemplateField HeaderText="Price Code" SortExpression="PC_PriceCode">

    <EditItemTemplate>

    &nbsp;<asp:DropDownList ID="DropDownList3" runat="server" AppendDataBoundItems="True"

    AutoPostBack="True" DataSourceID="sdsPriceCodes" DataTextField="PC_PriceCode" DataValueField="PC_ID"

    SelectedValue='<%# Bind("PF_PriceCode") %>'>

    </asp:DropDownList>

    </EditItemTemplate>

    <ItemTemplate>

    <asp:Label ID="Label2" runat="server" Text='<%# Bind("PC_PriceCode") %>'></asp:Label>

    </ItemTemplate>

    </asp:TemplateField>

    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />

    </Columns>

    </asp:GridView>

    <asp:SqlDataSource ID="sdsProductSizes" runat="server" ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString1 %>"

    DeleteCommand="Delete from Megasafety_ProductSizes where PS_ID = @PS_ID" SelectCommand="select PS_ID, PS_SID, S_ID, S_Size, PS_PriceCode, PC_ID, PC_PriceCode from Megasafety_ProductSizes, Megasafety_PriceCodes, Megasafety_Sizes where S_ID = PS_SID and PC_ID = PS_PriceCode and PS_PID = @PID order by PS_ID"

    UpdateCommand="update Megasafety_ProductSizes set PS_SID = @PS_SID, PS_PriceCode = @PS_PriceCode where PS_ID = @PS_ID">

    <SelectParameters>

    <asp:QueryStringParameter Name="PID" QueryStringField="pid" />

    </SelectParameters>

    <DeleteParameters>

    <asp:Parameter Name="PS_ID" />

    </DeleteParameters>

    <UpdateParameters>

    <asp:Parameter ConvertEmptyStringToNull="False" Name="PS_SID" />

    <asp:Parameter Name="PS_PriceCode" ConvertEmptyStringToNull="False" />

    <asp:Parameter Name="PS_ID" ConvertEmptyStringToNull="False" />

    </UpdateParameters>

    </asp:SqlDataSource>

    <asp:SqlDataSource ID="sdsSizes" runat="server" ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString1 %>"

    SelectCommand="select * from megasafety_sizes"></asp:SqlDataSource>

    <asp:SqlDataSource ID="sdsPriceCodes" runat="server" ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString1 %>"

    SelectCommand="SELECT [PC_ID], [PC_PriceCode] FROM [Megasafety_PriceCodes]"></asp:SqlDataSource>

    <asp:SqlDataSource ID="sdsProductFeatures" runat="server" ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString1 %>"

    DeleteCommand="Delete from Megasafety_ProductFeatures where PF_ID = @PF_ID"

    SelectCommand="Select PF_ID, PF_PID, PF_Feature, PF_PriceCode, PC_ID, PC_PriceCode from Megasafety_ProductFeatures, Megasafety_PriceCodes where PF_PID = @PID and PC_ID = PF_PriceCode"

    UpdateCommand="Update Megasafety_ProductFeatures set PF_Feature = @PF_Feature, PF_PriceCode = @PF_PriceCode where PF_ID = @PF_ID">

    <DeleteParameters>

    <asp:Parameter Name="PF_ID" />

    </DeleteParameters>

    <UpdateParameters>

    <asp:Parameter Name="PF_Feature" />

    <asp:Parameter Name="PF_PriceCode" />

    <asp:Parameter Name="PF_ID" />

    </UpdateParameters>

    <SelectParameters>

    <asp:QueryStringParameter Name="PID" QueryStringField="PID" />

    </SelectParameters>

    </asp:SqlDataSource>

    I hope this helps to shed some light on it. The values in the list are 1, 2 and 3 and the values in the gridview table are 1 and 2.

    Kind regards

    Thursday, August 9, 2007 12:26 PM
  • User-1952482173 posted

    Are you sure in your table Megasafty_PriceCodes the column PF_PriceCode has the values same to PC_Id? Are not there some NULL values in the column? The column PF_PriceCode in the two tables has same dataType? There are no reasons there the first works but the second not.

    I would like to create a method on code-behind to trace the value passed:

    protected string GetSelectedIndex(string code)

    {

            return code.Trim() != "" ? code : 1;

           //or, just return 1, or 2 for test

          //And, you can put breakPoint here to see want value of code passed 

    }

    On your web page, modify this line:

    <asp:DropDownList ID="DropDownList3" runat="server" AppendDataBoundItems="True"

    AutoPostBack="True" DataSourceID="sdsPriceCodes" DataTextField="PC_PriceCode" DataValueField="PC_ID"

    SelectedValue='<%# GetSelectedIndex( Bind("PF_PriceCode").ToString() ) %>'>

    </asp:DropDownList>

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 9, 2007 3:44 PM
  • User-12361734 posted

    Thank you SO much! It was the datatype! Now it works perfectly.

    I really appreciate you putting so much effort into this for me.

    Best wishes

    MaidMarion

    Friday, August 10, 2007 2:06 AM