Showing posts with label category. Show all posts
Showing posts with label category. Show all posts

Thursday, 8 March 2012

Connect 2 DropDownList and Received "Input string was not in a correct format"

I have 2 DropDownList, the first one to display the product category and the other one to display the products based on the selected category. However, when I run code I received the following error message:Input string was not in a correct format.

Please find the code below.

Thanks,

Andy.

<%@.PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<div>

<asp:DropDownListID="DropDownList1"runat="server"DataSourceID="SqlDataSource1"

DataTextField="Name"DataValueField="Name">

</asp:DropDownList>

<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:Assignment2ConnectionString %>"

SelectCommand="SELECT [Name] FROM [Category]"></asp:SqlDataSource>

<br/>

<asp:DropDownListID="DropDownList2"runat="server"DataSourceID="SqlDataSource2"

DataTextField="Name"DataValueField="Name">

</asp:DropDownList>

<asp:SqlDataSourceID="SqlDataSource2"runat="server"ConnectionString="<%$ ConnectionStrings:Assignment2ConnectionString %>"

SelectCommand="SELECT [Name] FROM [Product] WHERE ([CategoryID] = @.CategoryID2)">

<SelectParameters>

<asp:ControlParameterControlID="DropDownList1"Name="CategoryID2"PropertyName="SelectedValue"

Type="Int32"/>

</SelectParameters>

</asp:SqlDataSource>

</div>

</form>

</body>

</html>

Assume you have a categoryID field in your Category table: You add CategoryID to the SELECT statement and changeDataValueField="CategoryID" .

<asp:DropDownListID="DropDownList1"runat="server"DataSourceID="SqlDataSource1"

DataTextField="Name"DataValueField="CategoryID">

</asp:DropDownList>

<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:Assignment2ConnectionString %>"

SelectCommand="SELECT [Name],[CategoryID] FROM [Category]"></asp:SqlDataSource>

<br/>