Orion Orion - 3 months ago 22
ASP.NET (C#) Question

Additional information: Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource

I m trying to retrieve and display user data from database based on CustomerID. I get above error while executing.

Here is my code to display data.

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


Get Customer Method code:

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;
}

}


User SignUp Page code:

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;
}
}

}
}


Error 8 Cannot implicitly convert type 'System.Collections.Generic.List<******.Entities.Customer>' to '******.Entities.Customer' C:\Users*****\Desktop\New folder*******\UserSignUpSuccess.aspx.cs 25 41 ******

Kindly help me to solve this issue

Answer

As error is clear enough you need to assign List<Customer> to DataSource. DataSource can't be assigned with a single object.

You need to return List<Customer> from GetCustomer method and assign to 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;
    }

}

Your GetCustomer method return List` so you need to get object from list to access its properties.

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