Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting a string into a date format 1

Status
Not open for further replies.

peebman2000

Programmer
Nov 28, 2006
30
US
Okay I’m back, again I’m a newbie still working on the same project. My project basically works now, its almost done; but I’m trying to convert a string into a date for each line it reads from the uploaded text file. Now in the text file its reading from the date is formatted at 2005-10-16 year, month, and day. I’m trying to convert it to 10/16/2005 (with forward slashes) when it writes out the text. Below is my code and I’m getting an error when it trys to convert the date, the error is “Conversion from string " " to type 'Date' is not valid.” I know it may be something small but I can’t see what I’m doing wrong. I’ve been to several websites and as I look at my code it seem like I coded it right. Does anyone know why I’m getting this error?

Imports System.IO
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Web.Security
Imports System.Web.Configuration
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Text
Imports System.Web.Management
Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'when the page is loaded
Label1.Visible = False
If Not Page.IsPostBack Then

End If
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Visible = True
Dim slnin As String = ""
Dim slnout As String = ""
Dim outputfile As String = ""
'Dim deptid As String
Dim semi As String = ""
Dim national_id As String = ""
Dim last_name As String = ""
Dim first_name As String = ""
Dim mid_initial As String = ""
Dim suffix As String = "" ' find this ' may be blank 30 characters
Dim dept As String = "" 'find this
Dim payroll As String = "" 'find this
Dim address1 As String = ""
Dim city As String = ""
Dim state As String = ""
Dim postal As String = ""
Dim phone As String = ""
Dim hire_dt As New Date()
Dim mystring As String = hire_dt.ToString("mm/dd/yyyy")

'Dim hire_dt As String = ""
Dim appt_type As String = "" 'find this
Dim serv_years As String = "" 'find this
Dim serv_days As String = "" 'find this
Dim collective_b_unit As String = "" 'find this
Dim collective_b_flag As String = "" 'find this
Dim classification_number As String = "" 'find this
Dim business_title As String = ""
Dim payflag As String = "" 'find this
Dim initial_probation_flag As String = "" 'find this
Dim sep_date As String = "" 'find this
Dim sep_code As String = "" 'find this
Dim pay_period_end As String = "" 'find this


'Dim mystringbuilder As New StringBuilder
semi = ";"

If FileUpload1.HasFile Then

'upload file
'Dim filein As String = FileUpload1.HasFile
'Dim objstreamreader As StreamReader
'

'declaring variables

'Dim orig_hire_dte As String = ""
'im filename As String
'Dim objstreamreader As StreamReader
Dim filename As String = ""
If filename = String.Empty Then
filename = FileUpload1.PostedFile.FileName
End If
Dim sr As StreamReader
'Dim streamreader As New StreamReader("c:\mastertestfile.txt")
sr = File.OpenText(filename)

While sr.Peek() <> -1
slnin = sr.ReadLine

'slnin = objstreamreader.ReadLine
'Do While Not strline Is Nothing

'objstreamreader = File.OpenText(filename) 'may undo
'objstreamreader = File.OpenText()'may undo


If Not slnin Is Nothing Then 'store text in slnin
'deptid = slnin.Substring(3000, 1000)
national_id = slnin.Substring(1384, 20)
last_name = slnin.Substring(1650, 30)
first_name = slnin.Substring(1685, 30)
mid_initial = slnin.Substring(1720, 30)
address1 = slnin.Substring(1193, 55)
city = slnin.Substring(1305, 30)
state = slnin.Substring(1365, 6)
postal = slnin.Substring(1372, 12)
phone = slnin.Substring(548, 24)
hire_dt = slnin.Substring(198, 10)
business_title = slnin.Substring(33, 10)

'last_name = last_name.PadRight(25)
'build line to be wrote out
'Dim myStringBuilder As New StringBuilder
'myStringBuilder.Append(national_id + semi)
slnout += national_id + semi
slnout += last_name + semi
slnout += mid_initial + semi
slnout += first_name + semi
slnout += address1 + semi
slnout += city + semi
slnout += state + semi
slnout += postal + semi
slnout += phone + semi
slnout += hire_dt.ToString("mm/dd/yyyy") + semi
'slnout += hire_dt + semi
slnout += business_title + semi & vbCrLf

outputfile += slnout '& vbCrLf
'Response.Write(slnout)
'Response.End()
End If
End While
Response.Write(slnout)
Response.End()
sr.Close()
'Dim fileout As String = "\newfile" & ".txt"
Try '& FileUpload1.Filename)
File.WriteAllText(Server.MapPath("uploads/newfile.txt"), outputfile)
Catch ex As Exception
''FileUpload1.SaveAs(Server.MapPath("uploads\" & FileUpload1.FileName))
Label1.Text = "Failed because <br/>" & ex.Message
End Try
Label1.Text = "File uploaded to server" '& FileUpload1.PostedFile.FileName
sr.Close()
'objstreamreader.Close()
'Console.ReadLine()
Else
Label1.Text = "Please select a file to upload"
End If

End Sub
End Class
 
You don't say where the error occurs, but
the error is “Conversion from string " " to type 'Date' is not valid.”
means that you are trying to convert a String which is basically 10 empty characters to a Date which will obviously fail.

You need to check the value of whatever String you are trying to convert.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Sorry ca8msm , I guess I didn't. Anyway I found a function on line to check the value in the string, I found this code below and it works now. Thanks for the reply.

Private Shared Function IsDate(ByVal sDate As String) As Boolean
Dim dt As DateTime
Dim isDate1 As Boolean = True
Try
dt = DateTime.Parse(sDate)
Catch
isDate1 = False
End Try
Return isDate1
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top