|
Hi all, I’m testing a web control with Northwind
Database. Using a drop down list: protected System.Web.UI.WebControls.DropDownList
TestDDL; Suppose, that the connection is successful with the
table “Categories” and I would like to view in the Drop Down List
the “CategoryName” Columns, but I’d like to obtain on the
other hand the “CategoryID” to continue with my processing since
what concerns me is the ID and not the name. When a user selects one of the
displayed names, i’d like to capture the corresponding ID of that name in
the database table. For instance: private void Page_Load(object sender,
System.EventArgs e) {
NorthWSqlConn.Open(); // Connection opened
String NorthWSqlCmdStr = "SELECT CategoryName FROM Categories ORDER BY
CategoryName"; //SQL Select Statement
SqlCommand NorthWSqlCmd = new SqlCommand (NorthWSqlCmdStr,NorthWSqlConn);
TestDDL.DataTextField = "CategoryName"; //Here to map the displayed
text with the database table column
//TestDDL.DataValueField = "CategoryID"; // this fails if I uncomment
NorthWSqlDA.Fill(NorthWDS);
TestDDL.DataSource = NorthWSqlCmd.ExecuteReader();
TestDDL.DataBind();
NorthWSqlConn.Close(); } My problem is with the commented line; namely,
//TestDDL.DataValueField = "CategoryID"; If uncommented, i get the following error: Server Error in '/WebApplication1' Application. -------------------------------------------------------------------------------- DataBinder.Eval: 'System.Data.Common.DbDataRecord'
does not contain a property with the name CategoryID. 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:
DataBinder.Eval: 'System.Data.Common.DbDataRecord' does not contain a property
with the name CategoryID. Source Error: Line 36:
NorthWSqlDA.Fill(NorthWDS); Line 37:
TestDDL.DataSource = NorthWSqlCmd.ExecuteReader(); Line 38:
TestDDL.DataBind(); Line 39:
NorthWSqlConn.Close(); Line 40:
} //Red Line on Line 38 Stack Trace: [HttpException (0x80004005): DataBinder.Eval:
'System.Data.Common.DbDataRecord' does not contain a property with the name
CategoryID.]
System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName)
+147 System.Web.UI.DataBinder.GetPropertyValue(Object
container, String propName, String format) +12
System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +472 System.Web.UI.Control.DataBind() +26 TestWebApp.TestWF.Page_Load(Object
sender, EventArgs e) in c:\inetpub\wwwroot\webapplication1\testwf.aspx.cs:38 System.Web.UI.Control.OnLoad(EventArgs
e) +67 System.Web.UI.Control.LoadRecursive()
+35 System.Web.UI.Page.ProcessRequestMain()
+750 Now how do I some how solve this? -------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework
Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032 Yahoo! Groups Links
|
- Re: [Microsofts_C_Sharp] DataValueField Problem Coosa A.
- Re: [Microsofts_C_Sharp] DataValueField Problem Chris Cottrell
