Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i am using where statement for selecting data it is giving error
An invalid data source is being used for DataList1. A valid data source must implement either IListSource or IEnumerable.

presentation layer

C#
protected void btnsearch_Click(object sender, EventArgs e)
        {
            Books language = new Books()
            {
                Language = txtlanguage.Text.Trim(),
            };
            List<Books> l = modle1.GetBookbylanguage(language);
            DataList1.DataSource = language;
            DataList1.DataBind();

        }


collection

C#
public class OnlinebookCollection<T> : List<T> where T : OnlineBookBase
    {
        public string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["OnlineBookConnectionString"].ToString();

        /// <summary>
        /// Used to Fetch Data from Database.
        /// </summary>
        /// <param name="command"></param>
        public void ExecuteQuery(SqlCommand command)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    using (SqlCommand cmd = command)
                    {
                        cmd.Connection = con;
                        con.Open();
                        SqlDataReader reader = cmd.ExecuteReader();
                        if (reader.HasRows)
                        {
                            Load(reader);
                        }

                        reader.Close();
                        reader.Dispose();
                    }
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        /// <summary>
        /// Used to Execute the command.
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public int ExecuteNonQuery(SqlCommand command)
        {
            try
            {
                int result = 0;
                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    using (SqlCommand cmd = command)
                    {
                        cmd.Connection = con;
                        con.Open();
                        result = cmd.ExecuteNonQuery();
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public object ExecuteScalar(SqlCommand command)
        {
            try
            {
                object result = null;
                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    using (SqlCommand cmd = command)
                    {
                        cmd.Connection = con;
                        con.Open();
                        result = cmd.ExecuteScalar();
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual void Load(SqlDataReader reader)
        {
        }

    }
}
Posted

1 solution

Look at your code:
C#
Books language = new Books()
{
    Language = txtlanguage.Text.Trim(),
};
List<Books> l = modle1.GetBookbylanguage(language);
DataList1.DataSource = language;


Books is not a class that implements IListSource or IEnumerable - did you mean to declare language as a OnlinebookCollection instead? Or perhaps use l as your datasource?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900