1

私は、「dd-MM-yyyy」であるインド形式でSQL Server 2008に日付を保存する必要があるasp.net Webアプリケーションに取り組んでいます。テキストボックスに日付を入力してからSQLに保存するカレンダーエクステンダーがあります。

デザインは次のとおりです。

<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
<asp:CalendarExtender ID="calStart" runat="server" TargetControlID="txtDate"
    PopupButtonID="imgvStartDate" Format="dd/MM/yyyy"></asp:CalendarExtender>
<asp:ImageButton ID="imgvStartDate" runat="server" ImageUrl="~/images/iconCalendar.gif"
TabIndex="6" Style="margin-left: -0%; height: 18px;" />

コードビハインド:-

Entity_SupplierPayment objSupplierPayment = new Entity_SupplierPayment();
    objSupplierPayment.Date = DateTime.ParseExact(txtDate.Text.Trim(), format, CultureInfo.CurrentCulture);

しかし、それはエラーを示しています:-- 文字列は有効な DateTime として認識されませんでした。

4

1 に答える 1

1

これを試して:

1 - 日付の設定: インドのカレンダー形式をグレゴリオ暦形式に変換し、データベースに保存します。

objSupplierPayment.Date = DateTime.ParseExact(txtDate.Text, "dd/MM/yyyy", null);

2 - 日付を取得:データベースから日付を読み取り、インドの形式に変換してから、GridView に表示します (セル 5 が DateTime フィールドであると仮定します)。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow
       && !string.IsNullOrEmpty(e.Row.Cells[5].Text)
       && e.Row.Cells[5].Text != "&nbsp;")
        e.Row.Cells[5].Text = Convert.ToDateTime(e.Row.Cells[5].Text).ToString("dd/MM/yyyy");
}
于 2013-09-28T10:52:58.690 に答える