Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am facing incorrect date value while insert the data..



Mysql
----------------------

Column DataType

Date ---- date


Please help me to resolve this issue

What I have tried:

[HttpPost]
       public JsonResult POSTDetails(POSTSubmitModel personModel)
       {

           SubmitModel person = new SubmitModel();
           try
           {
               //This is my connection string i have assigned the database file address path
               string MyConnection2 = "datasource=mysql-64923-0.cloudclusters.net;port=11378;username=admin;password=XXXXXXX";
               //This is my insert query in which i am taking input from the user through windows forms
               string Query = "insert into Vellammal.ProfitSubmitDetails(Date,ProfitValues,SaleCategory,Comment,Expense) values('" + DateTime.Now + "','" + personModel.ProfitValues + "','" + personModel.SaleCategory + "','" + personModel.Comment + "','" + personModel.Expense + "');";
               //This is  MySqlConnection here i have created the object and pass my connection string.
               MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
               //This is command class which will handle the query and connection object.
               MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
               MySqlDataReader MyReader2;
               MyConn2.Open();
               MyReader2 = MyCommand2.ExecuteReader();     // Here our query will be executed and data saved into the database.

               while (MyReader2.Read())
               {
               }
               MyConn2.Close();
               sendMail(personModel.ProfitValues, personModel.SaleCategory,"");
               return Json(person);
           }
           catch (Exception ex)
           {
               return Json(person);
           }


       }
Posted
Updated 16-Jan-22 0:25am

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?

Fix that through your whole application, and the problem you have noticed will probably disappear.
 
Share this answer
 
//if your date format in db is like MM/dd/yyyy do:

string Query = "insert into Vellammal.ProfitSubmitDetails(Date,ProfitValues,SaleCategory,Comment,Expense) 
values('" + DateTime.Now.ToString("MM/dd/yyyy") + "','" + personModel.ProfitValues + "','" + personModel.SaleCategory + "','" + personModel.Comment + "','" + personModel.Expense + "');";

//change format accordinlgy to your date format in DB!
 
Share this answer
 
Comments
Richard MacCutchan 16-Jan-22 7:18am    
Dates should never be stored as strings. See also OriginalGriff's comments above.
M Imran Ansari 16-Jan-22 8:23am    
In this scenario, we are not storing dates into string, we are just parsing datetime into correct format while creating the query.
Richard MacCutchan 16-Jan-22 9:03am    
So what do you think this means:
DateTime.Now.ToString("MM/dd/yyyy")

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