Get DateTime Value from TextBox in FormView

Sanju picture Sanju · Jul 22, 2010 · Viewed 14k times · Source

I need to find a value in a TextBox, contained within a FormView which holds a short date.

DateTime LastPayDate = (DateTime)FormView1.FindControl("user_last_payment_date");

I get the error:

CS0030: Cannot convert type 'System.Web.UI.Control' to 'System.DateTime'

And, I have no idea how to put a value back in the same format. Would love some help, pulling my hair out and their isn't much left.

Thanks

Answer

Pranay Rana picture Pranay Rana · Jul 22, 2010

There is error in you code because you are trying to convert control directly in date time, so to resolve your error you need to convert control in textbox control and than convert text in datetime as given below

 DateTime LastPayDate = Convert.ToDateTime( 
                      ((System.Web.UI.WebControls.TextBox)  
                       FormView1.FindControl("user_last_payment_date")).Text);