Export to csv in debug mode success but in run-time mode it fails - sql

I am reading records from a SQL Server table into a dataset and then exporting it to a CSV file. When I step through it in debug mode, everything works fine. When I run it, it exports just one cell with crap data. Since it is an ASP.NET application I am not sure if I am doing something wrong regarding postbacks. It seems in run-time, it doesn't wait for the CSV file to be written.
Dim sbldRecordCSV As New StringBuilder
Dim dtHCMTable As DataTable = mdsHCMTable.Tables(0)
Dim intIndex As Integer = 0
'Read the connection-string from web.config
Dim connHCM As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("connHCM").ConnectionString)
'Read the exported CSV file path from web.config
Dim strCSVpath As String = System.Configuration.ConfigurationManager.AppSettings("CSVpath").ToString
Try
For intIndex = 0 To mdsHCMTable.Tables(0).Rows.Count - 1
Dim drHCMTable As DataRow = dtHCMTable.Rows(intIndex)
For Each field As Object In drHCMTable.ItemArray
sbldRecordCSV.Append(field.ToString & "|")
Next
sbldRecordCSV.Replace("|", vbNewLine, sbldRecordCSV.Length - 1, 1)
Next
My.Computer.FileSystem.WriteAllText(strCSVpath.ToString & lstDataTables.SelectedValue.ToString & ".csv", sbldRecordCSV.ToString, False)

After some searching around I realized that I neede to modify the ExportCSV code. The modified routine is as follows:
Protected Sub ExportToCSV(mdsHCMTable As DataSet)
Dim dt As DataTable = mdsHCMTable.Tables(0)
'Read the exported CSV file path from web.config
Dim strCSVpath As String = System.Configuration.ConfigurationManager.AppSettings("CSVpath").ToString
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=" & lstDataTables.SelectedValue.ToString & ".csv")
Response.Charset = ""
Response.ContentType = "application/text"
Dim sb As New StringBuilder()
For k As Integer = 0 To dt.Columns.Count - 1
'add separator
sb.Append(dt.Columns(k).ColumnName + "|"c)
Next
'append new line
sb.Append(vbCr & vbLf)
For i As Integer = 0 To dt.Rows.Count - 1
For k As Integer = 0 To dt.Columns.Count - 1
'add separator
sb.Append(dt.Rows(i)(k).ToString().Replace(",", "|") + "|"c)
Next
'append new line
sb.Append(vbCr & vbLf)
Next
Response.Clear()
Response.BufferOutput = False
Response.Output.Write(sb.ToString())
Response.Flush()
End Sub
This solution worked for me. I hope it can be helpful to others.

Related

Skip the first line of the CSV file (Headers) Visual Basic

Like many on here, I am new to programming and mainly focus on web development. I have written a program cobbled together from help on here that works perfectly. I take a CSV file and inject it into an SQL database. I am getting a "MalformedLineException" line exception on the last line of the CSV file and believe it is because the header line is not being skipped.
Would love some help on working out how to skip the first line from my code below:
Private Sub subProcessFile(ByVal strFileName As String)
'This is the file location for the CSV File
Using TextFileReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(strFileName)
'removing the delimiter
TextFileReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
TextFileReader.SetDelimiters(",")
ProgressBar1.Value = 0
Application.DoEvents()
'variables
Dim TextFileTable As DataTable = Nothing
Dim Column As DataColumn
Dim Row As DataRow
Dim UpperBound As Int32
Dim ColumnCount As Int32
Dim CurrentRow As String()
'Loop To read in data from CSV
While Not TextFileReader.EndOfData
Try
CurrentRow = TextFileReader.ReadFields()
If Not CurrentRow Is Nothing Then
''# Check if DataTable has been created
If TextFileTable Is Nothing Then
TextFileTable = New DataTable("TextFileTable")
''# Get number of columns
UpperBound = CurrentRow.GetUpperBound(0)
''# Create new DataTable
For ColumnCount = 0 To UpperBound
Column = New DataColumn()
Column.DataType = System.Type.GetType("System.String")
Column.ColumnName = "Column" & ColumnCount
Column.Caption = "Column" & ColumnCount
Column.ReadOnly = True
Column.Unique = False
TextFileTable.Columns.Add(Column)
ProgressBar1.Value = 25
Application.DoEvents()
Next
clsDeletePipeLineData.main()
End If
Row = TextFileTable.NewRow
'Dim Rownum As Double = Row
'If Rownum >= 1715 Then
' MsgBox(Row)
'End If
For ColumnCount = 0 To UpperBound
Row("Column" & ColumnCount) = CurrentRow(ColumnCount).ToString
Next
TextFileTable.Rows.Add(Row)
clsInsertPipeLineData.main(CurrentRow(0).ToString, CurrentRow(1).ToString, CurrentRow(2).ToString, CurrentRow(3).ToString, CurrentRow(4).ToString, CurrentRow(5).ToString, CurrentRow(6).ToString, CurrentRow(7).ToString, CurrentRow(9).ToString)
ProgressBar1.Value = 50
Application.DoEvents()
End If
Catch ex As _
Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message &
"is not valid and will be skipped.")
End Try
End While
ProgressBar1.Value = 100
Application.DoEvents()
clsMailConfirmation.main()
TextFileReader.Dispose()
MessageBox.Show("The process has been completed successfully")
End Using
"MalformedLineException" says that Line cannot be parsed using the current Delimiters, to fix it, adjust Delimiters so the line can be parsed correctly, or insert exception-handling code in order to handle the line.
Someone encountered similar question, maybe its reply can help you.

vb.net how do i add long text into csv

hello this is my firs thread ,
i'm trying to convert description of this page (https://www.tokopedia.com/indoislamicstore/cream-zaitun-arofah)
with regex and replace <br/> tag with new line and convert it to csv .
the datagridview it's alright but the csv got screwed
this is my code :
Dim dskrip As New System.Text.RegularExpressions.Regex("<p itemprop=""description"" class=""mt-20"">(.*?)\<\/p>\<\/div>")
Dim dskripm As MatchCollection = dskrip.Matches(rssourcecode0)
For Each itemdskrm As Match In dskripm
getdeskripsinew = itemdskrm.Groups(1).Value
Next
Dim deskripsinew As String = Replace(getdeskripsinew, ",", ";")
Dim deskripsitotal As String = Replace(deskripsinew, "<br/>", Environment.NewLine)
' ListView1.s = Environment.NewLine & deskripsinew
txtDeskripsi.Text = deskripsitotal
datascrapes.ColumnCount = 5
datascrapes.Columns(0).Name = "Title"
datascrapes.Columns(1).Name = "Price"
datascrapes.Columns(2).Name = "Deskripsi"
datascrapes.Columns(3).Name = "Gambar"
datascrapes.Columns(4).Name = "Total Produk"
Dim row As String() = New String() {getname, totalprice, deskripsitotal, directoryme + getfilename, "10"}
datascrapes.Rows.Add(row)
Dim filePath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\" & "Tokopedia_Upload.csv"
Dim delimeter As String = ","
Dim sb As New StringBuilder
For i As Integer = 0 To datascrapes.Rows.Count - 1
Dim array As String() = New String(datascrapes.Columns.Count - 1) {}
If i.Equals(0) Then
For j As Integer = 0 To datascrapes.Columns.Count - 1
array(j) = datascrapes.Columns(j).HeaderText
Next
sb.AppendLine(String.Join(delimeter, array))
End If
For j As Integer = 0 To datascrapes.Columns.Count - 1
If Not datascrapes.Rows(i).IsNewRow Then
array(j) = datascrapes(j, i).Value.ToString
End If
Next
If Not datascrapes.Rows(i).IsNewRow Then
sb.AppendLine(String.Join(delimeter, array))
End If
Next
File.WriteAllText(filePath, sb.ToString)
this is the csv file
I'm not sure where your problem is looking at the CSV file, but there are certain cases where you'll want to quote the values for a CSV. There's no official spec but RFC 4180 is often used as an unofficial standard. I would recommend using a library like CSV Helper

IDE thinks I need to close a loop but it looks fine to me

IDE says the Next for the
for i
right below the "Loop through folders commentis missing.
Here is my code.
Dim strVal As String
'Loop through Folders
For i As Integer = 0 To lbFolder.Items.Count - 1
Dim iText As String = CStr(lbFolder.Items(i))
Dim partPath As String = lblPath.Text + "\" + iText
Dim pathNum As String = partPath + "\1900\"
Dim directory As New DirectoryInfo(pathNum)
Dim fileArr As FileInfo() = directory.GetFiles() ' Get a reference to each file in that directory.
' Display the names of the files.
Dim xItem As FileInfo
'loop through files
For Each xItem In fileArr ' add to listbox
lblFname.Text = xItem.ToString
strVal = pathNum & xItem.ToString
lbFiles.Items.Add(strVal)
Next
For j = 0 To lbFiles.Items.Count - 1 'read through files listbox
Dim FileID, Sequence, Time, Lat, Longitude, Average, Channel As String
'declaration of filestream to open file
Dim sr As StreamReader
'filestream object to open and read file
Dim fs As FileStream
Try
'clearing listbox data toavoid confusion
ListView1.Items.Clear()
fs = New FileStream((lbFiles.Items.Item(j)), FileMode.OpenOrCreate)
'stream reader object to read streamed input
sr = New StreamReader(fs)
Dim itm As Object
'reading line by line
itm = sr.ReadLine
While Not itm = Nothing
Dim split As String() = itm.Split(New [Char]() {","})
FileID = split(0)
Sequence = split(1)
Time = split(2)
Lat = split(3)
Longitude = split(4)
Average = split(5)
Channel = split(6)
With ListView1
.Items.Add(FileID)
.Items(ListView1.Items.Count - 1).SubItems.Add(Sequence)
.Items(ListView1.Items.Count - 1).SubItems.Add(Time)
.Items(ListView1.Items.Count - 1).SubItems.Add(Lat)
.Items(ListView1.Items.Count - 1).SubItems.Add(Longitude)
.Items(ListView1.Items.Count - 1).SubItems.Add(Average)
.Items(ListView1.Items.Count - 1).SubItems.Add(Channel)
End With
itm = sr.ReadLine
End While
'close stream reader and filestrema object
sr.Close()
fs.Close()
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message, "Load Tool Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Next j
lbFiles.Items.Clear()
Next i
End Sub
I am using visual studio, when I click on the for i it shows the closing next but when I run I get an error.
What am I missing?
Add **xItem** after the Next
For Each xItem In fileArr ' add to listbox
lblFname.Text = xItem.ToString
strVal = pathNum & xItem.ToString
lbFiles.Items.Add(strVal)
Next xItem
Restarted the IDE and everything ran with no errors.
Thanks all who responded

SQL Table in CSV file shows NULL Rows as " " in Excel

Basically, I am displaying an SQL table in my VB.NET gridview, once its in my gridview, I then have a button that exports the results from visual studio into a CSV file, Everything in the CSV file looks fine, except any cell that is NULL in the SQL table now displays as:
I tried to fix this in a stored procedure using the code bellow:
UPDATE EmailManagementTable
SET Excluded = ''
WHERE Excluded IS NULL
But even with this code I still get the same problem.
This is my Code for the Button that exports the gridview results to CSV:
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=gvtocsv.csv")
Response.Charset = ""
Response.ContentType = "application/text"
Dim sBuilder As StringBuilder = New System.Text.StringBuilder()
For index As Integer = 0 To GridView1.Columns.Count - 1
sBuilder.Append(GridView1.Columns(index).HeaderText + ","c)
Next
sBuilder.Append(vbCr & vbLf)
For i As Integer = 0 To GridView1.Rows.Count - 1
For k As Integer = 0 To GridView1.HeaderRow.Cells.Count - 1
sBuilder.Append(GridView1.Rows(i).Cells(k).Text.Replace(",", "") + ",")
Next
sBuilder.Append(vbCr & vbLf)
Next
Response.Output.Write(sBuilder.ToString())
Response.Flush()
Response.[End]()
I was wondering if I could get some assistance with this?
Thankyou in advance
I solved my problem!
I edited some code inside my Export Button, Here's the code:
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=gvtocsv.csv")
Response.Charset = ""
Response.ContentType = "application/text"
Dim sBuilder As StringBuilder = New System.Text.StringBuilder()
For index As Integer = 0 To GridView1.Columns.Count - 1
sBuilder.Append(GridView1.Columns(index).HeaderText + ","c)
Next
sBuilder.Append(vbCr & vbLf)
For i As Integer = 0 To GridView1.Rows.Count - 1
For k As Integer = 0 To GridView1.HeaderRow.Cells.Count - 1
sBuilder.Append(GridView1.Rows(i).Cells(k).Text.Replace(",", "").Replace(" ", "") + ",")
Next
sBuilder.Append(vbCr & vbLf)
Next
Response.Output.Write(sBuilder.ToString())
Response.Flush()
Response.[End]()

Can't write to files properly in VB.NET

I am trying to make a program that writes the binary code of a file in a separate text file.
When I use this program on a text file, it doesn't write anything in the new file. I then tested this for .jpg and .mp3 files and the program seems to write most of the binary code but leaves out the last couple of bytes. Here is my code:
Sub Main()
Console.Write("Filename: ")
Dim Filename As String = Console.ReadLine()
Console.Write("Extension: ")
Dim Extension As String = Console.ReadLine()
Console.WriteLine()
Dim Stream_1 As FileStream = New FileStream(Filename & "." & Extension, FileMode.Open)
Dim Stream_2 As FileStream = New FileStream(Filename & "_b.txt", FileMode.Create)
Dim Reader_1 As BinaryReader = New BinaryReader(Stream_1)
Dim Writer_2 As StreamWriter = New StreamWriter(Stream_2)
Dim File_Bytes() As Byte = Reader_1.ReadBytes(Convert.ToInt32(Stream_1.Length))
Dim Binary_String As String = ""
'These are used to a add line break after every 8 bytes
Dim Binary_String_Collection As String = ""
Dim Counter As Integer
For Each File_Byte In File_Bytes
Counter += 1
Binary_String = Convert.ToString(File_Byte, 2)
For I = 1 To 8 - Binary_String.Length
Binary_String = "0" & Binary_String
Next
Binary_String_Collection = Binary_String_Collection & Binary_String & " "
If Counter = 8 Then
Writer_2.WriteLine(Binary_String_Collection)
Counter = 0
Binary_String_Collection = ""
End If
Next
If Binary_String_Collection <> "" Then
Writer_2.WriteLine(Binary_String_Collection)
End If
Console.ReadLine()
End Sub
At first I thought that my program wasn't reading the binary code properly so I added console outputs at locations where it writes to the file. The program displayed correct output so I'm confused why it isn't writing properly.
Make sure you close the file and Dispose of the streams correctly.