I am in bad situation. I have a program that it works very well in my computer but when I upload it on the host it gives me this error "String was not recognized as a valid DateTime." my program is on ASP.net C# visual studio 2015
Edit: both date's format on my computer and on the host are the same. like this:"mm/dd/yyyy"
would you please help me? here is my code: HTML:
<%@ Page Title="" Language="C#" MasterPageFile="~/mainMembers/frameMembers.Master" AutoEventWireup="true" CodeBehind="GMList.aspx.cs" Inherits="Federation.mainMembers.GMList" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainFrame" runat="server">
<form id="gml" runat="server" method="post">
<asp:Label ID="titr" runat="server" CssClass="shekaste" Text="title"></asp:Label>
<br />
<br />
<div runat="server" style="height: 400px; width:800px; overflow: scroll">
<asp:GridView ID="fedList" runat="server" AutoGenerateColumns="false" CssClass="parastoo" OnRowDataBound="MatchList_RowDataBound" >
<Columns>
<asp:BoundField HeaderText="a" DataField="birthday" />
<asp:BoundField HeaderText="b" DataField="start_sport" />
<asp:BoundField HeaderText="c" DataField="ToDate" />
</Columns>
</asp:GridView>
</div>
</form>
</asp:Content>
C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Globalization;
using System.Drawing;
using System.Text.RegularExpressions;
using System.IO;
namespace Federation.mainMembers
{
public partial class GMList : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NSG"].ConnectionString);
//SqlConnection con = new SqlConnection(conStr);
DataSet ds = new DataSet();
//private string strSQL;
private SqlCommand com;
SqlDataAdapter sda;
// DateTime dtime;
DataTable dt = new DataTable();
//DataTable dt1 = new DataTable();
PersianCalendar pc = new PersianCalendar();
//string memberID = "1111111112";
string matchID = "";
Boolean[] age2 = new Boolean[6];
Boolean[] weight = new Boolean[9];
//Int16 morshedi = 0;
byte[] img1 = new byte[0];
Int16 level;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["FID"] != null)
{
matchID = this.Session["FID"].ToString();
//memberID = this.Session["VID"].ToString();
//morshedi = Convert.ToInt16(this.Session["VMID"].ToString());
//string s;
if (!Page.IsPostBack)
{
//********************************************************
dt.Rows.Clear();
con.Close();
sda = new SqlDataAdapter("select birthday,start_sport,ToDate from general_members ", con);
con.Close();
con.Open();
dt.Rows.Clear();
sda.Fill(dt);
con.Close();
//string dds=dt.Rows[0]["ToDate"].ToString();
DataView dv1 = new DataView(dt);
fedList.DataSource = dv1;
fedList.DataBind();
}
}
else
{
Response.Redirect("~/a.aspx");
}
}
protected void MatchList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Find the value in the c04_oprogrs column. You'll have to use
// some trial and error here to find the right control. The line
// below may provide the desired value but I'm not entirely sure.
TableCell statusCell1 = e.Row.Cells[0];
TableCell statusCell2 = e.Row.Cells[1];
TableCell statusCell3 = e.Row.Cells[2];
string dstr = "";
int year, month, day;
year = Convert.ToInt32(pc.GetYear(Convert.ToDateTime(statusCell1.Text)));
month = Convert.ToInt32(pc.GetMonth(Convert.ToDateTime(statusCell1.Text)));
day = Convert.ToInt32(pc.GetDayOfMonth(Convert.ToDateTime(statusCell1.Text)));
dstr = year.ToString() + "/" + month.ToString() + "/" + day.ToString();
statusCell1.Text = dstr;
year = Convert.ToInt32(pc.GetYear(Convert.ToDateTime(statusCell2.Text)));
month = Convert.ToInt32(pc.GetMonth(Convert.ToDateTime(statusCell2.Text)));
day = Convert.ToInt32(pc.GetDayOfMonth(Convert.ToDateTime(statusCell2.Text)));
dstr = year.ToString() + "/" + month.ToString() + "/" + day.ToString();
statusCell2.Text = dstr;
year = Convert.ToInt32(pc.GetYear(Convert.ToDateTime(statusCell3.Text)));
month = Convert.ToInt32(pc.GetMonth(Convert.ToDateTime(statusCell3.Text)));
day = Convert.ToInt32(pc.GetDayOfMonth(Convert.ToDateTime(statusCell3.Text)));
dstr = year.ToString() + "/" + month.ToString() + "/" + day.ToString();
statusCell3.Text = dstr;
}
}
}
}
The problem is very likely different regional settings on your host.
To address this ensure you pass the expected CultureInfo to your conversion method:
https://msdn.microsoft.com/en-us/library/9xk1h71t(v=vs.110).aspx
See below, applied to your code. You will need to substitute the relevant culture string in the CultureInfo constructor:
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
year = Convert.ToInt32(pc.GetYear(Convert.ToDateTime(statusCell1.Text,culture)));
month = Convert.ToInt32(pc.GetMonth(Convert.ToDateTime(statusCell1.Text,culture)));
day = Convert.ToInt32(pc.GetDayOfMonth(Convert.ToDateTime(statusCell1.Text,culture)));