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!

write out a string as a space if date is null or nothing

Status
Not open for further replies.

peebman2000

Programmer
Nov 28, 2006
30
US
Hey everyone, peebman2000 needs help again; still on the same project. This is my last issue though, well it should be. Anyway below is my code and look at the pay_period_end substring within the IsDate if statement. What I’m trying to do is when it reads the text file if the isdate is false I want it to store in as a space. Currently if there is no date it stores in 01/01/1900 and I don’t want it to do that. I’ve tried several different ways to default to “ “ but I keep getting convert string type to date error. Does anyone have any suggestions on how I can get it to write out “ “ a space if there is no date there.

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.UserControl
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 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 = ""
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 reg_temp As String = ""
'Dim full_part_time As String = ""
Dim appt_type As String = " "
Dim oh_srv_yrs As String = ""
Dim oh_srv_dys As String = ""
Dim barg_unit As String = ""
Dim oh_bu_flag As String = ""
'Dim union_cd As String = ""
'Dim jobcode As String = ""
Dim business_title As String = "" ' coded this variable thinking this was the job_title
Dim emp_status As String = "" ' coded this variablet thinking this was the payflag
Dim probation_dt As New Date()
Dim probationstring As String = probation_dt.ToString("MM/dd/yyyy")
Dim termination_dt As New Date() ' coded this variable thinking this was the separation_dt
Dim terminationstring As String = termination_dt.ToString("MM/dd/yyyy")
'Dim action As String = ""
'Dim action_reason As String = ""
Dim sep_code As String = " "
Dim pay_period_end As New Date()
Dim spacestring As String = ""
Dim paystring As String = pay_period_end.ToString("MM/dd/yyyy")
Dim probation_check As String = ""
Dim class_indc As String = "" 'code this variable thinking this was the job_indicator
Dim dept As String = ""
Dim dept_ID As String = ""
Dim payroll As String = ""
semi = ";"
Dim space As String = ""
Dim payflag As Boolean = False
Dim termflag As Boolean = False




'Dim mystringbuilder As New StringBuilder

'Dim fileok As Boolean = False ' may delete
Dim doctxt As String = Right(FileUpload1.FileName, 3).ToString
'Try
'If doctxt.ToLower = "txt" Or doctxt.ToLower = "txt" Or doctxt.ToLower = Nothing Or doctxt = "" Then
If FileUpload1.HasFile And doctxt.ToLower = "txt" Or doctxt.ToLower = "txt" Then
Dim filename As String = ""
If filename = String.Empty Then
filename = FileUpload1.PostedFile.FileName
End If
Dim sr As StreamReader

sr = File.OpenText(filename)

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

If Not slnin Is Nothing Then 'store text in slnin

national_id = slnin.Substring(1384, 20)
last_name = slnin.Substring(1650, 30)
first_name = slnin.Substring(1685, 30)
mid_initial = slnin.Substring(1720, 30)
suffix = slnin.Substring(1645, 15)
dept_ID = slnin.Substring(0, 9)
dept = dept_ID
address1 = slnin.Substring(1196, 22)
'address2 = slnin.Substring(446, 55)
city = slnin.Substring(1306, 30)
state = slnin.Substring(1366, 6)
postal = slnin.Substring(1372, 12)
phone = slnin.Substring(1408, 24) ' this is the business phone, the home phone is not at the postion layout says.
phone = Replace(phone, "/", "")
phone = Replace(phone, "-", "")
If IsDate(slnin.Substring(341, 10)) = False Then
hire_dt = "99/99/9999"
Else
hire_dt = slnin.Substring(341, 10)
End If
'full_part_time = slnin.Substring(152, 1)
class_indc = slnin.Substring(5163, 1)
oh_srv_yrs = slnin.Substring(4852, 2)
oh_srv_yrs = Replace(oh_srv_yrs, " ", "0")
oh_srv_dys = slnin.Substring(4856, 2)
oh_srv_dys = Replace(oh_srv_dys, " ", "0")
'reg_temp = slnin.Substring(52, 1)
'appt_type = slnin.Substring(5161, 1)
barg_unit = slnin.Substring(320, 4)
oh_bu_flag = slnin.Substring(316, 1)
'union_cd = slnin.Substring(317, 3)
'jobcode = slnin.Substring(27, 6)
business_title = slnin.Substring(33, 10)
emp_status = slnin.Substring(54, 1)
If IsDate(slnin.Substring(1510, 10)) = False Then
pay_period_end = "01/01/1900"
Else
pay_period_end = slnin.Substring(1510, 10)
End If
If pay_period_end = Nothing Then
space = " "
End If

'If IsDate(slnin.Substring(1510, 10)) = False Then

'spacestring = " "
'payflag = False
'Else
'pay_period_end = slnin.Substring(1510, 10)
'payflag = True
'End If
'Response.Write(IsDate(slnin.Substring(1510, 10)))
If IsDate(slnin.Substring(361, 10)) = False Then
spacestring = " "
payflag = False
Else
termination_dt = slnin.Substring(361, 10)
payflag = True
End If
'action = slnin.Substring(68, 3)
'action_reason = slnin.Substring(71, 3)
If IsDate(slnin.Substring(1800, 10)) = False Then
probation_dt = "1/1/1900"
Else
probation_dt = slnin.Substring(1800, 10)
End If

If probation_dt <= "12/18/2006" Then
probation_check = "Y"
End If


slnout = national_id.Trim + semi
slnout += last_name.Trim.PadRight(20, " ") + semi
slnout += first_name.Trim.PadRight(20, " ") + semi
slnout += mid_initial.Trim.PadRight(15, " ") + semi
slnout += suffix.Trim.PadRight(4, " ") + semi
slnout += dept_ID.Trim + semi
slnout += payroll + semi
slnout += address1.PadRight(35, " ") + semi
slnout += city.Trim.PadRight(20, " ") + semi
slnout += state.PadRight(6, " ") + semi
slnout += postal.Trim + semi
slnout += phone.Trim + semi
slnout += hire_dt.ToString("MM/dd/yyyy") + semi
slnout += appt_type + semi
slnout += oh_srv_yrs + semi
slnout += oh_srv_dys + semi
'slnout += full_part_time + semi
slnout += barg_unit.Trim + semi
slnout += oh_bu_flag + semi
slnout += class_indc + semi
slnout += business_title.PadRight(12, " ") + semi
slnout += emp_status + semi ' coded this as the payflag
slnout += probation_check + semi ' coded this as the IPFlag, for probation
If payflag = True Then
slnout += termination_dt.ToString("MM/dd/yyyy") + semi
Else
slnout += spacestring.ToString + semi
End If

slnout += sep_code + semi ' (fyi this is not right we do not have sepcode at the time of conversion was told to leave blank)

'Response.Write(IsDate(pay_period_end))
'If payflag = True Then
slnout += pay_period_end.ToString("MM/dd/yyyy") & vbCrLf
'Else
'slnout += spacestring.ToString & vbCrLf
'End If

'If termflag = True Then
'slnout += termination_dt.ToString("MM/dd/yyyy")
'Else
'slnout += spacestring.ToString
'End If

outputfile += slnout

'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
'Else
'Label1.Text = "Please Choose a Text file only with extension of .txt"
'End If
'Catch ex As Exception
'Response.Write("You did not choose the correct file")
'Response.End()
' End Try


End Sub

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

Public Function isNumeric(ByVal val As String) As Boolean
Dim result As Integer
Return Integer.TryParse(val, result)
End Function
End Class
 
I'm not all here today (sick) so I might not be understanding what you are asking, but pay_period_end is set as a date so it can never be a space.

-I hate Microsoft!
-Forever and always forward.
 
Right kind of, Sorwen they pay_period_end is set to 01/01/1900 if there is no date found in the text file when its reading it. I don't want that, I want it to be set to "" if there is no date found in the text file when its being read. Any Ideas and drink some OJ.
 
1) create a date wrapper class that returns blank on tostring if it is < some arbitrary date

or

2) use oo design and have your employee class expose a getPayPeriod end function do the same thing.

ps: why are you initializing it to 1/1/1900 instead of just letting it be minValue?



Brian Begy
BugSentry - Automatic error reporting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top