Best practice for working with DateTime in and my sql server

Best practice for working with DateTime in and my sql server
Format the update parameter value at time of updating List View

my sql store data in  yyyy-mm-dd format...
but it can execute update command if date is in mm/dd/yyyy fromat.
My web app displays controls in uk , indian format ie dd/mm/yyyy
You can retrive date in dd/mm/yyyy format from mysql by folowing query 
SELECT  DATE_FORMAT(sch_dt,'%d/%m/%Y') as sch_dt,  FROM comm_prog

So finally here is the Best practice for working with DateTime in and my sql server.

Make an entry in your web.config file like this inside system.web

 <!-- To let the default date as en-US style mm/dd/yyyy in backgrount and let controls have en-GB dd/mm/yyyy format /  Culture fr-CH, Date: 30.11.2007 00:00:00-->
 <globalization   requestEncoding="utf-8"   responseEncoding="utf-8"   culture = "en-US"   uiCulture = "en-US" />

Let the user see dates in dd/mm/yyyy format

                        <asp:TextBox ID="sch_dtTextBox" runat="server" Text='<%# Bind("sch_dt") %>' />
                        <asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="sch_dtTextBox" Format="dd/MM/yyyy">

But you workaround in MM/dd/yyyy format in your code and change my sql update query  parameter in list view itemupdating event as 

Protected Sub ListView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewUpdateEventArgs) Handles ListView1.ItemUpdating
        Dim ukProvider As IFormatProvider = New System.Globalization.CultureInfo("en-GB")  ' uk style date dd/MM/yyyy
        Dim txt As TextBox = TryCast(ListView1.EditItem.FindControl("sch_dtTextBox"), TextBox)
        If txt IsNot Nothing Then
            Dim temp As String = DateTime.Parse(txt.Text, ukProvider, System.Globalization.DateTimeStyles.NoCurrentDateDefault)
            'SqlDataSource1.UpdateParameters("sch_d").DefaultValue = temp 'Strings.Mid(temp, 1, temp.length - 1)
            e.NewValues("sch_dt") = temp
        End If
    End Sub 

Dont forgot to Envoke this from Listview1 onitemupdating event:

 <asp:ListView ID="ListView1" runat="server" DataKeyNames="uid" 
            DataSourceID="SqlDataSource1" InsertItemPosition="LastItem" OnItemUpdating ="ListView1_ItemUpdating">

Vinod Kotiya


Most Viewed Post

solution for facebook waiting

Connect Sony XPLOD MEX-BT2500 Bluetooth CD Receiver with ur mobile

Crystal Report : Show data horizontally (Left to right) i.e. columns as rows