Search code examples
c#winformscheckin

find the way to check in with count


Could you please help me to get a solution to my issue. I am using a SQL Server database, it is a gymnastics program, and I want to get check in the client when he comes to the gym , I have two way of offering the first one is a monthly way , and the second is a daily, the first I don't have a problem with it and I use this code for checkin;

using (SqlCommand com = new SqlCommand("select count(*)from enddate where ID=@ID and startdate <=@C1 and endDate >=@C2", con))
                {

                    com.Parameters.AddWithValue("@ID", ID.Text);
                    com.Parameters.AddWithValue("@C1", DateTime.Now);
                    com.Parameters.AddWithValue("@C2", DateTime.Now);

                    int count = (int)com.ExecuteScalar();
                    if (count > 0)
                    {
                        using (SqlCommand com1 = new SqlCommand("INSERT INTO [checkin] (ID,time,username) VALUES (@ID,@time,@username)", con))
                        {
                            com1.Parameters.AddWithValue("@ID", ID.Text);

                            com1.Parameters.AddWithValue("@time", txttime.Text);

                            com1.Parameters.AddWithValue("@username", txtusername.Text);
                            com1.ExecuteNonQuery();
                        }
                        MetroFramework.MetroMessageBox.Show(this, "Check In Sucssesfuly ................... ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MetroFramework.MetroMessageBox.Show(this, "this ID Expired .....................", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);


                    }
                    con.Close();
                }

I want to add to this code the second condition ( the daily offer ) I have the enddate table like ;

| ID | Startdate | month | day | enddate |          offer       |
| 1  | 20-3-2019 |   3   |null |20-6-2019|( summer ) monthly    |
| 2  | 20-3-2019 | null  | 5   |20-3-2019|( student )  daily    |

in this case, the first one can come anytime for 3 months, in the second ID he can come for 5 times only.

my checkin table ;

| ID |   Time   | username |
| 1  | 21-3-2019| test     |
| 1  | 25-3-2019| test     |
| 2  | 27-3-2019| test 2   | 

I can count how many time he comes to the gym but I don't know how to add it in my code


Solution

  • I think you may want to re-think the approach of solving the issue. If I were you I would:

    1. Get the record in enddate table using ID.text, I assume this is your customer offer table. So you have the data of STARTDATE, ENDDATE, Offer and other information for this customer ID.
    2. If ENDDATE is null and Offer = dayly, ENDDATE = DATE(Datetime.Now)
    3. Count the record from checkin table using ID.text. So you have a sum of visits by using the following statement.

    SELECT COUNT(*) From checkin WHERE Time >= STARTDATE and (Time <= ENDDATE)

    1. You have the count of visits now, you can put a condition to check if the customer has used up the offer "day" of 5.

    After spending a bit of time, I tried to complete your whole logic in C#:

    var goodForVisit = false;
    int visitedCount;
    int offerDayCount;
    var endDate = DateTime.MinValue;
    DateTime startDate = DateTime.MinValue;
    
    using (SqlCommand com = new SqlCommand("select * from [enddate] where ID=@ID", con))
    {
        com.Parameters.AddWithValue("@ID", ID.Text);
        using (SqlDataReader reader = com.ExecuteReader())
        {
            if (reader.Read())
            {
                //get information from enddate table
                var offer = “”;
                if(reader[“offer”] != null)
                      offer = reader["offer"].ToString();
                if (reader[“day”] != null)
                      offerDayCount = (int)reader["day"];
                startDate = (DateTime)reader["Startdate"];
                if (reader["enddate"] != null)
                    endDate = (DateTime)reader["enddate"];
    
    
                if (reader["enddate"] == null && offer == "dayly")
                {
                    endDate = DateTime.Now.Date;
                }
    
                //count the visit from checkin table
                using (var com2 = new SqlCommand("SELECT COUNT(*) as count From checkin WHERE Time >= @STARTDATE and (Time <= @ENDDATE)"))
                {
                    com.Parameters.AddWithValue("@STARTDATE", startDate);
                    com.Parameters.AddWithValue("@ENDDATE", endDate);
    
                    using (SqlDataReader reader2 = com2.ExecuteReader())
                    {
                        if (reader2.Read())
                        {
                            visitedCount = (int)reader2["count"];
                            if (offer == "dayly" && visitedCount < offerDayCount)
                                goodForVisit = true;
    
                            if (offer == "monthly" && DateTime.Now >= startDate && DateTime.Now <= endDate)
                                goodForVisit = true;
                        }
                    }
                }
            }
        }
    }
    
    if (goodForVisit)
    {
        using (SqlCommand com1 = new SqlCommand("INSERT INTO [checkin] (ID,time,username) VALUES (@ID,@time,@username)", con))
        {
            com1.Parameters.AddWithValue("@ID", ID.Text);
    
            com1.Parameters.AddWithValue("@time", txttime.Text);
    
            com1.Parameters.AddWithValue("@username", txtusername.Text);
            com1.ExecuteNonQuery();
        }
        MetroFramework.MetroMessageBox.Show(this, "Check In Sucssesfuly ................... ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    else
    {
        MetroFramework.MetroMessageBox.Show(this, "this ID Expired .....................", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }