Results 1 to 6 of 6

Thread: Format date from SQL into a textbox

  1. #1

    Thread Starter
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    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:
    1. Dim conn As SqlConnection
    2.    Dim cmd As SqlCommand
    3.    Dim connstring As String         Dim cmdstring As String
    4.    Dim rowsaffacted As Integer
    5.    Dim oAdapter As New SqlDataAdapter()
    6.    Dim oTable As New DataTable()
    7.    connstring = ConfigurationSettings.AppSettings("connectionstring")
    8.    conn = New SqlConnection(connstring)
    9.    Try
    10.     conn.Open()
    11.     cmd = New SqlCommand("SELECT * FROM vmvsurvey where id = " & _id.ToString(), conn)
    12.     lblStatus.Text = "SELECT * FROM vmvsurvey where id = " _
    13.                            & _id.ToString()
    14.      oAdapter = New SqlDataAdapter(cmd)
    15.      oAdapter.Fill(oTable)
    16.  
    17.      ' This is where I'm having trouble
    18.      txtDate.Text = oTable.Rows(0).Item("surveydate")  
    19.    Catch sqlex As SqlException
    20.        lblStatus.Text = sqlex.ToString()
    21.     Catch ex As Exception
    22.        lblStatus.Text = ex.ToString()
    23.     Finally
    24.        With conn
    25.           If .State <> ConnectionState.Closed Then
    26.               .Close()
    27.           End If
    28.        End With
    29.        conn = Nothing
    30.    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 ...

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    you mean this does not work for you?

    txtDate.Text = oTable.Rows(0).Item("surveydate").ToString("d")

  3. #3

    Thread Starter
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651
    Lunatic3

    You're right , the code below does not work

    VB Code:
    1. 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 ...

  4. #4
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    What about doing this?

    VB Code:
    1. Dim survdate as DateTime
    2. survdate= oTable.Rows(0).Item("surveydate")
    3. txtDate.Text=survdate.ToString("d")

    For sure the formating works, but y not in your case I have no idea

  5. #5

    Thread Starter
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651
    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:
    1. 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:
    1. ' This will output 07/02/03
    2. txtsurveydate.Text = CType(oTable.Rows(0).Item("surveydate"), DateTime).ToString("dd/MM/yy")
    3.  
    4. ' This will output 07/18/03
    5. 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 ...

  6. #6
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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
  •  



Click Here to Expand Forum to Full Width