I am inserting data into MySql
from C# code in Windows Forms
application. This work fine on my development machine. when I move the code to other machine to test I get the error of
string was not recognized as valid datetime
I have tried with every solution TryParse
, ParseExact
, TryParseExact
even Convert function. in last I tried to done with direct in query to skip further problems.
string dd = row["TIMESTAMP1"].ToString(); // 14-Nov-2018 02:25:00 AM
DateTime dt1 = DateTime.ParseExact(dd, "dd-MMM-yy h:mm:ss tt", null);
//string date2 = dt1.ToString("yyyy-MM-dd hh:mm:ss");
string y = dt1.Year.ToString();
string m = dt1.Month.ToString();
string d = dt1.Day.ToString();
string h = dt1.Hour.ToString();
string i = dt1.Minute.ToString();
string s = dt1.Second.ToString();
string dte = y + "," + m + "," + d + "," + h + "," + i + "," + s;
//DateTime.TryParseExact(date2, "yyyy-MM-dd hh:mm:ss", new CultureInfo("en-US"), DateTimeStyles.None, out dateDb);
//DateTime date3 = DateTime.Parse(date2, "yyyy-MM-dd hh:mm:ss");
string query = "INSERT INTO transaction (transid, ticketid, storeid, TIMESTAMP, transtypeid, total) Values ('" + row["transid"] + "','" + row["ticketid"] + "','" + Convert.ToInt32(row["storeid"]) + "',STR_TO_DATE('"+dte+ "', '%Y,%m,%d,%h,%i,%s'),'" + Convert.ToInt32(row["transtypeid"]) + "','" + Convert.ToDecimal(row["total"]) + "')";
How do I make this possible to work on other machines without exceptions. It works fine on development machine.
Update
tried with parameters too
cmd.Parameters.Add(new MySqlParameter("@date", date2)).MySqlDbType = MySqlDbType.DateTime;