Solved

String Format for DateTime?

Posted on 2013-06-07
8
323 Views
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?

SS
            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"
            Else
                '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
                            sb.Append(stDataReader2(i).ToString())
                        End If
                        i = i + 1
                    End While
                    sw.WriteLine(sb.ToString())
                End While
            End If

            'close StreamWriter
            sw.Close()

            'close dataREADER
            stDataReader2.Close()

Open in new window


TIA,
Andrew
0
Comment
Question by:Ahelbling
  • 3
  • 3
  • 2
8 Comments
 
LVL 10

Expert Comment

by:adriankohws
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")
0
 
LVL 20

Expert Comment

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

Expert Comment

by:adriankohws
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.
0
 

Author Comment

by:Ahelbling
ID: 39233535
Any suggestions on how to accomplish this?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 20

Expert Comment

by:informaniac
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.
0
 

Author Comment

by:Ahelbling
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
0
 
LVL 10

Accepted Solution

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

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

Author Comment

by:Ahelbling
ID: 39233663
I will try that - thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now