[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

CSV QUestion in VB.net

I could really use some help with this code here.  I have code that I was thought to only export 1 line with header than later found out that I need a number of them.  

The text coming in is less than cleaned up and I was able to do 1 line and strip all the characters out.

I am having a problem just getting it to loop through x amount of lines.


            ''Dim filePathcopy As String = Server.MapPath("hcfilesbk/Post" & "-" & DateTime.Now.ToString("MM-dd-yy-HH-mm") & ".csv")

            ''Dim delimiterCopy As String = ","
            ''Dim TheJobDescriptioncopy As String
            ''TheJobDescriptioncopy = (Replace(JobDescription.Text, "•", "<li>"))
            ''TheJobDescriptioncopy = (Replace(JobDescription.Text, "•", "<li>"))
            ''TheJobDescriptioncopy = (Replace(JobDescription.Text, "’", "&quot;"))
            ''TheJobDescriptioncopy = (Replace(JobDescription.Text, vbCrLf, ""))

            ''Dim TheBenefitsRequirementscopy As String
            ''TheBenefitsRequirementscopy = (Replace(BenefitsRequirements.Text, "•", "<li>"))
            ''TheBenefitsRequirementscopy = (Replace(BenefitsRequirements.Text, "•", "<li>"))
            ''TheBenefitsRequirementscopy = (Replace(BenefitsRequirements.Text, "’", "&quot;"))
            ''TheBenefitsRequirementscopy = (Replace(BenefitsRequirements.Text, vbCrLf, ""))

            ''Dim TheCompanyOverviewcopy As String
            ''TheCompanyOverviewcopy = (Replace(CompanyOverview.Text, "•", "<li>"))
            ''TheCompanyOverviewcopy = (Replace(CompanyOverview.Text, "•", "<li>"))
            ''TheCompanyOverviewcopy = (Replace(CompanyOverview.Text, "’", "&quot;"))
            ''TheCompanyOverviewcopy = (Replace(CompanyOverview.Text, vbCrLf, ""))


            ''Dim outputcopy As String()() = New String()() _
            ''{New String() {"SR_ID", "SR_Name", "SR_Name_Show", "SR_Address", "SR_City", "SR_State", "SR_zip", "SR_Phone", "SR_Phone_Show", "SR_Fax", "SR_Fax_Show", "SR_Email", "SR_JobID", "SR_OpeningsID", "SR_JobTitle", "JobTypeCode", "SR_MCA_Other_Text", "SR_SalaryRangeBase", "SR_SalaryRangeHigh", "SR_LocName", "SR_City1", "SR_State1", "SR_Zip1", "SR_PosReqs", "PositionSummary", "SR_Date", "SR_Discipline_ID", "SR_Job_Status"}, _
            '' New String() {(Request.QueryString("ID")), (Replace(FullName.Text, ",", "&#44;")), _
            '' (SR_Name_Show.Text), (Replace(SR_Address.Text, ",", "&#44;")), (Replace(SR_City.Text, ",", "&#44;")), _
            '' (selStateCode.Text), (SR_Zip.Text), (Replace(SR_Phone.Text, ",", "&#44;")), (SR_Phone_Show.Text), _
            '' (Replace(SR_Fax.Text, ",", "&#44;")), (SR_Fax_Show.Text), (Replace(email.Text, ",", "&#44;")), _
            '' (SR_JobID.Text), (Replace(PrimaryIndustry.Text, ",", "&#44;")), (Replace(JobTitle.Text, ",", "&#44;")), _
            '' (JobTypeCode.Text), (Replace(TheCompanyOverview, ",", "&#44;")), (Replace(BasePayL.Text, ",", "&#44;")), _
            '' (Replace(BasePayH.Text, ",", "&#44;")), (Country.Text), (Replace(City.Text, ",", "&#44;")), (State.Text), _
            '' (Replace(USZip5.Text, ",", "&#44;")), (Replace(TheBenefitsRequirements, ",", "&#44;")), _
            '' (Replace(TheJobDescription, ",", "&#44;")), (Replace(SR_Date.Text, ",", "&#44;")), _
            '' (SR_Job_Status.Text)}}

            ''Dim lengthc As Integer = outputcopy.GetLength(0)
            ''Dim sbc As New StringBuilder()

            ''For index As Integer = 0 To length - 1
            ''    sbc.AppendLine(String.Join(delimiterCopy, outputcopy(index)))
            ''Next

            ''File.WriteAllText(filePathcopy, sbc.ToString())


            'objCmd = New System.Data.OleDb.OleDbCommand()
            'With objCmd
            '    .Connection = objConn
            '    .CommandType = CommandType.Text
            '    .CommandText = strSQL
            'End With

            'objCmd.ExecuteNonQuery()

            'Next
0
tjpal
Asked:
tjpal
  • 2
1 Solution
 
tjpalAuthor Commented:
a claner description is :
I'm just trying to get it to loop all records and not loop the header I resear I have researched many ways of exporting, but this was the only way I could find to get the result I needed.
 


output = New String()() _
            '{New String() {"SR_ID", "SR_Name", "SR_Name_Show", "SR_Address", "SR_City", "SR_State", "SR_zip", "SR_Phone", "SR_Phone_Show", "SR_Fax", "SR_Fax_Show", "SR_Email", "SR_JobID", "SR_OpeningsID", "SR_JobTitle", "JobTypeCode", "SR_MCA_Other_Text", "SR_SalaryRangeBase", "SR_SalaryRangeHigh", "SR_LocName", "SR_City1", "SR_State1", "SR_Zip1", "SR_PosReqs", "PositionSummary", "SR_Date", "SR_Job_Status"}, _
            ' New String() {(Request.QueryString("ID")), (Replace(FullName.Text, ",", "&#44;")), _
            ' (SR_Name_Show.Text), (Replace(SR_Address.Text, ",", "&#44;")), (Replace(SR_City.Text, ",", "&#44;")), _
            ' (selStateCode.Text), (SR_Zip.Text), (Replace(SR_Phone.Text, ",", "&#44;")), (SR_Phone_Show.Text), _
            ' (Replace(SR_Fax.Text, ",", "&#44;")), (SR_Fax_Show.Text), (Replace(email.Text, ",", "&#44;")), _
            ' (SR_JobID.Text), (Replace(PrimaryIndustry.Text, ",", "&#44;")), (Replace(JobTitle.Text, ",", "&#44;")), _
            ' (JobTypeCode.Text), (Replace(TheCompanyOverview, ",", "&#44;")), (Replace(BasePayL.Text, ",", "&#44;")), _
            ' (Replace(BasePayH.Text, ",", "&#44;")), (Country.Text), (Replace(City.Text, ",", "&#44;")), (State.Text), _
            ' (Replace(USZip5.Text, ",", "&#44;")), (Replace(TheBenefitsRequirements, ",", "&#44;")), _
            ' (Replace(TheJobDescription, ",", "&#44;")), (Replace(SR_Date.Text, ",", "&#44;")), _
            ' (SR_Job_Status.Text)}}
            ''(Replace(longtext, ",", "&#44;", "'", "&#39;"))
            'Dim length As Integer = output.GetLength(0)
            'Dim sb As New StringBuilder()

            'For index As Integer = 0 To length - 1
            '    sb.AppendLine(String.Join(delimiter, output(index)))
            'Next




            'File.WriteAllText(thefilePath, sb.ToString())
0
 
Paul_Harris_FusionCommented:
Not sure where to start...this is not how I would approach the problem.   However,  i know it is irritating to get told to do a complete re-write so I will try to help with the code you provide...

The technique of putting everyting into an array of string arrays is not very will suited to looping.   Looping implies that you would extend the master array dynamically and arrays are not great for this.

You could declare output as List (of string()) instead which will allow you to add values to the list.

So, try the following.....

Create a function BuildHeaderArray to return a string array containing column names
   -- basically split off the first part of the code you show above

Create a function BuildValueArray to return a string array containing then next set of field values
   -- it is not obvious from your sample code how the looping will work and what the muilti-line data source is -   you will have to research this yourself. show above

Then use the following pattern

dim outputs as new List (of string())
dim header() as string = BuildHeaderArray()
outputs.add(header)

do while (some condition)
    outputs.add( BuildValueArray()  )
loop

dim sb as new system.text.stringbuilder
For index as integer = 0 to outputs.count
            sb.AppendLine(String.Join(delimiter, output(index)))
Next

0
 
tjpalAuthor Commented:
I was hoping to use what I had and though I was just missing a loop around it.  I'm only gfetting 1 row and the header.  I'd like to keep the header and loop my rows,   but this is very helpful
0
 
Amandeep Singh BhullarCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now