Click here to Skip to main content
15,909,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C#
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");
                        }
                    }
                }
            }
        }
    }
Posted
Updated 12-Feb-19 5:24am
v2
Comments
Richard Deeming 12-Feb-19 11:16am    
MessageBox.Show("True");

Same problem as your previous question: this will not work in a web application.

Read what I said when you asked this last time: Sql open connection just once for multiple readers[^]
 
Share this answer
 
Comments
Commish13 10-Feb-19 15:44pm    
I appreciate your help but I still don't know how to get the loop and the if statement to work together.
Commish13 10-Feb-19 16:31pm    
How can I get someone else to look at this question?
Maciej Los 12-Feb-19 12:04pm    
Agree!
while (reader.Read())
Keeps executing until there are no more records to read.
myRepeater.DataSource = reader;
myRepeater.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:
C#
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(); 
    }
}
 
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