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

C#
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

What I have tried:

C#
<pre>var goodForVisit = false;
        int visitedCount;
        int offerDayCount;
        var endDate = DateTime.MinValue;
        DateTime startDate = DateTime.MinValue;
        using (SqlConnection con = new SqlConnection("Data Source=SQL5037.site4now.net;Initial Catalog=DB_A448D1_Dragon;User Id=**********;Password=************"))
        {
            con.Open();
            //try
            //{
                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 = 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["month"] == null && offer != null)
                            {
                                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)"))
                            {
                                com2.Parameters.AddWithValue("@STARTDATE", startDate);
                                com2.Parameters.AddWithValue("@ENDDATE", endDate);

                                using (SqlDataReader reader2 = com2.ExecuteReader())
                                {
                                    if (reader2.Read())
                                    {
                                        visitedCount = (int)reader2["count"];
                                        if (offer != null && visitedCount < offerDayCount)
                                            goodForVisit = true;

                                        if (offer != null && 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);
                }
Posted
Updated 19-Mar-19 10:25am
v2
Comments
Richard MacCutchan 19-Mar-19 10:45am    
Just a suggestion ...
If the enrolment is for a month, then set maxvisits to -1 and check the end date.
If the enrolment is for a day, then set maxvisits to 5 and ignore the end date.
Maciej Los 19-Mar-19 15:26pm    
Sounds like a solution ;)

Not a direct solution, but general advice...

I'd use stored procedure[^] for such of requirement. There's a lot of benefits of using stored procedures (SP)...

How to write such of stored procedure. You've got 2 ideas:
1) Richards' MacCutchan - in the comment to the question
2) Gerry's Schmitz - in the solution #1.

I'd also improve your offer table:
UserID | Startdate | Periodtype | Periodvalue |  enddate  |          offer       |
  1    | 20-3-2019 |   'M'      |     3       | 20-6-2019 |  ( summer ) monthly  |
  2    | 20-3-2019 |   'D'      |     5       | 20-3-2019 |  ( student )  daily  |


Then the ody of SP, which returns true/false might look like:
SQL
DECLARE @limitOfVisits INT = 0;
DECLARE @isValidOffer BIT = 0; --or BOOLEAN

--check how many times user can access to gym
SELECT @limitOfVisits = CASE
    WHEN Periodtype = 'M' AND Stardate>=@startdate AND enddate <=@enddate THEN PeriodValue
    WHEN Periodtype = 'D' AND Stardate>=@startdate THEN PeriodValue
   ELSE 0 END
FROM offertable
WHERE UserID=@UserID;

--check if limit of entries has not been exceeded
SELECT @isValidOffer = COUNT(*)<@limitOfVisits
FROM [checkin]
WHERE UserID = @UserID AND Stardate>=@startdate AND enddate <=@enddate;

RETURN @isValidOffer;


As you can see, this is an implementation of other members ideas.

For further details, please see:
CREATE PROCEDURE (Transact-SQL) - SQL Server | Microsoft Docs[^]
How to create a SQL Server stored procedure with parameters[^]

Good luck!
 
Share this answer
 
Comments
el_tot93 20-Mar-19 1:56am    
i need help with my code in c#
Maciej Los 20-Mar-19 17:01pm    
Wow! Really? And you want to ignore good advice?
("Checkin table" only requires ID; username is redundant and problematic.)

1) Create a method called "HasValidOffer( int id )"
2) Call HasValidOffer( int id ) with id of "returning" customer (which implies a "customer master" in addition to what you've shown). In HasValidOffer():

- Check if client has offer; if not, return false (no valid offer).
- If offer expired, return false.
- If offer is daily, count visits during period; if visits < max, return true, else false.

3) So, if HasValidOffer() returns true, has valid offer (and therefore admit), else no valid offer in effect.
 
Share this answer
 
Comments
Maciej Los 19-Mar-19 12:10pm    
5ed!
Your solution is very similar to Richard's solution provided in a comment ;)
el_tot93 20-Mar-19 1:55am    
help me with my code plz

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