Plz先生,这是一个紧急的问题。 [英] Plz sir this is an urgent.
问题描述
您好先生,
在我的项目中,我正在更新gridview中的数据。面临此问题将varchar数据类型转换为日期时间数据类型会导致超出范围的值。 。
PLZ先生,给我一些解决方案。
代码..............
Hello sir,
In my project I am updating data in gridview. facing this problem "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value." .
plz sir,Send me some solution.
Code..............
protected void gvgrpB_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string presentDesig;
if ((gvgrpB.Rows[e.RowIndex].FindControl("txtPresentDesignation") as TextBox).Text == "")
{
presentDesig = null;
}
else
{
presentDesig = (gvgrpB.Rows[e.RowIndex].FindControl("txtPresentDesignation") as TextBox).Text;
}
string joiningdate;
if ((gvgrpB.Rows[e.RowIndex].FindControl("txtJoiningDate") as TextBox).Text == "")
{
joiningdate = null;
}
else
{
joiningdate = (gvgrpB.Rows[e.RowIndex].FindControl("txtJoiningDate") as TextBox).Text;
}
DateTime joiningDate = Convert.ToDateTime(joiningdate);
DateTime firstpromotionDate = Convert.ToDateTime(firstpromotiondate);
UpdateGroupB(joiningDate ,firstpromotionDate )
}
public void UpdateGroupB( DateTime joiningDate, DateTime firstpromotionDate)
{
try
{
string strConn = ConfigurationManager.ConnectionStrings["bdConnection"].ConnectionString;
string selectSQL = "UPDATE idco.empdtl_grpB SET Joining_Date='" + joiningDate + "',First_Promotion_Date='", Update_ts=getdate() WHERE Sl_no = " + slno + "";
SqlConnection con = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand(selectSQL, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
gvgrpB.EditIndex = -1;
BindData();
}
catch (Exception ex)
{
throw ex;
}
推荐答案
您正在以文本格式传递无效日期。请检查da正确的字符串...在这些代码行中使用DateTime.Parse而不是Convert.ToDateTime()
You are passing a invalid date in text format. Check the date string properly... Use DateTime.Parse instead of Convert.ToDateTime() in these lines of code
DateTime joiningDate = Convert.ToDateTime(joiningdate);
DateTime firstpromotionDate = Convert.ToDateTime(firstpromotiondate);
DateTime.Parse(String)方法尝试转换字符串将日期和时间值表示为其DateTime等效项。要解析的字符串可以采用以下任何一种形式:
带有日期和时间组件的字符串。
带有日期但没有时间组件的字符串。
带时间但没有日期组件的字符串。
包含时区信息并符合ISO 8601的字符串。例如,以下两个字符串中的第一个指定协调世界时(UTC);第二个指定比UTC早7个小时的时区:
2008-11-01T19:35:00.0000000Z
2008-11-01T19:35:00.0000000-07:00
包含GMT指示符并符合RFC 1123时间的字符串格式。例如:
星期六,2008年11月1日19:35:00 GMT
包含的字符串日期和时间以及时区偏移信息。例如:
03/01/2009 05:42:00 -5:00
这个方法尝试完全解析s并避免抛出FormatException。如果可能,它会忽略无法识别的数据,并使用当前日期填写缺少的月,日和年信息。如果s只包含日期而没有时间,则此方法假定午夜12点。如果s仅包含时间且没有日期,则此方法假定当前日期。如果s包含具有两位数年份的日期组件,则会根据Calendar.TwoDigitYearMax属性的值将其转换为当前文化的当前日历中的年份。 s中的任何前导,内部或尾随空格字符都将被忽略。日期和时间可以用一对前导和尾随的NUMBER SIGN字符('''',U + 0023)括起来,并且可以用一个或多个NULL字符(U + 0000)跟踪。
The DateTime.Parse(String) method tries to convert the string representation of a date and time value to its DateTime equivalent. The string to be parsed can take any of the following forms:
A string with a date and a time component.
A string with a date but no time component.
A string with a time but no date component.
A string that includes time zone information and conforms to ISO 8601. For example, the first of the following two strings designates the Coordinated Universal Time (UTC); the second designates the time in a time zone seven hours earlier than UTC:
2008-11-01T19:35:00.0000000Z
2008-11-01T19:35:00.0000000-07:00
A string that includes the GMT designator and conforms to the RFC 1123 time format. For example:
Sat, 01 Nov 2008 19:35:00 GMT
A string that includes the date and time along with time zone offset information. For example:
03/01/2009 05:42:00 -5:00
This method attempts to parse s completely and avoid throwing a FormatException. It ignores unrecognized data if possible and fills in missing month, day, and year information with the current date. If s contains only a date and no time, this method assumes 12:00 midnight. If s contains only a time and no date, this method assumes the current date. If s includes a date component with a two-digit year, it is converted to a year in the current culture''s current calendar based on the value of the Calendar.TwoDigitYearMax property. Any leading, inner, or trailing white space character in s is ignored. The date and time can be bracketed with a pair of leading and trailing NUMBER SIGN characters (''#'', U+0023), and can be trailed with one or more NULL characters (U+0000).
这篇关于Plz先生,这是一个紧急的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!