|
-
Feb 11th, 2003, 09:42 AM
#1
Thread Starter
Fanatic Member
Format date from SQL into a textbox
Hi
How do I format a datetime column from a table for display in a textbox. Here's my code
VB Code:
Dim conn As SqlConnection
Dim cmd As SqlCommand
Dim connstring As String Dim cmdstring As String
Dim rowsaffacted As Integer
Dim oAdapter As New SqlDataAdapter()
Dim oTable As New DataTable()
connstring = ConfigurationSettings.AppSettings("connectionstring")
conn = New SqlConnection(connstring)
Try
conn.Open()
cmd = New SqlCommand("SELECT * FROM vmvsurvey where id = " & _id.ToString(), conn)
lblStatus.Text = "SELECT * FROM vmvsurvey where id = " _
& _id.ToString()
oAdapter = New SqlDataAdapter(cmd)
oAdapter.Fill(oTable)
' This is where I'm having trouble
txtDate.Text = oTable.Rows(0).Item("surveydate")
Catch sqlex As SqlException
lblStatus.Text = sqlex.ToString()
Catch ex As Exception
lblStatus.Text = ex.ToString()
Finally
With conn
If .State <> ConnectionState.Closed Then
.Close()
End If
End With
conn = Nothing
End Try
The date value is currently displayed as 07/02/2003 15:18:00 but I would like it to be displayed as 07/02/03. I tried many formats etc. but can't seem to find the right approach. I can bind the textbox to the datasource so that's already out.
Thanks
Using VB.NET 2003/.NET 1.1/C# 2.0
http://del.icio.us/rajoo
Blow your mind, smoke gunpowder
Ashes to ashes, dust to dust
If God won't have you, the devil will. - Author unknown
Don't follow me, I'm lost too ...
-
Feb 11th, 2003, 12:50 PM
#2
Frenzied Member
you mean this does not work for you?
txtDate.Text = oTable.Rows(0).Item("surveydate").ToString("d")
-
Feb 12th, 2003, 12:07 AM
#3
Thread Starter
Fanatic Member
Lunatic3
You're right , the code below does not work
VB Code:
txtDate.Text = oTable.Rows(0).Item("surveydate").ToString("d")
For the moment I am explicitly formatting the date in my SQL statement but I wish there was something more direct ..
Using VB.NET 2003/.NET 1.1/C# 2.0
http://del.icio.us/rajoo
Blow your mind, smoke gunpowder
Ashes to ashes, dust to dust
If God won't have you, the devil will. - Author unknown
Don't follow me, I'm lost too ...
-
Feb 12th, 2003, 04:01 AM
#4
Frenzied Member
What about doing this?
VB Code:
Dim survdate as DateTime
survdate= oTable.Rows(0).Item("surveydate")
txtDate.Text=survdate.ToString("d")
For sure the formating works, but y not in your case I have no idea
-
Feb 12th, 2003, 06:12 AM
#5
Thread Starter
Fanatic Member
Thanks a lot Lunatic3.
Here's how I finally fixed it ("d" was producing 07/02/2003 when I was looking for 07/02/03).
VB Code:
txtsurveydate.Text = CType(oTable.Rows(0).Item("surveydate"), DateTime).ToString("dd/MM/yy")
I would like to share some of my findings while struggling with this. It seems that the format string is case sensitive. So,
if you have a date like 07-Feb-2003 15:18 comming from the table the formats below will produced the following results.
VB Code:
' This will output 07/02/03
txtsurveydate.Text = CType(oTable.Rows(0).Item("surveydate"), DateTime).ToString("dd/MM/yy")
' This will output 07/18/03
txtsurveydate.Text = CType(oTable.Rows(0).Item("surveydate"), DateTime).ToString("dd/mm/yy")
I was thinking that "dd/mm/yy" and "dd/MM/yy" meant the same thing
Thanks again for your help Lunatic3
Using VB.NET 2003/.NET 1.1/C# 2.0
http://del.icio.us/rajoo
Blow your mind, smoke gunpowder
Ashes to ashes, dust to dust
If God won't have you, the devil will. - Author unknown
Don't follow me, I'm lost too ...
-
Feb 12th, 2003, 08:10 AM
#6
Frenzied Member
It is not strange 'Mr.No', as 'm' stands for minute. and 'M' for month. You may like to take a look at here to find more about the format strings of Date and Time.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|