Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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..............
C#
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;

        }
Posted
Updated 4-Dec-12 18:48pm
v2
Comments
Sergey Alexandrovich Kryukov 5-Dec-12 0:22am    
If it was really urgent, you would provide some code sample.
--SA
bbirajdar 5-Dec-12 0:31am    
It really seems to be urgent .. You forgot to paste the code :)
Patel Shailendra 5-Dec-12 0:48am    
check your data type conversion. i think you are trying to convert large value to small. eg. int32 to int 16 in that scenario this kind of error occur. Or to get best output for you code paste your code here. Member will solve resolve your problem.
bbirajdar 5-Dec-12 0:51am    
I guess the error is in this line ...

DateTime joiningDate = Convert.ToDateTime(joiningdate);
DateTime firstpromotionDate = Convert.ToDateTime(firstpromotiondate);

Right ??????????

1 solution

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

C#
DateTime joiningDate = Convert.ToDateTime(joiningdate);
 DateTime firstpromotionDate = Convert.ToDateTime(firstpromotiondate);


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).
 
Share this answer
 
v3
Comments
MT_ 5-Dec-12 1:21am    
+5 :-)
bbirajdar 5-Dec-12 1:54am    
Thank you Milind :)
Expert Coming 5-Dec-12 2:06am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900