'ddlDeviceType' has a SelectedValue which is invalid because it does not exist in the list of items.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hey guys,

Having a bit of a problem, ive only been programming in C# and ASP.net for 3/4 months but Im getting the hang of it. Just need some help on something which Im sure is minor.
Im trying to put dropdown lists in my gridview but not having success.
The error Im getting is

"ddlDeviceType has a SelectedValue which is invalid because it does not exist in the list of items.<br/>
Parameter name: value"

This is getting frustrating ,but hey thats programming. Any help would be great.

I followed this tutorial step by step but now get the error.
http://msdn.microsoft.com/en-us/library/ms178294(VS.80).aspx MSDN Tutorial
Heres my code, thanks for any help guys!
ASP Code
<pre class="prettyprint <%@ Page Title = "Show Equipment" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="ShowEquipment.aspx.cs" Inherits="ShowEquipment" %>

<%@ Register assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI.DataVisualization.Charting" tagprefix="asp" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent

<style type="text/css
body
{
background-image:url(RBKSmall.png);
}
.All
{
height: 268px;
width: 918px;
}
</style>
</asp:Content>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent

<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:TemplateField HeaderText="DeviceType" SortExpression="DeviceType
<EditItemTemplate>
<asp:DropDownList ID="ddlDeviceType" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Description"
DataValueField="Description" SelectedValue=<%# Bind("DeviceType") %>>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="ddlDeviceType" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Description"
DataValueField="Description" SelectedValue=<%# Bind("DeviceType") %>>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Manufacturer" HeaderText="Manufacturer"
SortExpression="Manufacturer" />
<asp:BoundField DataField="SerialNumber" HeaderText="SerialNumber"
SortExpression="SerialNumber" />
<asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" />
<asp:BoundField DataField="Warrenty" HeaderText="Warrenty"
SortExpression="Warrenty" />
<asp:BoundField DataField="CarePack" HeaderText="CarePack"
SortExpression="CarePack" />
<asp:BoundField DataField="AssetTag" HeaderText="AssetTag"
SortExpression="AssetTag" />
<asp:BoundField DataField="DateOfPurchase" HeaderText="DateOfPurchase"
SortExpression="DateOfPurchase" />
<asp:BoundField DataField="CostPrice" HeaderText="CostPrice"
SortExpression="CostPrice" />
<asp:BoundField DataField="DepreciationType" HeaderText="DepreciationType"
SortExpression="DepreciationType" />
<asp:BoundField DataField="DepreciationRate" HeaderText="DepreciationRate"
SortExpression="DepreciationRate" />
<asp:BoundField DataField="TotalDepreciation" HeaderText="TotalDepreciation"
SortExpression="TotalDepreciation" />
<asp:BoundField DataField="NetBookValue" HeaderText="NetBookValue"
SortExpression="NetBookValue" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:MarksDatabaseConnectionString %>"
DeleteCommand="DELETE FROM [Equipment] WHERE [ID] = @original_ID AND [DeviceType] = @original_DeviceType AND [Manufacturer] = @original_Manufacturer AND [SerialNumber] = @original_SerialNumber AND [Model] = @original_Model AND [Warrenty] = @original_Warrenty AND (([CarePack] = @original_CarePack) OR ([CarePack] IS NULL AND @original_CarePack IS NULL)) AND (([AssetTag] = @original_AssetTag) OR ([AssetTag] IS NULL AND @original_AssetTag IS NULL)) AND [DateOfPurchase] = @original_DateOfPurchase AND [CostPrice] = @original_CostPrice AND (([DepreciationType] = @original_DepreciationType) OR ([DepreciationType] IS NULL AND @original_DepreciationType IS NULL)) AND (([DepreciationRate] = @original_DepreciationRate) OR ([DepreciationRate] IS NULL AND @original_DepreciationRate IS NULL)) AND (([TotalDepreciation] = @original_TotalDepreciation) OR ([TotalDepreciation] IS NULL AND @original_TotalDepreciation IS NULL)) AND (([NetBookValue] = @original_NetBookValue) OR ([NetBookValue] IS NULL AND @original_NetBookValue IS NULL))"
InsertCommand="INSERT INTO [Equipment] ([DeviceType], [Manufacturer], [SerialNumber], [Model], [Warrenty], [CarePack], [AssetTag], [DateOfPurchase], [CostPrice], [DepreciationType], [DepreciationRate], [TotalDepreciation], [NetBookValue]) VALUES (@DeviceType, @Manufacturer, @SerialNumber, @Model, @Warrenty, @CarePack, @AssetTag, @DateOfPurchase, @CostPrice, @DepreciationType, @DepreciationRate, @TotalDepreciation, @NetBookValue)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT * FROM [Equipment]"
UpdateCommand="UPDATE [Equipment] SET [DeviceType] = @DeviceType, [Manufacturer] = @Manufacturer, [SerialNumber] = @SerialNumber, [Model] = @Model, [Warrenty] = @Warrenty, [CarePack] = @CarePack, [AssetTag] = @AssetTag, [DateOfPurchase] = @DateOfPurchase, [CostPrice] = @CostPrice, [DepreciationType] = @DepreciationType, [DepreciationRate] = @DepreciationRate, [TotalDepreciation] = @TotalDepreciation, [NetBookValue] = @NetBookValue WHERE [ID] = @original_ID AND [DeviceType] = @original_DeviceType AND [Manufacturer] = @original_Manufacturer AND [SerialNumber] = @original_SerialNumber AND [Model] = @original_Model AND [Warrenty] = @original_Warrenty AND (([CarePack] = @original_CarePack) OR ([CarePack] IS NULL AND @original_CarePack IS NULL)) AND (([AssetTag] = @original_AssetTag) OR ([AssetTag] IS NULL AND @original_AssetTag IS NULL)) AND [DateOfPurchase] = @original_DateOfPurchase AND [CostPrice] = @original_CostPrice AND (([DepreciationType] = @original_DepreciationType) OR ([DepreciationType] IS NULL AND @original_DepreciationType IS NULL)) AND (([DepreciationRate] = @original_DepreciationRate) OR ([DepreciationRate] IS NULL AND @original_DepreciationRate IS NULL)) AND (([TotalDepreciation] = @original_TotalDepreciation) OR ([TotalDepreciation] IS NULL AND @original_TotalDepreciation IS NULL)) AND (([NetBookValue] = @original_NetBookValue) OR ([NetBookValue] IS NULL AND @original_NetBookValue IS NULL))
<DeleteParameters>
<asp:Parameter Name="original_ID" Type="Int32" />
<asp:Parameter Name="original_DeviceType" Type="Int32" />
<asp:Parameter Name="original_Manufacturer" Type="Int32" />
<asp:Parameter Name="original_SerialNumber" Type="String" />
<asp:Parameter Name="original_Model" Type="Int32" />
<asp:Parameter Name="original_Warrenty" Type="Int32" />
<asp:Parameter Name="original_CarePack" Type="Int32" />
<asp:Parameter Name="original_AssetTag" Type="Int32" />
<asp:Parameter DbType="Date" Name="original_DateOfPurchase" />
<asp:Parameter Name="original_CostPrice" Type="Decimal" />
<asp:Parameter Name="original_DepreciationType" Type="Int32" />
<asp:Parameter Name="original_DepreciationRate" Type="Int32" />
<asp:Parameter Name="original_TotalDepreciation" Type="Decimal" />
<asp:Parameter Name="original_NetBookValue" Type="Decimal" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="DeviceType" Type="Int32" />
<asp:Parameter Name="Manufacturer" Type="Int32" />
<asp:Parameter Name="SerialNumber" Type="String" />
<asp:Parameter Name="Model" Type="Int32" />
<asp:Parameter Name="Warrenty" Type="Int32" />
<asp:Parameter Name="CarePack" Type="Int32" />
<asp:Parameter Name="AssetTag" Type="Int32" />
<asp:Parameter DbType="Date" Name="DateOfPurchase" />
<asp:Parameter Name="CostPrice" Type="Decimal" />
<asp:Parameter Name="DepreciationType" Type="Int32" />
<asp:Parameter Name="DepreciationRate" Type="Int32" />
<asp:Parameter Name="TotalDepreciation" Type="Decimal" />
<asp:Parameter Name="NetBookValue" Type="Decimal" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="DeviceType" Type="Int32" />
<asp:Parameter Name="Manufacturer" Type="Int32" />
<asp:Parameter Name="SerialNumber" Type="String" />
<asp:Parameter Name="Model" Type="Int32" />
<asp:Parameter Name="Warrenty" Type="Int32" />
<asp:Parameter Name="CarePack" Type="Int32" />
<asp:Parameter Name="AssetTag" Type="Int32" />
<asp:Parameter DbType="Date" Name="DateOfPurchase" />
<asp:Parameter Name="CostPrice" Type="Decimal" />
<asp:Parameter Name="DepreciationType" Type="Int32" />
<asp:Parameter Name="DepreciationRate" Type="Int32" />
<asp:Parameter Name="TotalDepreciation" Type="Decimal" />
<asp:Parameter Name="NetBookValue" Type="Decimal" />
<asp:Parameter Name="original_ID" Type="Int32" />
<asp:Parameter Name="original_DeviceType" Type="Int32" />
<asp:Parameter Name="original_Manufacturer" Type="Int32" />
<asp:Parameter Name="original_SerialNumber" Type="String" />
<asp:Parameter Name="original_Model" Type="Int32" />
<asp:Parameter Name="original_Warrenty" Type="Int32" />
<asp:Parameter Name="original_CarePack" Type="Int32" />
<asp:Parameter Name="original_AssetTag" Type="Int32" />
<asp:Parameter DbType="Date" Name="original_DateOfPurchase" />
<asp:Parameter Name="original_CostPrice" Type="Decimal" />
<asp:Parameter Name="original_DepreciationType" Type="Int32" />
<asp:Parameter Name="original_DepreciationRate" Type="Int32" />
<asp:Parameter Name="original_TotalDepreciation" Type="Decimal" />
<asp:Parameter Name="original_NetBookValue" Type="Decimal" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<br />
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MarksDatabaseConnectionString %>"
SelectCommand="SELECT DISTINCT [Description] FROM [DeviceType]
</asp:SqlDataSource>
<br />

</asp:Content>



[/code]
<br/>
Hope I made sense!
Thanks again. http://msdn.microsoft.com/en-us/library/ms178294(VS.80).aspx
<br/>


View the full article
 
Back
Top