DataList1使用了无效的数据源。有效的数据源必须实现IListSource或IEnumerable。 [英] An invalid data source is being used for DataList1. A valid data source must implement either IListSource or IEnumerable.

查看:351
本文介绍了DataList1使用了无效的数据源。有效的数据源必须实现IListSource或IEnumerable。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用where语句选择数据时它会给出错误

DataList1使用了无效的数据源。有效的数据源必须实现IListSource或IEnumerable。



表示层



 受保护  void  btnsearch_Click( object  sender,EventArgs e)
{
图书语言= 图书()
{
语言= txtlanguage.Text。修剪(),
};
列表<图书> l = modle1.GetBookbylanguage(language);
DataList1.DataSource = language;
DataList1.DataBind();

}





collection



< pre lang =cs> public class OnlinebookCollection< T> :列表< T> 其中 T:OnlineBookBase
{
public string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings [ OnlineBookConnectionString]。的ToString();

/// < 摘要 >
/// 用于从数据库中获取数据。
/// < / summary >
/// < param < span class =code-summarycomment> name =command > < / param >
public void ExecuteQuery(SqlCommand命令)
{
尝试
{
使用(SqlConnection con = < span class =code-keyword> new SqlConnection(connectionString))
{
使用(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 >
/// < param < span class =code-summarycomment> name =command > < / param >
/// < 返回 > < / return >
public int ExecuteNonQuery(SqlCommand command)
{
try
{
int result = 0 ;
使用(SqlConnection con = new SqlConnection(connectionString))
{
使用(SqlCommand cmd = command)
{
cmd.Connection = con;
con.Open();
result = cmd.ExecuteNonQuery();
}
}
返回结果;
}
catch (例外情况)
{
throw ex;
}
}

public object ExecuteScalar( SqlCommand命令)
{
尝试
{
object result = null ;
使用(SqlConnection con = new SqlConnection(connectionString))
{
使用(SqlCommand cmd = command)
{
cmd.Connection = con;
con.Open();
result = cmd.ExecuteScalar();
}
}
返回结果;
}
catch (例外情况)
{
throw ex;
}
}

public virtual void 加载(SqlDataReader reader)
{
}

}
}

解决方案

查看你的代码:

图书语言= 新的书籍()
{
语言= txtlanguage.Text.Trim(),
};
列表<图书> l = modle1.GetBookbylanguage(language);
DataList1.DataSource = language;





书籍不是实现IListSource或IEnumerable的类 - 你的意思是声明语言作为 OnlinebookCollection 而不是?或者使用 l 作为数据源?


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

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

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)
        {
        }

    }
}

解决方案

Look at your code:

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?


这篇关于DataList1使用了无效的数据源。有效的数据源必须实现IListSource或IEnumerable。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆