如何将日期时间类型转换为波斯日期?

时间:2018-12-10 07:19:13

标签: html sql asp.net

我有一个代码,可以在表中插入数据。数据插入正常。但是它不会插入用户插入的DateTime,并且当我想显示数据时,它不会将表的DateTime字段转换为我之前插入过的波斯日历 这是我的代码:

    <asp:GridView ID="gvEmployees" CssClass="gridStyle" runat="server" Caption="کارمندان موجود در پایگاه داده" DataSourceID="sqlDtSrcEmployees" AutoGenerateColumns="False">
            <AlternatingRowStyle BackColor="#FF99CC" />
            <Columns>
                <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" SortExpression="EmployeeID" />
                <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                <asp:BoundField DataField="HireDate" HeaderText="HireDate" SortExpression="HireDate" />
            </Columns>
            <EditRowStyle BackColor="#FF99CC" />
            <HeaderStyle BackColor="#FF66CC" Font-Bold="True" Font-Names="Calibri" ForeColor="White" />
            <RowStyle BackColor="#FFCCFF" Font-Names="Calibri" HorizontalAlign="Center" />
        </asp:GridView>


        <asp:SqlDataSource ID="sqlDtSrcEmployees" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString2 %>"
            InsertCommand="INSERT INTO [Employees] ([LastName], [FirstName], [HireDate]) VALUES (@LastName, @FirstName, @HireDate)"
            SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [HireDate] FROM [Employees]">
            <InsertParameters>
                <asp:ControlParameter ControlID="txtLastName" Name="LastName" PropertyName="Text" Type="String" />
                <asp:ControlParameter ControlID="txtFirstName" Name="FirstName" PropertyName="Text" Type="String" />
                <asp:Parameter Name="HireDate" Type="DateTime" />
            </InsertParameters>
        </asp:SqlDataSource>

 protected void gvEmployees_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DateTime usDate = (DateTime)DataBinder.Eval(e.Row.DataItem, "HireDate");
                    System.Globalization.PersianCalendar faDate = new System.Globalization.PersianCalendar();

                    string day = faDate.GetDayOfMonth(usDate).ToString("D2");
                    string month = faDate.GetMonth(usDate).ToString("D2");
                    string year = faDate.GetYear(usDate).ToString("D4");

                    e.Row.Cells[3].Text = string.Format("{0} / {1} / {2}", year, month, day);
                }
            }
            catch { }
        }

        protected void sqlDtSrcEmployees_Inserting(object sender, SqlDataSourceCommandEventArgs e)
        {
            try
            {
                int day = Convert.ToInt32(TxtHireDay.Text);
                int month = Convert.ToInt32(TxtHireMonth.Text);
                int year = Convert.ToInt32(TxtHireYear.Text);

                System.Globalization.PersianCalendar faDate = new System.Globalization.PersianCalendar();
                e.Command.Parameters["@HireDate"].Value = faDate.ToDateTime(year, month, day, 23, 0, 0, 0);
            }
            catch { }
        }
        protected void Button1_Click1(object sender, EventArgs e)
        {
            sqlDtSrcEmployees.Insert();
        }

我不知道为什么它不起作用?!

0 个答案:

没有答案