views:

94

answers:

4

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

+1  A: 

I'm not sure that this will compiled, but will give you the clue

DateTime LastPayDate = DateTime.Parse( (TextBox)FormView1.FindControl("user_last_payment_date")).Text );
Orsol
+2  A: 

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);
Pranay Rana
+2  A: 

FindControl will return a Control, not the contents of the control.

TextBox textBox = (TextBox)FormView1.FindControl("user_last_payment_date");
DateTime LastPayDate = DateTime.Parse(textBox.Text);
Phil
+2  A: 
    //If you really need to find the textbox
    TextBox dateTextBox = 
            FormView1.FindControl("user_last_payment_date") as TextBox;

    if(dateTextBox == null)
    {
        //could not locate text box
        //throw exception?
    }

    DateTime date = DateTime.MinValue;

    bool parseResult = DateTime.TryParse(dateTextBox.Text, out date);

    if(parseResult)
    {
        //parse was successful, continue
    }
fletcher