Search code examples
asp.netdatetimedatetimepicker

datetime exception


im getting exception while i am fatching the data from the text-box of gridview

DateTime dt;
.
.
{

 dt = DateTime.Parse(Request.Form[row.FindControl("txtPLI_MAN").UniqueID]);
 // then i handel this to data base  

but im getting exception over here that "String was not recognized as a valid DateTime"

here is the exception detail

System.FormatException was unhandled by user code
Message="String was not recognized as a valid DateTime."
Source="mscorlib"
StackTrace:
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles   styles)
   at System.DateTime.Parse(String s)
   at Newattendance.Button1_Click(Object sender, EventArgs e) in   c:\Inetpub\wwwroot\conversion\work_space\my_workspace.aspx.cs:line 61
   at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 

where i am wrong how to get the date from text box properly


Solution

  • Try to use a string and convert the string to datetime. like:

     string s="";
    s=Request.Form[row.FindControl("txtPLI_MAN").UniqueID];
    datetime dt;
    dt=datetime.Parse(s);
    

    or

    dt=(datetime)s;
    

    and see what happens

    At least with this you can debug at each and every step to see the content and see where you are making a mistake