Click here to Skip to main content
15,906,094 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a textbox on a asp.net screen. i am asking user to enter date that must be today or greater than today. please let me know how to do.

What I have tried:

SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=.\\sqlexpress;Initial Catalog=college_education;User ID=sa;Password=system;";
        con.Open();
        string q = "update cart set status='CO',Order_Date='" + TextBox7.Text + "',Order_Due_Date='" + TextBox1.Text + "' Where user_name='" + Session["user"] + "' and status='ATC'";
        
        SqlCommand com = new SqlCommand(q,con);
        com.ExecuteNonQuery();
        {
            Response.Write("<script>alert('Your Order is Placed Successfully. Our Agent will contact you shortly.')</script>");
        }
Posted
Updated 22-Oct-17 21:52pm

1 solution

For starters, don't do databases 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. Use Parametrized 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?

Checking dates is fairly easy: use DateTime.TryParse to convert the entered string into a DateTime value, then if it is valid, compare it against the current date:
C#
DateTime dt;
if (!DateTime.TryParse(TextBox1.text, out dt))
   {
   /// Report bad date format to user...
   return;
   }
if (dt >= DateTime.Now.Date())
   {
   // Report bad date to user...
   return;
   }
Then send the DateTime value to SQL via a parameterized query.
 
Share this answer
 
Comments
CPallini 23-Oct-17 4:08am    
5.
Richard Deeming 24-Oct-17 13:37pm    
Do you ever get the feeling that you're banging your head against a brick wall? We've been telling him about SQLi since January[^], but he's just not listening.
OriginalGriff 24-Oct-17 13:59pm    
But it feels so good when you stop...

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