开发者

Error binding DataAdapter->DataSet->LINQ->ASP.Net DataGrid

I have the following code:

using (SqlConnection cn = new SqlConnection(Connection.Instance.ConnectionString))
{
    // Open the connection
    using (SqlCommand cmd = new SqlCommand())
    {
        try
        {
            cmd.Connection = cn;

            cmd.CommandText = "Select Customers.CustomerID, Addresses.AddressCode, Addresses.FirstName, Addresses.LastName, Addresses.Address1, Addresses.City, Addresses.State, " +
            "Addresses.Zip, Addresses.Home AS HomePhone, Addresses.Phone AS WorkPhone, Addresses.EmailAddress  From Customers " +
            "LEFT OUTER JOIN Addresses ON Addresses.ID=Customers.AddressID " +
            "Where CustomerType IN ('HomeOwner', 'Home Owner') AND Customers.ResellerID=@ResellerID ";

            cmd.Parameters.AddWithValue("@ResellerID", base.UserID);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet dsCustomer = new DataSet();
            da.Fill(dsCustomer);

            var customers = from c in dsCustomer.Tables[0].AsEnumerable().AsQueryable()
                            where c.Field<string>("CustomerID") == txtSearchCriteria.Text
                            select c;

            dgCustomers.CurrentPageIndex = 0;

            dgCustomers.DataSource = customers;
            dgCustomers.DataBind(开发者_如何学JAVA);
        }
        catch (Exception e)
        {
            throw new Exception(e.Message + e.StackTrace);
        }
        finally
        {
            if ((cn != null) && (cn.State != ConnectionState.Closed))
                cn.Close();
        }
    }
}

Which is giving me the error

AllowCustomPaging must be true and VirtualItemCount must be set for a DataGrid with ID 'dgCustomers' when AllowPaging is set to true and the selected data source does not implement ICollection.   at System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource)

How do I convert this LINQ query so that it can be pagable?

Note: This is a simplified version of what I'm trying to do. I know in this example I could simply modify the SQL statement to include "And CustomerID=@CustomerID" and bypass LINQ completely. But, in the bigger picture, I can't do that.


The error message is clear, you need to implement your paging logic to take advantage from paging. BTW, to make your code to work just use a ICollection as DataSource, changing this line:

dgCustomers.DataSource = customers.ToList();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜