peebman2000
Programmer
Hello again, again I’m a newbie and I’m still working on the same project and now I have a deadline to meet of December 18th. Below is my code, I’m trying select a text file from a directory and convert it into a different formatted text file and uploaded it to the server in a different text file name. Just to give you an explanation of my code, under button1 I have declared my variables of what fields of data I want from the selected text file. Then if the fileupload has the file I selected I want it to open the text file and then read each line, then I want it to pull out the characters of data I only want from that text file. Then I want it to write all of the text that I only want from the text file to a folder on the server under the new file name of “newfile.txt”. For some reason it will not write the new text out nor will it upload it to the server folder under newfile.txt. Does anyone know why? I know its something small, but I can’t see it, can someone please show me what I’m missing. Any additional code would be helpful also. I appreciate it thank you.
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
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 mid_initial As String = ""
Dim address1 As String = ""
Dim first_name As String = ""
Dim city As String = ""
Dim state As String = ""
Dim postal As String = ""
Dim phone As String = ""
Dim hire_dt As String = ""
Dim business_title As String = ""
Dim space As String = ""
space = " "
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)
mid_initial = slnin.Substring(1720, 8)
first_name = slnin.Substring(1685, 15)
address1 = slnin.Substring(1193, 25)
city = slnin.Substring(1305, 13)
state = slnin.Substring(1365, 3)
postal = slnin.Substring(1372, 11)
phone = slnin.Substring(548, 23)
hire_dt = slnin.Substring(188, 21)
business_title = slnin.Substring(33, 18)
'last_name = last_name.PadRight(25)
'build line to be wrote out
'slnout = deptid
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 + semi
slnout += business_title + semi
outputfile += slnout & vbCrLf
End If
End While
'Response.Write(slnout)
'Response.End()
Dim fileout As String = "\newfile" & ".txt"
Try '& FileUpload1.Filename)
File.WriteAllText(Server.MapPath("uploads\") & fileout, outputfile)
Label1.Text = "File uploaded to server" '& FileUpload1.PostedFile.FileName
Catch ex As Exception
''FileUpload1.SaveAs(Server.MapPath("uploads\" & FileUpload1.FileName))
Label1.Text = "Failed because <br/>" & ex.Message
End Try
sr.Close()
'objstreamreader.Close()
Console.ReadLine()
Else
Label1.Visible = True
Label1.Text = "Please select a file to upload"
End If
End Sub
End Class
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
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 mid_initial As String = ""
Dim address1 As String = ""
Dim first_name As String = ""
Dim city As String = ""
Dim state As String = ""
Dim postal As String = ""
Dim phone As String = ""
Dim hire_dt As String = ""
Dim business_title As String = ""
Dim space As String = ""
space = " "
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)
mid_initial = slnin.Substring(1720, 8)
first_name = slnin.Substring(1685, 15)
address1 = slnin.Substring(1193, 25)
city = slnin.Substring(1305, 13)
state = slnin.Substring(1365, 3)
postal = slnin.Substring(1372, 11)
phone = slnin.Substring(548, 23)
hire_dt = slnin.Substring(188, 21)
business_title = slnin.Substring(33, 18)
'last_name = last_name.PadRight(25)
'build line to be wrote out
'slnout = deptid
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 + semi
slnout += business_title + semi
outputfile += slnout & vbCrLf
End If
End While
'Response.Write(slnout)
'Response.End()
Dim fileout As String = "\newfile" & ".txt"
Try '& FileUpload1.Filename)
File.WriteAllText(Server.MapPath("uploads\") & fileout, outputfile)
Label1.Text = "File uploaded to server" '& FileUpload1.PostedFile.FileName
Catch ex As Exception
''FileUpload1.SaveAs(Server.MapPath("uploads\" & FileUpload1.FileName))
Label1.Text = "Failed because <br/>" & ex.Message
End Try
sr.Close()
'objstreamreader.Close()
Console.ReadLine()
Else
Label1.Visible = True
Label1.Text = "Please select a file to upload"
End If
End Sub
End Class