Click here to Skip to main content
15,923,197 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a website use ASP.NET, C#, SQL2005

datetime in my local machine is format "dd/MM/yyyy"

I insert datetime when i run application is ok.

But when i publish it and run in localhost (IIS v5.1):
1. DayTo = "11/05/2013" DateTime.Now= "31/05/2013" -> OK
2. DayTo = "13/05/2013" DateTime.Now= "31/05/2013" -> String was not recognized as a valid DateTime.

I need your help! Thanks.

my insert code

C#
string insert1 = "insert into tbHangNhap(DayTo,TimeNow,TenDangNhap)values(@day1,@timenow,@user)";
                   object[] pa1 = {"day1",DateTime.Parse(txtTimeTO.Text),"@timenow",DateTime.Now,"@user",txtuser.tex};



p/s: sorry, My English is to bad.
Posted
Updated 7-May-13 1:15am
v2

C#
string insert1 = "insert into tbHangNhap(DayTo,TimeNow,TenDangNhap)values(@day1,@timenow,@user)";
                   object[] pa1 = {"day1",DateTime.Parse(txtTimeTO.Text).ToString("MM/dd/yyyy"),"@timenow",DateTime.Now.ToString("MM/dd/yyyy");,"@user",txtuser.tex};
 
Share this answer
 
v3
Comments
Jessica Phạm 7-May-13 21:13pm    
Thanks you! I try it but not OK. My problem is "DayTo", I run in application is OK all, no problem. But when I publish it and run in my localhost ( IIS) at the same machine, it can't insert or update day >12. I think have wrong format, but I change day in my localhost by 31/05/2013, and test TimeNow still insert in my database is 31/05/2013 12:00:00 AM ok.
Check this line

object[] pa1 = {"day1",DateTime.Parse(txtTimeTO.Text),"@timenow",DateTime.Now,"@user",txtuser.tex};

-> you have provided parameter "day1" instead of "@day1";

-> You converting txtTimeTO.Text to DateTime , try DateTime.Parse(txtTimeTO.Text+" 12:00:00 AM")

DateTime should be in format 31/05/2013 12:00:00 AM.
As you are providing only date it is throwing error.
 
Share this answer
 
Comments
Jessica Phạm 7-May-13 21:34pm    
Thanks you,
-> you have provided parameter "day1" instead of "@day1"; -> yes, OK
I fix it. But my application run is OK in all case, but when I publish it and run in my IIS at the same machine, case "13/05/2013" not OK. I all format in my code and machine is "dd/MM/yyyy".
Thanks you!
C#
string replace_day1= your desired date.ToString("dd/MM/yyyy");
string replace_timenow=your desired date.ToString("dd/MM/yyyy");

C#
string insert1 = "insert into tbHangNhap(DayTo,TimeNow,TenDangNhap)values(@day1,@timenow,@user)";
Sqlcommand cmd=new Sqlcommand(insert1,your connection);
cmd.Parameters.Add(new SqlParameter("@day1", replace_day1));
cmd.Parameters.Add(new SqlParameter("@timenow",replace_timenow));
cmd.executenonquery();


Another Alternative:

C#
string replace_day1= your desired date.ToString("dd/MM/yyyy");
string replace_timenow=your desired date.ToString("dd/MM/yyyy");


C#
string insert1 = "insert into tbHangNhap(DayTo,TimeNow,TenDangNhap)values(@day1,@timenow,@user)";
object[] pa1 = {"@day1",replace_day1,"@timenow",replace_timenow,"@user",txtuser.tex};
 
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