3

I want dates to look on page like dd.MM.yyyy. Me like this way, which is culture-independent.

I don't know how make it right.

cmd.Parameters.Add("@eventdate", SqlDbType.Date).Value = dateAddTxtBox.Text;

Format Exception:

String was not recognized as a valid DateTime

I tried many snippets like DateTime.ParseExact(dateAddTxtBox.Text, "dd.MM.yyyy", null) or using System.Globalization.CultureInfo.InvariantCulture as well. Nothing helped!

Or maybe it will better done in SQL procedure side, won't it?

CREATE PROCEDURE add_history
@user       VARCHAR(20),
@eventdate  DATE,
...
INSERT INTO History(userid, eventdate, ...)
VALUES ((SELECT userid FROM Users.SUsers WHERE suname=@user), @eventdate, ...)

All worked pretty good while I haven't format my TextBoxes with pattern:

<asp:TextBox id="dateAddTxtBox" runat="server" ReadOnly="True" />
<asp:CalendarExtender ID="AddTxtBoxCalendarExtender" runat="server" Enabled="True" TargetControlID="dateAddTxtBox" Format="dd.MM.yyyy" ... />

UPD:

A little mention >> there was read-only attribute on TextBox. That's why it didn't work!

Now the question is, is it correct to skip ReadOnly="True"? I don't want users to edit dates right in the box.

UPD2:

ReadOnly="True" doesn't make a sense at all. It works fine as it should! I don't know why.

master-lame-master
  • 3,101
  • 2
  • 30
  • 47
  • 2
    When I tried this DateTime.ParseExact("31.12.1983","dd.MM.yyyy",null), it worked for me. Why don't you put breakpoint and check what your text box is actually returning. Also at the same place try the same that I tried in watch(with some static dates) so that you can know if it is something the problem with your textbox entry or for every date. Just a try – Pawan Nogariya Jan 11 '13 at 06:17
  • But it won't with TextBox! Ok, I found the reason! – master-lame-master Jan 11 '13 at 06:24

1 Answers1

2

Don't specify SqlDbType.Date. Rely on auto-detection instead, it will work:

System.DateTime dateValue = DateTime.ParseExact(...);
command.Parameters.AddWithValue("@name", dateValue);
abatishchev
  • 98,240
  • 88
  • 296
  • 433