错误仅在主机上,而不是在我的计算机上;字符串未被识别为有效的日期时间
Error just on host, not on my computer; String was not recognized as a valid DateTime
我的处境很糟糕。我有一个程序,它在我的电脑上工作得很好,但是当我把它上传到主机上时,它给了我这个错误 "String was not recognized as a valid DateTime." 我的程序在 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"
你能帮帮我吗?
这是我的代码:
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#代码:
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;
}
}
}
}
问题很可能是您主机上的不同区域设置。
要解决此问题,请确保将预期的 CultureInfo 传递给您的转换方法:
https://msdn.microsoft.com/en-us/library/9xk1h71t(v=vs.110).aspx
见下文,已应用于您的代码。您需要在 CultureInfo 构造函数中替换相关的文化字符串:
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)));
我注意到在您代码的 c#
部分,您使用了 Convert.ToDateTime(string)
重载。如果您正在使用它,那么您传递的字符串将使用您 运行 此代码所在机器的区域设置进行转换。因此,最可能的原因是您的本地机器和主机的区域设置不同。
您可以尝试两种方法:
- 将您计算机上的区域设置更改为主机上的区域设置。很可能错误也会出现在您的本地系统上。
- 尝试使用重载
Convert.ToDateTime(string, IFormatProvider)
。这将确保转换始终以相同的方式执行,无论您的本地设置如何。
如果从 Sql 返回的数据是日期时间(数据类型是日期时间),那么您可以直接在 Gridview 中为日期列设置日期格式,如下所示:
<asp:BoundField HeaderText="Birth Date" DataField="birthday" DataFormatString="{0:yyyy/MM/dd}" HtmlEncode="false" >
并从所有日期列的 RowDataBound 事件中删除以下代码
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;
我的处境很糟糕。我有一个程序,它在我的电脑上工作得很好,但是当我把它上传到主机上时,它给了我这个错误 "String was not recognized as a valid DateTime." 我的程序在 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"
你能帮帮我吗? 这是我的代码: 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#代码:
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; } } } }
问题很可能是您主机上的不同区域设置。
要解决此问题,请确保将预期的 CultureInfo 传递给您的转换方法:
https://msdn.microsoft.com/en-us/library/9xk1h71t(v=vs.110).aspx
见下文,已应用于您的代码。您需要在 CultureInfo 构造函数中替换相关的文化字符串:
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)));
我注意到在您代码的 c#
部分,您使用了 Convert.ToDateTime(string)
重载。如果您正在使用它,那么您传递的字符串将使用您 运行 此代码所在机器的区域设置进行转换。因此,最可能的原因是您的本地机器和主机的区域设置不同。
您可以尝试两种方法:
- 将您计算机上的区域设置更改为主机上的区域设置。很可能错误也会出现在您的本地系统上。
- 尝试使用重载
Convert.ToDateTime(string, IFormatProvider)
。这将确保转换始终以相同的方式执行,无论您的本地设置如何。
如果从 Sql 返回的数据是日期时间(数据类型是日期时间),那么您可以直接在 Gridview 中为日期列设置日期格式,如下所示:
<asp:BoundField HeaderText="Birth Date" DataField="birthday" DataFormatString="{0:yyyy/MM/dd}" HtmlEncode="false" >
并从所有日期列的 RowDataBound 事件中删除以下代码
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;