 |
|
C# String to DateTime ช่วยหาวิธีแปลงเอาของมูลลง Table ที |
|
 |
|
|
 |
 |
|
DateTime.Parse("2008-05-01T07:34:42-5:00").ToString("yyyy-MM-dd") ลองดูอันนี้นนะครับ
|
 |
 |
 |
 |
Date :
2012-05-31 22:24:14 |
By :
pStudio |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
DateTime.Parse("2008-05-01T07:34:42-5:00").ToString("yyyy-MM-dd") ลองดูอันนี้นนะครับ
เเล้วเวลาจะเป็นปัจจุบันไหม เอ่ย?
|
 |
 |
 |
 |
Date :
2012-06-01 09:35:13 |
By :
cutsumoto |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
น่าจะเหมือกับ กระทู้นี้นะครับ Click
|
 |
 |
 |
 |
Date :
2012-06-01 09:42:30 |
By :
kanchen |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำไมต้องแปลงเป็น string ก่อนเอาลง table ด้วยล่ะ
เอา datetime ยัดลงไปเลยไม่ได้เหรอ ไม่ต้องจัด format ด้วย
เพราะยังไงก็เก็บลงเป็น type datetime อยู่แล้ว
แปลงไปแปลงมาทำไม เสียเวลา แปลงแล้วก็ชอบ error อีก
ปัญหาของ จขกท เกิดจากการ format datetime ของ app กับ ui ไม่ตรงกัน
ถ้าแปลงเป็น datetime ได้ก็ไม่มีปัญหาแล้ว database จะเก็บ format แบบไหนก็ชั่งมัน
ซึ่งโดยปกติ ข้าพเจ้าจะใช้ Convert.ToDatetime("1/6/2555", System.Globalization.CultureInfo("th_TH"))
ในโค้ดจะบอกมันว่า เราใช้ string format อะไร อันไหนเป็ยวัน เป็นเดือน เป็นปี พ.ศ. หรือ ค.ศ. เพื่อที่มันจะได้แปลงเป็น
datatime ได้ถูกต้อง
พอแปลงได้ก็หมดปัญหาแล้ว สามารถนำไปใช้แบบไหนก็ได้ จะแสดงผลเป็น format ไหนก็ได้แล้ว
ส่วนจะเอาลง table แนะว่าควรใช้แบบ paramaters แทนการส่งไปแบบ string
|
 |
 |
 |
 |
Date :
2012-06-01 13:37:48 |
By :
พี่หมี |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมใส่ datetime.now.tostring text box ให้มันโชวเวลาปัจจุบันไว้ เเล้ว จะทำ การ insert text box นั้นเข้าไปในตราราง ที่มี Data Type เป็น DateTime
ลอง Convert.ToDatetime("1/6/2555", System.Globalization.CultureInfo("th_TH")) เเล้ว มัน ขึ้น Input string was not in a correct format. ตลอด ลองหาวิธีต่างๆมาใช้ ก็ไม่ได้สักที
|
 |
 |
 |
 |
Date :
2012-06-02 13:49:49 |
By :
cutsumoto |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
usecarTableAdapter use = new usecarTableAdapter();
use.Insert(txtuseid.Text, txtidcars.Text, Convert.ToDateTime(txtusedate.Text).ToShortDateString(), txttimestart.Text.ToString(), txttimeend.Text.ToString(), Convert.ToDecimal(txtmitstart.Text), Convert.ToDecimal(txtmitend.Text), Convert.ToDecimal(lbltotalmon.Text), "ปกต");
Code
Input string was not in a correct format.
Server Error in '/asdf' Application.
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
Line 172:
Line 173: usecarTableAdapter use = new usecarTableAdapter();
Line 174: use.Insert(txtuseid.Text, txtidcars.Text, Convert.ToDateTime(txtusedate.Text).ToShortDateString(), txttimestart.Text.ToString(), txttimeend.Text.ToString(), Convert.ToDecimal(txtmitstart.Text), Convert.ToDecimal(txtmitend.Text), Convert.ToDecimal(lbltotalmon.Text), "ปกต");
Line 175:
Line 176:
Source File: d:\asdf\inusecar.aspx.cs Line: 174
Stack Trace:
[FormatException: Input string was not in a correct format.]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +7471623
System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt) +146
System.Convert.ToDecimal(String value) +83
inusecar.btnsub_Click(Object sender, EventArgs e) in d:\asdf\inusecar.aspx.cs:174
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
ไม่รู้ว่ามันเกิดจากตรงไหน
|
 |
 |
 |
 |
Date :
2012-06-03 13:35:17 |
By :
cutsumoto |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|