Insert DateTime in ddMMyyyy format in SQL Server

rifayeem
 
on Feb 20, 2013 01:43 AM
8153 Views

can anyone tell me how to store the datetime value in dd/mm/yyyy format to the sqlserver database,,,,,this is mycode

 

    connection.Open()
        cmd = New SqlCommand("insert into datecheck(id,fromdate,todate)values(@id,@fromdate,@todate)", connection)
        cmd.Parameters.Add("id", SqlDbType.Int).Value = TextBox1.Text
        cmd.Parameters.Add("fromdate", SqlDbType.DateTime).Value = DateTime.Parse(txtdob.Text)
        cmd.Parameters.Add("todate", SqlDbType.DateTime).Value = DateTime.Parse(txtdoj.Text)
        cmd.ExecuteNonQuery()
        Response.Write("ok")
    End Sub

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Mudassar
 
on Feb 20, 2013 01:45 AM

You cannot store formatted date in database.  You can do formatting when displaying data on page

 

Mustafa
 
on Feb 20, 2013 04:52 AM

I have just created the sample codes to just change the formate of date while displaying on web page

 

Dim fromDate As New DateTime()
Dim toDate As New DateTime()
Dim Id As String, fromDateString As String, toDateString As String
connection.Open()
Dim cmd As New SqlCommand("SELECT id,fromdate,todate FROM datecheck WHERE id = 1", connection)
Dim reader As SqlDataReader = cmd.ExecuteReader()
While reader.Read()
	Id = reader(0).ToString()
	fromDate = Convert.ToDateTime(reader(1))
	toDate = Convert.ToDateTime(reader(2))
End While

fromDateString = fromDate.ToString("dd/MM/yyyy")
toDateString = toDate.ToString("dd/MM/yyyy")

now in the above code you can replace "fromDateString" with you control Name

for eg: if it is text box than do like this

textBox1.Text = fromDate.ToString("dd/MM/yyyy")

and in the same way you can do this for your to Date

suddu89
 
on Feb 20, 2013 04:56 AM

txtdob.Text = Convert.ToDatetime(retrieveddatefromtable).ToString("dd/MM/yyyy")

this way you can get the date from table in DD/MM/YYYY format