Visual Basic Spline Chart with missing value - vb.net

I'm creating in Visual Basic an application in winforms with a Chart control that displays the values taken from different CSV files. After having selected the files, the graph appears as in the figure:
I've noticed that blue vertical lines are created when a value in the series is missing. How can I fix?
This is my code:
Dim openFileDialog1 As New OpenFileDialog With {
.Filter = "CSV files (*.csv)|*.csv",
.FilterIndex = 1,
.Multiselect = True,
.Title = "Select only CSV files"
}
If openFileDialog1.ShowDialog() = DialogResult.OK Then
Chart1.Visible = True
If Chart1.Series.Count > 0 Then
Chart1.Series.Clear()
End If
Dim fileCounter As Integer = 0
For Each file As String In openFileDialog1.FileNames
fileCounter += 1
Dim lineCounter As Integer = 0
Dim sr As New StreamReader(file)
Dim currentLine As String
While sr.Peek() >= 0
currentLine = sr.ReadLine()
lineCounter += 1
If lineCounter = 1 Then
Dim series As New Series With {
.Name = Path.GetFileNameWithoutExtension(file),
.ChartType = SeriesChartType.Spline
}
Chart1.Series.Add(series)
Else
Dim values() As String = currentLine.Split(",")
Dim timers As String = values(0)
Dim speed As Double = Double.Parse(values(1))
Chart1.Series(fileCounter - 1).Points.AddXY(timers, speed)
End If
End While
sr.Close()
Next
Chart1.Series(0).ChartType = SeriesChartType.Spline
End If

The numbers in the series are decimal, but in the American style, i.e. floating point with .
While in Italy we are in the habit of using the ,
I fixed it by changing the culture with the code:
Threading.Thread.CurrentThread.CurrentCulture = New CultureInfo("us-US")
Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator = "."

Related

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

system.io.ioexception the process cannot access because it is being used by another process

i am getting this problem in some systems, some systems working properly, here my code is,
Dim fileName As String = "FaultTypesByMonth.csv"
Using writer As IO.StreamWriter = New IO.StreamWriter(fileName, True, System.Text.Encoding.Default) '------------ rao new ----
Dim Str As String
Dim i As Integer
Dim j As Integer
Dim headertext1(rsTerms.Columns.Count) As String
Dim k As Integer = 0
Dim arrcols As String = Nothing
For Each column As DataColumn In TempTab.Columns
arrcols += column.ColumnName.ToString() + ","c
k += 1
Next
writer.WriteLine(arrcols)
For i = 0 To (TempTab.Rows.Count - 1)
For j = 0 To (TempTab.Columns.Count - 1)
If j = (TempTab.Columns.Count - 1) Then
Str = (TempTab.Rows(i)(j).ToString)
Else
Str = (TempTab.Rows(i)(j).ToString & ",")
End If
writer.Write(Str)
Next
writer.WriteLine()
Next
writer.Close()
writer.Dispose()
End Using
Dim FileToDelete As String = Nothing
Dim sd As New SaveFileDialog
sd.Filter = "CSV Files (*.csv)|*.csv"
sd.FileName = "FaultTypesByMonth"
If sd.ShowDialog = Windows.Forms.DialogResult.OK Then
FileCopy(fileName, sd.FileName)
MsgBox(" File Saved in selected path")
FileToDelete = fileName
If System.IO.File.Exists(FileToDelete) = True Then
System.IO.File.Delete(FileToDelete)
End If
End If
FileToDelete = fileName
If System.IO.File.Exists(FileToDelete) = True Then
System.IO.File.Delete(FileToDelete)
End If
when i am trying to save this file in desired path, then i am getting this error.
if save in shared folder i am not getting this error
system.io.ioexception the process cannot access because it is being used by another process...
what i am doing wrong,Help me

how to use componentone c1printdocument in asp.net for reading text file and convert into pdf with format?

I am using the C1PrintDocument class to read a text file and convert into pdf in web. Actually it converts into pdf but without the format as like in text file.
For eg. I have a text file which has only one page. But it makes into two pages while convert into pdf. Below is the code what I use.
Imports System.IO
Imports C1.C1Preview
Imports C1.C1Pdf
Imports C1.Web.Wijmo.Controls.C1ReportViewer
Imports System.Runtime.Serialization.Formatters.Binary
Imports C1.C1Pdf.PdfDocumentInfo
Imports C1.C1Preview.C1PrintDocument
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
Try
Dim strmappath As String = System.Web.HttpContext.Current.Server.MapPath("~")
Dim filename As String
filename = Path.Combine(Server.MapPath("~"), "12214.txt")
Dim sr As New System.IO.StreamReader(filename, System.Text.Encoding.UTF8)
Dim str As [String] = sr.ReadToEnd()
Dim doc1 As C1PrintDocument = C1ReportViewer.CreateC1PrintDocument()
Dim doc As New C1PrintDocument
doc.Pages(1).PageSettings.TopMargin = 0
doc.Pages(1).PageSettings.LeftMargin = 0
doc.Pages(1).PageSettings.RightMargin = 0
doc.StartDoc()
Dim overlay As New RenderArea()
overlay.Width = "100%"
overlay.Height = "100%"
overlay.Style.Borders.All = LineDef.Default
doc.PageLayout.Overlay = overlay
Dim lines As String() = System.IO.File.ReadAllLines(filename)
Dim linecnt As Integer = System.IO.File.ReadLines(filename).Count
Dim strpages1 As String()
ReDim strpages1(Math.Abs(linecnt / 66) - 1)
Dim intcnt As Integer
Dim intlinereadcnt As Integer
intlinereadcnt = 0
intcnt = 0
intlinereadcnt = 0
For Each line In lines
If intcnt <= 66 Then
If intcnt <> 66 Then
strpages1(intlinereadcnt) = strpages1(intlinereadcnt) &
line & vbCrLf
End If
intcnt = intcnt + 1
ElseIf intcnt = 67 Then
intlinereadcnt = intlinereadcnt + 1
intcnt = 0
End If
Next
Dim strPages As String() = str.Split(Chr(12))
Dim strPage As String = String.Empty
Dim txtPage As New C1.C1Preview.RenderText(doc)
With doc
.AllowNonReflowableDocs = True
For Each strPage In strpages1
If strPage.Length > 0 Then
txtPage.Text = strPage
txtPage.X = 150
doc.Style.TextColor = Drawing.Color.Blue
.RenderBlockText(txtPage.Text)
.Style.WordWrapMode = False
.NewPage()
End If
Next
End With
Dim m_pdfExporter As C1.C1Preview.Export.PdfExporter
m_pdfExporter = C1.C1Preview.Export.ExportProviders.
PdfExportProvider.NewExporter()
m_pdfExporter.Document = doc
m_pdfExporter.Export(Server.MapPath("~") & "12214" & ".pdf")
Dim sr2 As System.IO.MemoryStream
sr2 = New MemoryStream
doc.Save(sr2)
Dim biteArray As Byte() = New Byte(sr2.Length - 1) {}
sr2.Position = 0
sr2.Read(biteArray, 0, CInt(sr2.Length))
'm_pdfExporter.Export(Path.GetTempPath & "\" & "12214" & ".pdf")
Response.Clear()
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/pdf"
'Response.BinaryWrite(biteArray)
Response.AddHeader("Content-Disposition", "attachment;filename=" & "12214" & ".pdf")
Response.BinaryWrite(System.IO.File.ReadAllBytes(Server.MapPath("~") & "12214" & ".pdf"))
Catch ex As Exception
End Try
End Sub
Try using c1PrintDocument1.Export("filename.pdf", true) instead.
Thanks,
Richa

dynamic table and button creation in asp.net

I have created a dynamic table using for loop condition.In which it has a button while i click a specific button it should open a file.But in my coding it is opening a file button in the last row.
If Not IsPostBack Then
txtlogsdate.Text = FormatDate(Now)
End If
Try
trViewlogs.Visible = True
lbllogs.Visible = False
lbllogname.Visible = True
RT1.Visible = False
pb.Visible = False
Dim d1 As DateTime = txtlogsdate.Text
Dim dd As String = d1.ToString("dd")
Dim mm As String = d1.ToString("MM")
Dim yy As String = d1.ToString("yy")
Dim d2 As String = yy & "" & mm & "" & dd
Dim di As DirectoryInfo = New DirectoryInfo(Server.MapPath("~\logs"))
Dim files As FileInfo() = di.GetFiles("*.log")
Dim tab As New Table()
tab.CellPadding = 0
tab.CellSpacing = 0
tab.BorderStyle = BorderStyle.Double
tab.Attributes.Add("style", "margin-left: 0.5px; width: 800px;")
Dim row As New TableRow()
Dim headerCell1 As New TableHeaderCell()
headerCell1.Text = "Logs"
headerCell1.Attributes.Add("style", "margin-left: 0.5px; height: 20px;")
headerCell1.BackColor = System.Drawing.Color.CornflowerBlue
headerCell1.ForeColor = System.Drawing.Color.White
row.Controls.Add(headerCell1)
tab.Controls.Add(row)
Dim headerCell2 = New TableHeaderCell()
headerCell2.Attributes.Add("style", "margin-left: 0.5px; height: 20px;")
headerCell2.BackColor = System.Drawing.Color.CornflowerBlue
headerCell2.ForeColor = System.Drawing.Color.White
headerCell2.Text = "Download"
row.Controls.Add(headerCell2)
tab.Controls.Add(row)
For i As Integer = 0 To files.Length - 1
Dim a As String = files(i).ToString.Replace("Event-", "")
Dim c As String = a.Substring(0, 6)
Dim sw As String
If d2 = c Then
sw = My.Computer.FileSystem.ReadAllText(_
GetWebSitePhysicalRoot & "\logs\" & files(i).ToString)
lbllogname.Text = files(i).ToString
lbllogname.Visible = False
row = New TableRow()
If i Mod 2 = 0 Then
row.BackColor = System.Drawing.Color.White
Else
row.BackColor = System.Drawing.Color.AliceBlue
End If
Dim cell As New TableCell()
cell.Text = lbllogname.Text
'cell.Width = New Unit("1000px")
cell.HorizontalAlign = HorizontalAlign.Center
row.Controls.Add(cell)
Dim cell2 As New TableCell()
Dim bt As New Button
bt.BorderStyle = BorderStyle.Solid
bt.Text = files(i).ToString
AddHandler bt.Click, AddressOf bt_Click
cell2.HorizontalAlign = HorizontalAlign.Center
cell2.Controls.Add(bt)
row.Controls.Add(cell2)
tab.Controls.Add(row)
Panel1.Controls.Add(tab)
End If
Next i
If lbllogname.Text = "" Then
lbllogname.Text = "No Logs to Display !"
End If
Session("pageurl") = ""
Session("pagecount") = ""
ib.SetInfo("Reports > View Logs", Infobar.InfoTypes.Caption)
Dim mi As Integer = GetQueryStringToInt("menuindex", 1)
If Not IsPostBack Then
leftmenu1.AddItem("View Logs", _
GetWebSiteUrlRoot & "/staff_rpt.aspx?rpt=logs&page=1&menuindex=" & mi)
End If
Catch ex As Exception
WriteLog(LogWriter.EventType.eError, ex.StackTrace.ToString)
End Try
Protected Sub bt_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs
Dim a As String
a = lbllogname.Text
Response.ContentType = "text/plain"
Response.AppendHeader("Content-Disposition", "attachment; filename=" & a)
Response.TransmitFile(Server.MapPath("~/logs/" & a))
Response.End()
End Sub
To use the dynamic table structure you have built in your code, then you need to uniquely name each button in each row; otherwise the button click handler (bt_Click) cannot figure out the correct row to open the file in, because they are all called the same and will use the last one.
Since you want a table structure, then I suggest you use the GridView server control, as it will provide similar output, but provide the ability to use templating to name the controls of each row the same, but allow for you to differentiate individual rows when a click event happens.

Reading Text line by line to make string manipulation

So to start this is the code I have already written:
Dim MyFile As String = "Path"
Dim str_new As String
Dim str_old As String = File.ReadAllText(MyFile)
Dim sr As New StreamReader(MyFile)
Dim strLines As String() = Strings.Split(sr.ReadToEnd, Environment.NewLine)
Dim Character As Integer = 5 'Line 1 always has 5 characters
For i = 2 To strLines.Length
Dim PreviousLine As String = sr.ReadLine(i - 1)
Dim CurrentLine As String = sr.ReadLine(i)
If CurrentLine.Contains(TextBox1.Text / 100) Then
If PreviousLine.Contains("divide") Then
Exit For
End If
End If
Character = Character + CurrentLine.Length
Next
sr.Close()
str_new = Replace(str_old, (TextBox1.Text / 100), (TextBox3.Text / 100), Character, 1)
Dim objWriter3 As New System.IO.StreamWriter(MyFile, False)
objWriter3.Write(str_new)
objWriter3.Flush()
objWriter3.Close()
I am trying to figure out a way to break a long code file into lines then check each line for certain strings. If the current line contains the string then I will do additional check on above and/or below lines to make sure This is the correct instance of the string. Finally I want to replace just that instance of the string with a different string.
An example: text file
class
...
0.3
divide <-- Previous Line
0.3 <-- TextBox1.Text is 30
.5
end
I want the code to go past the first instance of 0.3
Find the second instance
Check previous line for divide
Exit Loop
Replace second instance of 0.3 to some value
I have been looking into this for a while now and any help would be greatly appreciated!
~Matt
Revised: Code
Dim MyFile As String = "Path"
Dim NewFile As String = "Temporary Path"
Dim PreviousLine As String = ""
Dim CurrentLine As String = ""
Using sr As StreamReader = New StreamReader(MyFile)
Using sw As StreamWriter = New StreamWriter(NewFile)
CurrentLine = sr.ReadLine
Do While (Not CurrentLine Is Nothing)
Dim LinetoWrite = CurrentLine
If CurrentLine.Contains(TextBox1.Text) Then
If PreviousLine.Contains("divide") Then
LinetoWrite = Replace(CurrentLine, TextBox1.Text, TextBox3.Text)
End If
End If
sw.WriteLine(LinetoWrite)
PreviousLine = CurrentLine
CurrentLine = sr.ReadLine
Loop
End Using
End Using
My.Computer.FileSystem.CopyFile(NewFile, MyFile, True)
You are facing the problem in the wrong way. You have to set both, reader and writer, and generate a new file with all the modifications. Your code has various parts which should be improved; in this answer, I am just showing how to use StreamReader/StreamWriter properly. Sample code:
Dim MyFile As String = "input path"
Dim OutputFile As String = "output path"
Dim PreviousLine As String = ""
Dim CurrentLine As String = ""
Using sr As StreamReader = New StreamReader(MyFile)
Using sw As StreamWriter = New StreamWriter(OutputFile)
CurrentLine = sr.ReadLine
Do While (Not CurrentLine Is Nothing)
Dim lineToWrite = CurrentLine
'Perform the analysis you wish by involving the current line, the previous one and as many other (previous) lines as you wish; and store the changes in lineToWrite. You should call a function here to perform this analysis
sw.WriteLine(lineToWrite) 'Writing lineToWrite to the new file
PreviousLine = CurrentLine 'Future previous line
CurrentLine = sr.ReadLine 'Reading the line for the next iteration
Loop
End Using
End Using