尝试在'while'和'if'条件下使用转发器但读取器不工作 [英] Trying to use a repeater within a 'while' and 'if' condition but reader not working

查看:64
本文介绍了尝试在'while'和'if'条件下使用转发器但读取器不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让转发器从if条件中显示我的数据,但由于某种原因它不起作用。每当我调试我的代码时,它都会在if条件中命中,因为它是真的并完美地完成了我的代码,但结果是错误的。任何帮助将不胜感激。



谢谢



我尝试过:



I'm trying to get the repeater to display my data from inside the if condition but for some reason it won't work. Whenever I debug my code it hits inside the if condition because it's true and goes through my code perfectly but the results are wrong. Any help would be appreciated.

Thanks

What I have tried:

private void LoadSchedule()
    {
        string connString = ConfigurationManager.ConnectionStrings["DatabaseConnection"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            using (SqlCommand comm = new SqlCommand("GetSchedule", conn))
            {
                using (SqlDataReader reader = comm.ExecuteReader())
                {

                    while (reader.Read())
                    {
                        DateTime localDate = DateTime.Now();
                        DateTime PoolStatusActive = Convert.ToDateTime(reader["PoolActive"]);
                        DateTime PoolStatusDisabled = Convert.ToDateTime(reader["PoolDisabled"]);

                        if (localDate > PoolStatusActive && localDate < PoolStatusDisabled)
                        {
                            myRepeater.DataSource = reader;
                            myRepeater.DataBind();
                            MessageBox.Show("True");
                        }

                        else
                        {
                            MessageBox.Show("False");
                        }
                    }
                }
            }
        }
    }

推荐答案

阅读上次询问时我说的话:多个读者只能打开一次连接 [ ^ ]
Read what I said when you asked this last time: Sql open connection just once for multiple readers[^]


while (reader.Read())

继续执行,直到没有更多的记录要读。

Keeps executing until there are no more records to read.

myRepeater.DataSource = reader;
myRepeater.DataBind();

告诉转发器读取所有剩余记录,并显示它们。不会对这些记录应用过滤器。当 DataBind()返回时,没有更多的记录要读。



如果你想过滤结果您正在转发器中显示,然后您需要筛选数据源。例如:

Tells the repeater to read all remaining records, and display them. No filter will be applied to those records. When DataBind() returns, there are no more records to read.

If you want to filter the results that you're displaying in the repeater, then you need to filter the data source. For example:

private void LoadSchedule()
{
    string connString = ConfigurationManager.ConnectionStrings["DatabaseConnection"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(connString))
    using (SqlCommand comm = new SqlCommand("GetSchedule", conn))
    {
        SqlDataAdapter adapter = new SqlDataAdapter(comm);
        DataTable table = new DataTable();
        adapter.Fill(table);
        
        DateTime localDate = DateTime.Now;
        
        myRepeater.DataSource = table.AsEnumerable()
            .Where(r => r.Field<DateTime>("PoolActive") < localDate)
            .Where(r => r.Field<DateTime>("PoolDisabled") > localDate);
        
        myRepeater.DataBind(); 
    }
}


这篇关于尝试在'while'和'if'条件下使用转发器但读取器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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