i have a calender control on my page which is generated using javascript. As shown below how it looks in my aspx page.

<td colspan="2"><asp:Label ID="lblPckgStartDate" runat="server" Text="Pckg Start Date"></asp:Label>
                <input ID="txtPckgStartDate" runat="server" class="NormalTextBox" ReadOnly=ReadOnly type="text" />&nbsp;<a onclick="showCalendarControl(txtPckgStartDate)" href="#"><img src="calendar.gif" style="width: 20px; height: 20px" border=0 /></a>
</td>

Now when i access this textbox value on server side and load it into string variable and then pass it to date time object it gives me following error.

DateTime date = new DateTime();
string dateinddmmyyyy = txtPckgStartDate.Value.Trim();
//date in mm/dd/yyyy format
date = Convert.ToDateTime(dateinddmmyyyy);

Error:
Failed to convert parameter value from a String to a DateTime.

Plz provide me some solutions to convert string to datetime ASAP.

Use DateTime.TryParseExact method.

DateTime date;
        string[] format = { "dd/MM/yyyy", "dd-MM-yyyy" };
        string dateinddmmyyyy = "31/12/2003";

        DateTime.TryParseExact(dateinddmmyyyy, format, null, System.Globalization.DateTimeStyles.None, out date);
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.