문제

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.

도움이 되었습니까?

해결책

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

System.DateTime dateValue = DateTime.ParseExact(...);
command.Parameters.AddWithValue("@name", dateValue);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top