附加信息:数据源类型无效.它必须是IListSource、IEnumerable或IDataSource
本文关键字:IListSource IEnumerable IDataSource 信息 数据源 无效 类型 | 更新日期: 2023-09-27 18:04:51
我试图检索和显示基于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>' to '******.实体。客户"C: '桌面用户* * * * * ' '新文件夹* * * * * * * ' UserSignUpSuccess.aspx.cs 25 41 ******
请帮我解决这个问题
由于错误足够明显,您需要将List<Customer>
分配给DataSource
。DataSource
不能被分配给single object
您需要从GetCustomer
方法返回List<Customer>
,并分配给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 ',所以你需要从List中获取对象来访问它的属性。
Customer customer = CustomerBL.GetCustomer(customerID).FirstOrDefault();
问题是您正在向请求List
类型数据源的GridView
组件插入一个且仅Customer
对象。顺便说一下,如果您想将客户对象添加到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;
}