C# for loop: Checking if DateTimePicker is weekend or in database

VAer 756 Reputation points
2021-01-12T23:56:28.62+00:00

I am not an IT professional, so below syntax is wrong, but it shows what I would like to do.

I have two DateTimePicker: DateTimePicker1 and DateTimePicker2 (DateTimePicker1 < DateTimePicker2). For any days between these two DateTimePickers, I would like to check:

  1. If it is weekend.
  2. If it is a date in Database table.

Could someone help me clean up below code?

Thank you very much!

public static bool isDateInDatabaseAppointmentTable(DateTime DateTimePicker.Value)  //How to write this statement. Check if the date is in database table
{

    OdbcConnection Cn = new OdbcConnection(GlobalVariables.DatabaseConnectionString);


    string select = "SELECT COUNT(*) from TableAppointment WHERE AppointmentDate = DataTimePicker.Value ";
    //How to write this SQL statement

    using (OdbcCommand cmd = new OdbcCommand(select, Cn))            
    {
        object obj = cmd.ExecuteScalar();

        int count = Convert.ToInt32(obj);

        if (count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }                          

    }


}



for (DateTime dt = DateTimePicker1.Value to DateTimePicker2.Value) 
{
    bool isFound = GlobalMethod.isDateInDatabaseAppointmentTable(dt);

    if (dt == Satursday || dt == Sunday)
    {
    MessageBox.Show("It is weekend, you don't work today")
    //I will do something here, and I think I know how to do it. Just using messagebox to replace it.
    }  
    else if (isFound == true)   
    {
    MessageBox.Show("You have appointment today, and you don't work today")
    //I will do something here, and I think I know how to do it. Just using messagebox to replace it.
    }  
    else
    {
    //I will do something here.
    }


}
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,828 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,237 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,611 Reputation points
    2021-01-15T06:22:12.07+00:00

    Hi VAer-4038,
    Based on your code, there are some problems you need to note.
    First, you need to declare the "isDateInDatabaseAppointmentTable" method with formal parameters instead of actual parameters.
    Then you can't use to operator in For loop.
    Finally, you can check day of week with DateTime.DayOfWeek that cheong00 said.
    Here is the modified code you can refer to.

    public static bool isDateInDatabaseAppointmentTable(DateTime dt)   
    {  
       ....  
    }  
      
    private void button1_Click(object sender, EventArgs e)  
    {  
        var startDate = dateTimePicker1.Value;  
        var endDate = dateTimePicker2.Value;  
        for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))  
        {  
            bool isFound = isDateInDatabaseAppointmentTable(date);  
      
            if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)  
            {  
                MessageBox.Show("It is weekend, you don't work today");  
            }  
            else if (isFound == true)  
            {  
                MessageBox.Show("You have appointment today, and you don't work today");  
      
            }  
            else  
            {  
            }  
        }  
    }  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Cheong00 3,471 Reputation points
    2021-01-13T03:56:59.537+00:00

    You can check day of week with DateTime.DayOfWeek.

    And here's how you add parameter to ODBC query. You need to add parameter with OdbcType.DateTime and assign your DataTimePicker.Value to parameter.Value.

    Alternatively just call cmd.Parameters.AddWithValue("@AppointmentDate", DataTimePicker.Value) and let the runtime guess the parameter type for you.

    0 comments No comments