Hey Guys,
I have a textbox which had readonly property set to true.
This textbox is used to take LongDate as it's input from a pop up calender. When i click on the button, it gives an error
"String was not recognised as valid datetime"
But when i set the textbox's readonly property to false it works completely alright, it returns values fromthe database.
Here is the code which i used

TextBox date_search = (TextBox)Page.PreviousPage.FindControl("tbMyDate");
            DateTime dateData;
            dateData = Convert.ToDateTime(date_search.Text.Trim());
            TextBox2.Text = dateData.ToLongDateString();

Why does it work when the readonly property is set to false????

First you need to check whether the value returned by 'date_search' does not contain blank value by putting break point.

Also the datetime parsing in C# is based on the local date time settings. Therefore if your local date format is dd/mm/yyyy and if you enter date in mm/dd/yyyy format then this error will be happened.

TextBox date_search = (TextBox)Page.PreviousPage.FindControl("TextBox1");
        DateTime dateData;
       
        if (date_search.Text!="" )
        {
           dateData =Convert.ToDateTime(date_search.Text.ToString());
           TextBox2.Text = dateData.ToLongDateString();
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.