Reputation: 17
I have a form that inserts a date..
Working locally, when I submit the form it works terrifically and saves the date to the server.
But when I try doing this from the server, I get an error:
String was not recognized as a valid DateTime.System.Collections.ListDictionaryInternal
Code:
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);
}
}
}
Upvotes: 1
Views: 3437
Reputation: 161
try this
DateTime curr = Convert.ToDateTime(txtDate.Text);
d.ArrivalDate = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(curr, "India Standard Time");
or
d.ArrivalDate = Convert.ToDateTime(txtDate.Text);
Upvotes: 0
Reputation: 1063
.NET will set your locale to whatever is default on the machine it's running. That means it will also parse DateTime and ToString() DateTime in that locale. So my guess is your local language/timezone preferences differ from the server?
You can set them in web.config to expect and output something specific:
<system.web>
<globalization culture="da-DK" uiCulture="da-DK" />
</system.web>
Mine is danish, maybe you want en-GB or en-US ?
Second option is to shoot fish in a barrel with an invariant formatprovider.
DateTime.Parse(txtDate.Text, DateTimeFormatInfo.InvariantInfo);
Hope that helps!
Upvotes: 3