Click here to Skip to main content
15,888,270 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
Could you please help me to get a solution for my issue. I am using a SQL Server database and c# i have a gym and i want to chechin my login client with his offer.

offers; 1- by month . 2- by count of days.

my offer table;

| ID | startdate |  month |  day  |  enddate  |      type        |<br />
| 1  | 2019-03-05|   3    | null  |2019-06-05 |Business Calendar |<br />
| 2  | 2019-03-05|  null  |   30  |2019-04-04 |Day Calendar      |


What I have tried:

C#
try
            {



                string che = @"(select count(*)from table where id='" + ID.Text + "' and startdate <='" + DateTime.Now + "' and endDate >='" + DateTime.Now + "' )";
                con.Open();
                SqlCommand sda = new SqlCommand(che, con);
                int count = (int)sda.ExecuteScalar();
                if (count > 0)
                {
                    using (SqlCommand com = new SqlCommand("INSERT INTO [checkin] (ID,time,username) VALUES (@ID,@time,@username)", con))
                    {
                        com.Parameters.AddWithValue("@ID", ID.Text);

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

                        com.Parameters.AddWithValue("@username", txtusername.Text);
                        com.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();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }


what i'm looking for is add something to this code if it day calendar count days not check the end date to expired the ID
Posted
Updated 16-Mar-19 21:46pm

1 solution

Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
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