1
votes

I tried to enter the date of the current day with the following code:

string now = (DateTime.Today.Day + "/" + DateTime.Today.Month + "/" + DateTime.Today.Year).ToString();
string tm = (DateTime.Today.Hour + ":" + DateTime.Today.Minute).ToString();
string sql2 = string.Format("INSERT INTO Kabala2 (Nu_kabala,Ma_num,Sk,Seif_hacnasa,Seif_name,Date) VALUES('{0}','{1}','{2}','{3}','{4}','{5}')", n, Session["Ma_num"], lprice, lkod,des, now );
Dal.DoQuery(sql2);

when I run this code it shows me the error in the title :

The conversion of a varchar data type to a datetime data type resulted in an out-of-range value

How do I solve it?

------------------edit

i changed it to:string now = DateTime.Today.ToString("yyyy-MM-dd HH:mm:ss"); and it works.

now i have another problem, when i run the code below it shows me the error:Error converting data type varchar to numeric.

string now = DateTime.Today.ToString("yyyy-MM-dd HH:mm:ss");
    string tm = DateTime.Now.ToString("HH:mm:ss");
    string sql = string.Format("INSERT INTO Kabala1 (Nu_kabala,Ma_num,Date,Time,Total,Status,Name,User_n) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", n, Session["Ma_num"], now, tm, lprice, "ddffs", Session["user"], "ddffs");
    Dal.DoQuery(sql);
    string sql2 = string.Format("INSERT INTO Kabala2 (Nu_kabala,Ma_num,Sk,Seif_hacnasa,Seif_name,Date) VALUES('{0}','{1}','{2}','{3}','{4}','{5}')", n, Session["Ma_num"], lprice, lkod, des, now);
    Dal.DoQuery(sql2);
    string sql3 = string.Format("INSERT INTO Kabala3 ((Nu_kabala,Msd,Ma_num,Kind_pay,Name_pay,Date_pay,Sk,Ms_sek,Snif,Bank,Date_klita,Seif) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}')", n,"1", Session["Ma_num"],"13", "ddffds", now, lprice, this.card.Text, this.mm.Text, this.yy.Text, now, lkod);
    Dal.DoQuery(sql3);
3
Looks like your tm string is useless since you are not using them. What is your sql2 looks like exactly when you debug your code?Soner Gönül
i use 'now' at the end of the string sql2user3542927

3 Answers

2
votes

You can Use DateTime.Today.ToString("yyyy-MM-dd HH:mm:ss") //Whatever format...

or Use direct sql instead using CONVERT method. Syntax is

in your case, use the 103 format for the date

CONVERT(VARCHAR,DateTime.Today.Day + "/" + DateTime.Today.Month + "/" + DateTime.Today.Year,103)

and 108 for the time

CONVERT(VARCHAR, DateTime.Today.Hour + ":" + DateTime.Today.Minute + ":00" ,108)

whould give format of hh:mm:ss

full reference from the following w3 link: http://www.w3schools.com/sql/func_convert.asp

1
votes

The standard form for dates in MySQL is YYYY-MM-DD. I would suggest using this format. Perhaps this will create the string in the right format:

string now = (DateTime.Today.Year + "-" + DateTime.Today.Month + "-" + DateTime.Today.Year).ToString();
-1
votes

Just Change the date settings in your computer or lap. I hope you know how its is done ! If you don't...

  1. choose your calendar from right bottom.
  2. then choose change date and time settings
  3. then choose change date and time
  4. then choose change calendar settings
  5. then in date formats> change short date as either MM/dd/yyyy or dd/MM/yyyy. Choose which is right for your computer. For me it was MM/dd/yyyy.