views:

61

answers:

4

Very simple problem...but weird results. Im just trying to fill a combo box / 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 TKSEINC.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: 'TKSEINC.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\jhermiz\Documents\Visual Studio 2010\WebSites\TKCISProductKeys\Default.aspx.cs:line 26
       at _Default.Page_Load(Object sender, EventArgs e) in c:\Users\jhermiz\Documents\Visual Studio 2010\WebSites\TKCISProductKeys\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 missing here?

+5  A: 

the VendorName property on your Vendor class has to be public rather than private.

lincolnk
i didn't see you guys posted :)
Srinivas Reddy Thatiparthy
A: 

Change the Vendor class property

private string VendorName { get; set; }

to

public string VendorName { get; set; }

and you should be all good.

code4life
A: 

change your property public,

public  string VendorName { get; set; }
Srinivas Reddy Thatiparthy
A: 

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;
    }
}
Kavinda Gayashan
This would work. Questions, is it better to use the extra Private variable "vendorName". As apposed to the examples above where they only have the public VendorName ??
aron
I have posted a question on that. I learnt about auto properties in c# 3.0 just now from that. please read it.http://stackoverflow.com/questions/3120140/defining-properties-of-a-class
Kavinda Gayashan