附加信息:数据源是无效类型。它必须是IListSource,IEnumerable或IDataSource

时间:2016-08-30 05:40:37

标签: c# sql asp.net

我试图根据CustomerID从数据库中检索和显示用户数据。我在执行时遇到上述错误 这是我显示数据的代码。

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int customerID = Convert.ToInt32(Session["CustomerID"]);
            MyProfileGridView.DataSource = CustomerBL.GetCustomer(customerID);
            MyProfileGridView.DataBind(); 
        }
    }

获取客户方法代码:

public static Customer GetCustomer(int customerID)
    {
        Customer customer = new Customer();

        string query = "SELECT * FROM [Customers] WHERE [CustomerID] = @CustomerID";
        SqlCommand cmd = new SqlCommand(query);
        cmd.Parameters.AddWithValue("@CustomerID", SqlDbType.Text).Value = customerID;

        DataTable dt = DbUtility.GetRecordsInDataTable(cmd);
        if (dt.Rows.Count > 0)
        {
            customer.CustomerID = Convert.ToInt32(dt.Rows[0]["CustomerID"]);
            customer.LoginID = dt.Rows[0]["LoginID"].ToString();
            customer.Password = dt.Rows[0]["Password"].ToString();
            customer.CustomerName = dt.Rows[0]["CustomerName"].ToString();
            customer.ShopName = dt.Rows[0]["ShopName"].ToString();
            customer.Address = dt.Rows[0]["Address"].ToString();
            customer.Mobile1 = dt.Rows[0]["Mobile1"].ToString();
            customer.Mobile2 = dt.Rows[0]["Mobile2"].ToString();
            customer.ReferenceNumber = dt.Rows[0]["ReferenceNumber"].ToString();
            customer.SignUpDate = Convert.ToDateTime(dt.Rows[0]["SignUpDate"]);
            customer.Enabled = Convert.ToBoolean(dt.Rows[0]["Enabled"]);
            return customer;
        }
        else
        {
            return null;
        }

    }

用户注册页码:

protected void Page_Load(object sender, EventArgs e)
    {
        int customerID;

        if (!Page.IsPostBack)
        {
            if (Request.QueryString["CustomerID"] != null)
            {

                customerID = Convert.ToInt32(Request.QueryString["CustomerID"]);

                Customer customer = CustomerBL.GetCustomer(customerID);

                if (customer != null)
                {
                    LoginIdLabel.Text = customer.LoginID;
                    PasswordLabel.Text = customer.Password;
                }
            }

        }
    }

错误8无法隐式转换类型'System.Collections.Generic.List< ******。Entities.Customer>'到'******。实体。客户'C:\用户***** \桌面\新文件夹******* \ UserSignUpSuccess.aspx.cs 25 41 ******

请帮我解决这个问题

2 个答案:

答案 0 :(得分:0)

由于错误很明显,您需要将List<Customer>分配给DataSourceDataSource无法分配single object

您需要从List<Customer>方法返回GetCustomer并指定给DataSource

public static List<Customer> GetCustomer(int customerID)
{
    Customer customer = new Customer();
    List<Customer> list = new List<Customer>();
    string query = "SELECT * FROM [Customers] WHERE [CustomerID] = @CustomerID";
    SqlCommand cmd = new SqlCommand(query);
    cmd.Parameters.AddWithValue("@CustomerID", SqlDbType.Text).Value = customerID;

    DataTable dt = DbUtility.GetRecordsInDataTable(cmd);
    if (dt.Rows.Count > 0)
    {
        customer.CustomerID = Convert.ToInt32(dt.Rows[0]["CustomerID"]);
        customer.LoginID = dt.Rows[0]["LoginID"].ToString();
        customer.Password = dt.Rows[0]["Password"].ToString();
        customer.CustomerName = dt.Rows[0]["CustomerName"].ToString();
        customer.ShopName = dt.Rows[0]["ShopName"].ToString();
        customer.Address = dt.Rows[0]["Address"].ToString();
        customer.Mobile1 = dt.Rows[0]["Mobile1"].ToString();
        customer.Mobile2 = dt.Rows[0]["Mobile2"].ToString();
        customer.ReferenceNumber = dt.Rows[0]["ReferenceNumber"].ToString();
        customer.SignUpDate = Convert.ToDateTime(dt.Rows[0]["SignUpDate"]);
        customer.Enabled = Convert.ToBoolean(dt.Rows[0]["Enabled"]);
        list.Add(customer);            
        return list;
    }
    else
    {
        return null;
    }

}

您的GetCustomer方法返回List`,因此您需要从列表中获取对象以访问其属性。

Customer customer = CustomerBL.GetCustomer(customerID).FirstOrDefault();

答案 1 :(得分:0)

问题是您正在向请求Customer类型数据源的GridView组件插入一个且仅List个对象。顺便说一句,如果你想将该客户对象添加到GridView,你可以尝试返回一个列表,

    public static List<Customer> GetCustomer(int customerID)
        {
            List<Customer> CusList = new List<Customer>();
            Customer customer = new Customer();
            .... // Before returning,
            CusList.Add(customer);
            return CusList;
        }