开发者

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.Rais开发者_JS百科ePostBackEvent(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


This is a fine way to retrieve the value. The error message says that the value is not in a format that it can convert to a string though. That's the problem you need to investigate (not enough details given for us to really help)

An alternative way to get the value without Request.Form is to ask the control directly...

dt = DateTime.Parse(((TextBox)row.FindControl("txtPLI_MAN")).Text);

This won't fix your parsing problem though


I'm not sure what row denotes in your code and also Request.form isn't right place to look for the values at the time of post back.

I'll suggest you should include the button1 in all grid view row like: <asp:button id="btn" runat="server commandname="doit" />

and then implement Row_Command event of a grid in order to get data correctly from textbox. check if the current command is "doit" by checking e.CommandName=="doit" and get your textbox from e.Item.findControl

I'm geussing you are not using above method from exception, since it says button1_click and you should not handle button click but handle row_command to get going with that.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜