Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey guys, I have been trying to do this for a while please help...

from this html :

XML
<div>
            <p>
                2.4. Event/Meeting start date and time (*):
                 <asp:RequiredFieldValidator ID="RequiredFieldValidator5" SetFocusOnError="true" runat="server" ControlToValidate="eventStartDate" ForeColor="red" ErrorMessage="<- Required"></asp:RequiredFieldValidator>
            </p>
            <asp:TextBox ID="eventStartDate" runat="server" Width="100px"></asp:TextBox>
            <ajaxToolkit:CalendarExtender ID="eventStartDate_CalendarExtender" Format="dd-MM-yyyy" TargetControlID="eventStartDate"
                runat="server">
            </ajaxToolkit:CalendarExtender>
            at
        <asp:DropDownList ID="eventStartHour" runat="server">
            <asp:ListItem>--</asp:ListItem>
            <asp:ListItem>00</asp:ListItem>
            <asp:ListItem>01</asp:ListItem>
            <asp:ListItem>02</asp:ListItem>
            <asp:ListItem>03</asp:ListItem>
            <asp:ListItem>04</asp:ListItem>
            <asp:ListItem>05</asp:ListItem>
            <asp:ListItem>06</asp:ListItem>
            <asp:ListItem>07</asp:ListItem>
            <asp:ListItem>08</asp:ListItem>
            <asp:ListItem>09</asp:ListItem>
            <asp:ListItem>10</asp:ListItem>
            <asp:ListItem>11</asp:ListItem>
            <asp:ListItem>12</asp:ListItem>
            <asp:ListItem>13</asp:ListItem>
            <asp:ListItem>14</asp:ListItem>
            <asp:ListItem>15</asp:ListItem>
            <asp:ListItem>16</asp:ListItem>
            <asp:ListItem>17</asp:ListItem>
            <asp:ListItem>18</asp:ListItem>
            <asp:ListItem>19</asp:ListItem>
            <asp:ListItem>20</asp:ListItem>
            <asp:ListItem>21</asp:ListItem>
            <asp:ListItem>22</asp:ListItem>
            <asp:ListItem>23</asp:ListItem>
        </asp:DropDownList>
            :
        <asp:DropDownList ID="eventStartMinute" runat="server">
            <asp:ListItem>--</asp:ListItem>
            <asp:ListItem>00</asp:ListItem>
            <asp:ListItem>05</asp:ListItem>
            <asp:ListItem>10</asp:ListItem>
            <asp:ListItem>15</asp:ListItem>
            <asp:ListItem>20</asp:ListItem>
            <asp:ListItem>25</asp:ListItem>
            <asp:ListItem>30</asp:ListItem>
            <asp:ListItem>35</asp:ListItem>
            <asp:ListItem>40</asp:ListItem>
            <asp:ListItem>45</asp:ListItem>
            <asp:ListItem>50</asp:ListItem>
            <asp:ListItem>55</asp:ListItem>
        </asp:DropDownList>
        </div>


comes "eventStartDate", "eventStartHour" and "eventStartMinute" (TextBoxID)

and I've been trying this background code:

C#
string date = arrivalDate.Text;
 string hour = arrivalHour.Text;
 string min = arrivalMinute.Text;

 //Set datestring and the format
 string dateString = date + " " + hour + ":" + min;
 string[] format = new string[] { "d/M/yyyy h:mm", "d/M/yyyy h:mm:ss" };

 DateTime dateTime;
 System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-CA");
 DateTime.TryParseExact(dateString, "d/M/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);


the string dateString is correct it has with this format = "14-01-2015 03:15" but when I do the DateTime.TryParseExact it becomes a default date ="0001-01-01 12:00:00 AM"

Help please
Posted
Updated 26-Jan-15 5:43am
v2
Comments
Richard Deeming 26-Jan-15 12:51pm    
This is the same question you asked three days ago:
How do I save a date made up of different strings to DB?[^]

1 solution

I'd start by looking at exactly what string you are passing to TryParseExact in dateString, and checking it's format manually. But...it will fail, whatever you pass.
You aren't using either of your formats that include minutes, but you do append the minutes (and a colon) to the date you collect - and then try to parse it using a format that has no minutes or colon:
C#
DateTime.TryParseExact(dateString, "d/M/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);
So...it will fail. But you don't check the return bool value, so you can't tell if it did or not. When it fails, it just leaves dateTime at the default, minimum, value.

[edit]Minor typos[/edit]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900