Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


String Format for DateTime?

Posted on 2013-06-07
Medium Priority
Last Modified: 2013-06-10
Can somebody please help me understand why I am getting this error when I try to format a smalldatetime field from SQL to a string formatted date time?

            Dim selectCommand6 As New SqlCommand("SELECT BarCode, BeginDate, Species, Notes, Owner, WaterCode FROM StakePrint WHERE Owner=@UID", stConPrint)
            selectCommand6.Parameters.AddWithValue("UID", sUID)

            Dim sw As New StreamWriter(HttpContext.Current.Server.MapPath("LabelFiles/Stakes/test.txt"))

            Dim stDataReader2 As SqlDataReader = selectCommand6.ExecuteReader
            'any rows returned?
            If Not stDataReader2.HasRows Then
                'no rows returned
                'lblTest2.Text = "NO RECORDS"
                'rows returned
                'lblTest2.Text = "RECORDS"
                Dim fields As Integer = stDataReader2.FieldCount - 1
                Dim sFieldName As String
                Dim sep As String = ","
                While stDataReader2.Read()
                    Dim i As Integer = 0
                    Dim sb As New StringBuilder()
                    While i <= fields
                        sFieldName = stDataReader2.GetName(i)
                        If sFieldName = "BarCode" Then
                            sb.Append(stDataReader2(i).ToString() + sep)
                        ElseIf sFieldName = "BeginDate" Then
                            sb.Append(stDataReader2(i).ToString("M/d/yyyy") + sep)
                        ElseIf sFieldName = "Notes" Then
                            sb.Append(stDataReader2(i).ToString() + sep)
                        ElseIf sFieldName = "Owner" Then
                            sb.Append(stDataReader2(i).ToString() + sep)
                        ElseIf sFieldName = "WaterCode" Then
                        End If
                        i = i + 1
                    End While
                End While
            End If

            'close StreamWriter

            'close dataREADER

Open in new window

Question by:Ahelbling
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
LVL 10

Expert Comment

ID: 39231035
Because when you get datetime from SQL database, it is going to be with date and time displaying time as many zeros with semi-colons. System won't identify as date in VB.

Do this:
Dim Datestring as string
Datestring = Format(CDate(stDataReader2(i)), "M/d/yyyy")
LVL 20

Expert Comment

ID: 39232501
I would guess that you are getting blanks/null in the date column.
LVL 10

Expert Comment

ID: 39232813
Looking at the error message, there's data, just that datatype was not correct as expected and this way of format doesn't work and resulted that the string converted, instead of a date is actually ‘M/d/yyyy' and system attempted to cast as integer.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 39233535
Any suggestions on how to accomplish this?
LVL 20

Expert Comment

ID: 39233643
What dateformat is returned by the database? Is it dd-mm-yyyy?

If it is it is possible tht you are getting a date greater than 12.

Author Comment

ID: 39233651
Exactly what is stored in the SQL table is what is displayed without any formatting performed.  It is a smalldatetime field.  Date-hours-mins-seconds
LVL 10

Accepted Solution

adriankohws earned 2000 total points
ID: 39233654
This doesn't work for you?

Datestring = Format(CDate(stDataReader2(i)), "M/d/yyyy")

Author Comment

ID: 39233663
I will try that - thanks

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question