2014-07-27 124 views
0

在本地,但沒有工作,我有一個插入日期的形式..日期時間在服務器上

在本地工作,當我提交表單它工作得不得了,並保存日期到服務器。

但是當我嘗試從服務器這樣做,我得到一個錯誤:

String was not recognized as a valid DateTime.System.Collections.ListDictionaryInternal

代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Globalization; 

public partial class test : System.Web.UI.Page 
{  
    protected void dog_add_submit_Click(object sender, EventArgs e) 
    { 
     /*Create and Populate Dog*/ 

     Dog d = new Dog(); 
     try 
     { 
      d.DogName = dog_add_name.Text; 
      d.ImageUrl = SaveImages(); 
      d.Colour = dog_add_colour.Text; 
      d.PlaceFrom = dog_add_placeFrom.Text; 
      d.Breed = dog_add_breed.Text; 

      d.ArrivalDate = DateTime.Parse(txtDate.Text); 

      //if fields were populated properly in c# proceed to insert into database 
      try 
      { 
       int numEffected = d.doginsert(); 
       // Response.Write("num of effected rows are " + numEffected.ToString()); 

       Response.Redirect("MedicalHistoryAdd.aspx?dogid=" + d.SqldogID); 
      } 
      catch (Exception ex) 
      { 
       Response.Write("There was an error when trying to insert the dog into  the database" + ex.Message); 
      } 
     } 
     catch (Exception ex) 
     { 
      Response.Write(ex.Message + ex.Data); 
     } 

    } 
} 
+0

的可能重複[「字符串未被識別爲有效的DateTime」中唯一的服務器,但它是在我的本地系統工作正常(HTTP: //stackoverflow.com/questions/16495751/string-was-not-recognized-as-a-valid-datetime-in-server-only-but-it-is-working) – Suji

回答

2

.NET將設置你的語言環境來無論是默認情況下它運行的計算機上。這意味着它也將在該語言環境中解析DateTime和ToString()DateTime。所以我的猜測是你的本地語言/時區偏好與服務器有所不同?

您可以將它們設置在web.config中的期望和特定的輸出的東西:

<system.web> 
    <globalization culture="da-DK" uiCulture="da-DK" /> 
</system.web> 

我的是丹麥,也許你想EN-GB或EN-US?

第二種選擇是用不變格式提供者在桶中拍攝魚。

DateTime.Parse(txtDate.Text, DateTimeFormatInfo.InvariantInfo); 

希望有幫助!

+0

工作對我很好。謝謝! – Ksuvec

0

試試這個

DateTime curr = Convert.ToDateTime(txtDate.Text); 
d.ArrivalDate = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(curr, "India Standard Time"); 

d.ArrivalDate = Convert.ToDateTime(txtDate.Text); 
相關問題