Format SqlDataReader as date




Format SqlDataReader as date
If you would like to format an SqlDataReader in a nice date format, below is an example showing how to do so:
  1. Dim objDR As SqlDataReader   
  2.   
  3. DateTime.Parse(objDR("date")).ToString("dddd dd MMMM yyyy")  
The above displays the date like this 'Monday 01 June 2009', you can obviously change this by changing the code within the 'ToString' brackets.
 
An example of this in full use is below:
 
  1. Dim MyConnection As SqlConnection   
  2.     MyConnection = New SqlConnection()   
  3.     MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString   
  4.   
  5. Dim sql As String = "SELECT * FROM TABLE WHERE TableID = 1"  
  6. Dim objDR As SqlDataReader   
  7. Dim CmdR As New SqlCommand(sql, MyConnection)   
  8.   
  9. MyConnection.Open()   
  10. objDR = CmdR.ExecuteReader(System.Data.CommandBehavior.CloseConnection)   
  11. While objDR.Read()   
  12.             Response.Write(DateTime.Parse(objDR("date")).ToString("dddd dd MMMM yyyy"))   
  13. End While  
  14. MyConnection.Close()  


No one has said that they like article yet, be the first and click here (0)
Send a link to this page via email Send a link to this page