Insert Persian Date to Database using ASP.Net SqlDataSource in C#

Last Reply 6 months ago By dharmendr

Posted 6 months ago

I want to get the persian date from user and covert it to insert it into DB.and also the before it is show to user convert it in to persian date.

but when i insert data to table fistname and lastname was inserted to table but the HireDate didn't insert to my table.

I do not know why it does not work correctly!

this is my code: 

        <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();
        }

 

You are viewing reply posted by: dharmendr 6 months ago.
Posted 6 months ago Modified on 6 months ago

Hi dorsa,

Assign the OnInserting event to the SqlDataSource and set the value to sqlDtSrcEmployees_Inserting.

<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]" OnInserting="sqlDtSrcEmployees_Inserting">
    <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>

And for reconverting parsian date assign the OnRowDataBound event ot the GridView and set it to gvEmployees_RowDataBound

<asp:GridView ID="gvEmployees" CssClass="gridStyle" runat="server" Caption="کارمندان موجود در پایگاه داده"
    DataSourceID="sqlDtSrcEmployees" AutoGenerateColumns="False" OnRowDataBound="gvEmployees_RowDataBound">