Reputation: 4028
I have a table 'TYPE' in this table i have 2 columns as 'SKEY' AND 'CASECODE'.
'SKEY' contains text value and 'CASECODE' contains the corresponding numeric value.
I have bound the values in the column 'SKEY' to a DropdownList control in ASP.net 3.5 C#, What i want is on selection of one item in the DropdownList the corresponding value in the column 'CASECODE' should be displayed.
I have created a datasource for a dropdown list and i want to have DataTextField which is visible to the user to be a value from column SKEY and dropdownlistDataValueField to be a value from column 'casecode' but when i try to do
<asp:DropDownList ID="DropDownList3" runat="server"
DataSourceID="SqlDataSource1" DataTextField="skey" DataValueField="casecode"
AppendDataBoundItems="True">
<asp:ListItem Selected="True" Value="S">Select</asp:ListItem>
</asp:DropDownList>
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'casecode'.
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.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'casecode'.
My .aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit.HTMLEditor" tagprefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form title="orderfrm" id="form1" runat="server" onsubmit="validate()" >
<div>
<br />
<b>
<asp:Label ID="Label1" runat="server" style="font-size: x-large"
Text="Order Modify"></asp:Label>
<br />
</b>
<br />
<asp:Label ID="Label2" runat="server" style="font-weight: 700"
Text="High Court:"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server"
>
<asp:ListItem>Panaji</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="Label3" runat="server" style="font-weight: 700"
Text="Stmp/Regn.:"></asp:Label>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Value="R">Registrer</asp:ListItem>
<asp:ListItem Value="S">Stamp</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:Label ID="Label4" runat="server" style="font-weight: 700" Text="Type:"></asp:Label>
<asp:DropDownList ID="DropDownList3" runat="server"
DataSourceID="SqlDataSource1" DataTextField="skey" DataValueField="casecode"
AppendDataBoundItems="True">
<asp:ListItem Selected="True" Value="S">Select</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="DropDownList3" ErrorMessage="Select Case Type"
InitialValue="S">*</asp:RequiredFieldValidator>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
ProviderName="<%$ ConnectionStrings:ConnectionString2.ProviderName %>"
SelectCommand="SELECT testcase.casetype.skey FROM testcase.casetype">
</asp:SqlDataSource>
<asp:Label ID="Label5" runat="server" style="font-weight: 700" Text="No"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox2" ErrorMessage="Enter Case No">*</asp:RequiredFieldValidator>
<asp:Label ID="Label6" runat="server" style="font-weight: 700" Text="Year:"></asp:Label>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox3" ErrorMessage="Enter Year">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ControlToValidate="TextBox3" ErrorMessage="Enter Valid Year"
ValidationExpression="^\d{4}$"></asp:RegularExpressionValidator>
<br />
<br />
<asp:Label ID="Label7" runat="server" style="font-weight: 700"
Text="Order Date"></asp:Label>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="TextBox4" ErrorMessage="Enter Date">*</asp:RequiredFieldValidator>
<asp:Label ID="Label8" runat="server" style="font-weight: 700"
Text="(dd/mm/yyyy)"></asp:Label>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox4" ErrorMessage="Enter Valid Date"
ValidationExpression="^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$"></asp:RegularExpressionValidator>
<br />
<asp:Button ID="Button2" runat="server" style="font-weight: 700" Text="Go"
onclick="Button2_Click"/>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
<br />
</div>
<cc1:Editor ID="Editor1" runat="server" AutoFocus="true" Height="200px" Width="500px" ActiveMode="Design" Font-Strikeout="false" />
<br />
<asp:ScriptManager ID="ScriptManager2"
runat="server" > </asp:ScriptManager>
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Show" />
<asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Select" />
<asp:Button ID="Button4" runat="server" onclick="Button4_Click"
Text="next page" />
</form>
</body>
</html>
Upvotes: 0
Views: 2699
Reputation: 96616
The select command of your data source does not include a field named casecode:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
ProviderName="<%$ ConnectionStrings:ConnectionString2.ProviderName %>"
SelectCommand="SELECT testcase.casetype.skey FROM testcase.casetype">
</asp:SqlDataSource>
Probably you want to edit SelectCommand
to something like this (depending on how your database looks like):
SelectCommand="SELECT testcase.casetype, testcase.casetype.skey FROM testcase.casetype"
Upvotes: 0
Reputation: 9986
I am not entirely sure what you are asking, but dropdownlist.SelectedValue
would give you the value which is the casecode
in this case.
Upvotes: 0
Reputation: 13843
Try binding your DataTextField and DataValueField -
dropdownlist.DataSource = <GetData>();
dropdownlistDataTextField="<SKEY column values>";
dropdownlistDataValueField="<CASECODE column values>";
dropdownlist.DataBind();
Upvotes: 0