Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,
I have c# project and and database to store information in it.
the problem is :

C#
string query = "UPDATE `accounting_security` SET `Email`='" + email + "', `UniqueID`='" + unID + "', `SubmitDate`='" + DateTime.Now + "', `Checked`='1' WHERE `Key`='" + key + "'";

//create mysql command
MySqlCommand cmd = new MySqlCommand();
//Assign the query using CommandText
cmd.CommandText = query;
//Assign the connection using Connection
cmd.Connection = connection;

//Execute query
cmd.ExecuteNonQuery();


the query executes successfully but datetime column doesn't save the DateTime.Now correctly
it save like this : 0000-00-00 00:00:00
what is the problem and how can I solve it?
Posted
Comments
Richard Deeming 28-May-15 10:05am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

1 solution

As Richard says, use Parameterized queries at all times - there is even a good chance that that will cure your problem at the same time.

If it doesn't, then look closely at your table definition: verify that the column you are trying to change is indeed called SubmitDate, and that a row with the appropriate key value exists. If it doesn't exactly match, then no rows will be updated, and that may create the impression that the value is being overwritten with zeros.
If it all looks good, then manually change the SubmitDate value to sometime next year and run your code again. Is the value changed to zeros in the DB? (Don't look via your code, use an external tool) If not, then the update isn't happening.
It's also worth a look at the code that you are using to detect that it's a zero - it may be that you are reading it wrong instead of writing it badly.

At the moment, we can't say "do this and it'll fix it" - you need to gather some information around the problem first.
But change all your code to use parameterized queries first - that's just too much of a risk to ignore.
 
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