loading ASP.NET DropDownList
Very simple problem...but weird results. Im just trying to fill a drop down list in c#
protected void Page_Load(object sender, EventArgs e)
{
if (!(Page.IsPostBack))
{
LoadDropDowns();
}
}
protected void LoadDropDowns()
{
ddlVendor.DataSource = BL.GetAllVendors();
ddlVendor.DataTextField = "VendorName";
ddlVendor.DataValueField = "VendorName";
ddlVendor.DataBind();
}
BL.GetAllVendors is simply a static class which does this:
public static List<Vendor> GetAllVendors()
{
return DL.GetAllVendors();
}
And DL.GetAllVendors is also static class (the data layer) that goes out builds the List:
public static List<Vendor> GetAllVendors()
{
using(SqlConnection con = new SqlConnection(connString))
{
//use sproc
SqlCommand cmd = new SqlCommand("selAllVendors", con);
cmd.CommandType = CommandType.StoredProcedure;
//temporary storage of list of vendors
List<Vendor> lv = new List<Vendor>();
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
//continue as long as we have vendors
while (reader.Read())
{
//instantiate a vendor
Vendor v = new Vendor((string)reader["VendorName"]);
//add them to the list
lv.Add(v);
}
//clean the reader
reader.Close();
reader = null;
//return that list
return lv;
}
}
The vendor class is simple:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Vendor
/// </summary>
namespace TK.Objects
{
public class Vendor
{
private string VendorName { get; set; }
public Vendor(string vn)
{
VendorName = vn;
}
}
}
I know my sproc is correct because I did a debug / watch and I see it pulls off two records "All", and "Microsoft".
And the sproc is simple:
CREATE PROCEDURE selAllVendors
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT
Vendor AS VendorName,
1 as SortOrder
FROM
Vendor
UNION ALL
SELECT
'All' AS VendorName,
0 as SortOrder
ORDER BY
SortOrder,
Vendor
END
GO
But when I run this code I get an error:
System.Web.HttpException was unhandled by user code
Message=DataBinding: 'TK.Objects.Vendor' does not contain a property with the name 'VendorName'.
Source=System.Web
ErrorCode=-2147467259
WebEventCode=0
StackTrace:
at System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName)
at System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName, String format)
at System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource)
at System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e)
at System.Web.UI.WebControls.ListControl.PerformSelect()
at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
at _Default.LoadDropDowns() in c:\Users\Documents\Visual Studio 2010\WebSites\T\Default.aspx.cs:line 26
at _Default.Page_Load(Object sender, EventArgs e) in c:\Users\Documents\Visual Studio 2010\WebSites\T\Default.aspx.cs:line 17
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
What am I开发者_如何学Go missing here?
the VendorName
property on your Vendor
class has to be public rather than private.
Change the Vendor class property
private string VendorName { get; set; }
to
public string VendorName { get; set; }
and you should be all good.
you can use this code for class Vendor.
public class Vendor
{
private string vendorName = string.Empty;
public string VendorName
{
get { return vendorName; }
set { vendorName = value; }
}
public Vendor(string vn)
{
VendorName = vn;
}
}
精彩评论