I have an asp.net website which when i put it up on my college server gave me an error when i retireved the inner text of an xml element and converted it to date which should be selected on the calender control.
The error i recieve is-
String was not recognized as a valid DateTime.
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: String was not recognized as a valid DateTime.
Source Error:
Line 91: displayEvent.Text += "<b>On-</b>" + node.SelectSingleNode("date").InnerText + "<br />";
Line 92: displayEvent.Text += "<b>Contact Number-</b>" + node.SelectSingleNode("phone").InnerText + "<br />";
Line 93: DateTime dts1 = Convert.ToDateTime(node.SelectSingleNode("date").InnerText);
Line 94: Calendar1.SelectedDate = dts1;
Line 95: latitude = node.SelectSingleNode("latitude").InnerText;
The xml file looks like-
<root1><data><event_name>Christmas Party</event_name><event_desc>The annual christmas bash is happening as planned. This year there is bound to be more excitement.</event_desc><date>12/25/2011</date><phone>111-111-1111</phone><latitude>43.700573</latitude><longitude>-79.296661</longitude></data><data><event_name>New Year Party</event_name><event_desc>Ring in the new year with us, the party is going to be a never before event with a huge celebrity guest line up.</event_desc><date>12/31/2011</date><phone>222-222-2222</phone><latitude>43.728572</latitude><longitude>-79.48669</longitude></data><data><event_name>Jt Birthday</event_name><event_desc>It's jasmeet's birthday. He wanted to get a samsung nexus s, so i should try and buy that for him.</event_desc><date>12/11/2011</date><phone>333-333-3333</phone><latitude>45.515849</latitude><longitude>-73.553417</longitude></data></root1>
Also i am using the latitude and longitude for showing them on a map. It also has problem converting the inner texts of these elements to double
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 75: {
Line 76: GMap1.reset();
Line 77: GMap1.addGMarker(new GMarker(new GLatLng(Convert.ToDouble(latitude),Convert.ToDouble(longitude))));
Line 78: GMap1.setCenter(new GLatLng(Convert.ToDouble(latitude), Convert.ToDouble(longitude)), 6);
Line 79: displayEvent.Text = " ";
The thing that i am unable to understand is that all of this working absolutely fine on my computer. when i test it on the local server it works fine but on my college server it is giving these errors. Can anybody guide me as to what am i doing wrong here. Other parts of the website are reading from other xml files and they dont throw any exceptions. This is the latest addition of xml reading to the website, everything else works fine.
The method which is used for reading the xml file and conversion-
protected void okButton_Click(object sender, EventArgs e)
{
GMap1.reset();
GMap1.addGMarker(new GMarker(new GLatLng(Convert.ToDouble(latitude),Convert.ToDouble(longitude))));
GMap1.setCenter(new GLatLng(Convert.ToDouble(latitude), Convert.ToDouble(longitude)), 6);
displayEvent.Text = " ";
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("Xml/try.xml"));
XmlNodeList nodeList = doc.SelectNodes("root1/data");
foreach (XmlNode node in nodeList)
{
if (node.SelectSingleNode("event_name").InnerText.Equals(DropDownList1.SelectedValue))
{
latitude = "";
longitude = "";
displayEvent.Text += "<b>Event name-</b>" + node.SelectSingleNode("event_name").InnerText + "<br />";
displayEvent.Text += "<b>Description-</b>" + node.SelectSingleNode("event_desc").InnerText + "<br />";
displayEvent.Text += "<b>On-</b>" + node.SelectSingleNode("date").InnerText + "<br />";
displayEvent.Text += "<b>Contact Number-</b>" + node.SelectSingleNode("phone").InnerText + "<br />";
Calendar1.SelectedDate = Convert.ToDateTime(node.SelectSingleNode("date").InnerText);
latitude = node.SelectSingleNode("latitude").InnerText;
longitude = node.SelectSingleNode("longitude").InnerText;
}
}
}
The two computers have different regional settings. You are converting string “12/25/2011” to a DateTime value. If in Control Panel/Regional Settings short date format is dd/MM/yyyy, then 25 is interpreted as month number and the string is considered invalid, since we only have twelve. As for longitude/latitude values, my guess would be that decimal separator is set to comma on your college server. Consider using the versions of Convert.ToDateTime/ToDouble with the second IFromatProvider parameter.