Getting system outofmemoryexception building string in vb - vb.net

I have a dataset that is with arround 30000records, so loping inside it and building the string fileStavki trows exeption, systemoutofmemmory. How can i solve this. Is working with arrays possible solution or something else?
Dim sb As New StringBuilder
For Each inRow In ds_in.Tables(0).Rows
rbr += 1
pisi_red = "2" & Enc3.prefill(rbr, "0", 6) & Share.RightPrefill(inRow("prioritet"), "0", 5)
pisi_red &= Enc3.prefill(Enc3.IsNull(inRow("smetka_davac"), "0"), "0", 15)
pisi_red &= Share.RightPrefill(Enc3.lat2kir(Enc3.IsNull(inRow("naziv_davac"), "")), " ", 70)
pisi_red &= Share.RightPrefill(Enc3.IsNull(inRow("povik_broj_davac"), ""), " ", 24)
pisi_red &= Share.RightPrefill(Enc3.lat2kir(Enc3.IsNull(inRow("cel_na_doznaka"), "")), " ", 70)
pisi_red &= Enc3.prefill(inRow("sif_tip_plakanje"), "0", 6)
pisi_red &= "+" & Enc3.prefill(FormatNumber(inRow("iznos"), 5, TriState.False, TriState.False, TriState.False), "0", 21)
pisi_red &= inRow("smetka_primac") & Share.RightPrefill(Enc3.lat2kir(Trim(inRow("naziv_primac"))), " ", 70)
pisi_red &= Share.RightPrefill(Enc3.IsNull(inRow("povik_broj_primac"), ""), " ", 24)
pisi_red &= inRow("tip_instrument") & Share.RightPrefill(Enc3.IsNull(inRow("broj_transakcija"), ""), " ", 16)
pisi_red &= inRow("sistem_poravnuva") & Enc3.prefill(Enc3.IsNull(inRow("edb"), ""), "0", 13)
pisi_red &= vbCrLf
sb.Append(pisi_red)
'fileStavki &= pisi_red
Next
after this i have
sw.Write(fileStavki)
sw.Flush()
sw.Close()
I think even if i use stringbuilder the string fileStavki will be build like this
filestavki = sb.ToString
And here i will also get outofmemory exeption

Related

How do I move the position of the player based on user input. (Represented by O). Code I have written So far. Pls answer in vb

Module Program
Dim startx As Integer
Dim starty As Integer
Dim endx As Integer
Dim endy As Integer
Dim maze As String(,) = {
{"x ", "x ", "x ", "x ", "x ", "x ", "x ", "x ", "x ", "x"},
{"O ", " ", " ", " ", " ", " ", " ", " ", " ", "x"},
{"x ", "x ", "x ", "x ", "x ", " ", "x ", "x ", " ", "x"},
{"x ", "x ", " ", "x ", "x ", " ", "x ", "x ", " ", "x"},
{"x ", "x ", " ", " ", " ", " ", "x ", "x ", " ", "x"},
{"x ", "x ", "x ", "x ", "x ", " ", "x ", "x ", "x ", "x"},
{"x ", " ", " ", " ", " ", " ", " ", " ", " ", "x"},
{"x ", " ", "x ", "x ", "x ", "x ", "x ", "x ", " ", "x"},
{"x ", " ", " ", " ", " ", "x ", "x ", "x ", " ", " "},
{"x ", "x ", "x ", "x ", "x ", "x ", "x ", "x ", "x ", "x"}}
Sub Main(args As String())
Console.WriteLine("This is a maze game")
Console.WriteLine("Please enter where you want to go (x and y cordinate)")
start
displaychessboard()
End Sub
Sub displaychessboard()
Console.WriteLine(" 0 1 2 3 4 5 6 7 8 9")
For x As Integer = 0 To 9
Console.Write(x & " ")
For y As Integer = 0 To 9
Console.Write(maze(x, y))
Next
Console.WriteLine()
Next
End Sub
Sub makemove()
End Sub
End Module
How do I move the position of the player based on user input.
You store the position of a player in one or more variables. Since you have a two-dimensional maze, you can use a two-element tuple or, for simplicity, a player_x and a player_y variable.
When the user provides input, you modify the variables accordingly. For example, "moving left" would decrease player_x by one.
When drawing your maze, you look at the current values of player_x and player_y. If they match the current position in your loop, you output "O" instead of a space.
Pls answer in vb
The Stack Overflow community will gladly help you understand concepts and teach you how to improve your Visual Basic skills, but we are not a "here is my task, you write code" service.

2d array of generic length in a class

I have a class that currently works like this.
Class Maze
Public board(10, 10) As Object
Dim maze1 As New Maze With {.board = {{" ", " ", "X", "X", " ", "X", "X", "X", " ", "X"},
{"X", " ", " ", " ", " ", "X", " ", " ", " ", "X"},
{" ", " ", "X", " ", "X", " ", " ", " ", " ", "X"},
{"X", " ", "X", " ", " ", "X", "X", "X", " ", " "},
{"X", " ", "X", "X", " ", " ", " ", " ", "X", " "},
{" ", "X", " ", " ", "X", " ", " ", "X", " ", " "},
{" ", "X", "X", " ", "X", " ", " ", " ", " ", "X"},
{" ", " ", " ", " ", " ", "X", "X", " ", " ", "X"},
{"X", "X", " ", "X", " ", " ", " ", "X", " ", " "},
{" ", " ", " ", "X", "X", " ", " ", " ", "X", " "}}}
However I cant find away to remove the prespecified length without methods such as GetLength now not working.
In the .Net world, if you define a type as Object you're almost always doing something very wrong you will soon come to regret. In this case, it looks like String may be more appropriate. However, perhaps based on how this is used later it might turn out that Integer or a specific class type could be better, alongside a special method to translate this into text or graphics for output.
Moving on to the specific issue of size... consider a List:
Class Maze
Dim board As List(Of List(Of String))
board = New List(Of List(Of string)) From {
New List(Of String) From {" ", " ", "X", "X", " ", "X", "X", "X", " ", "X"},
New List(Of String) From {"X", " ", " ", " ", " ", "X", " ", " ", " ", "X"},
New List(Of String) From {" ", " ", "X", " ", "X", " ", " ", " ", " ", "X"},
New List(Of String) From {"X", " ", "X", " ", " ", "X", "X", "X", " ", " "},
New List(Of String) From {"X", " ", "X", "X", " ", " ", " ", " ", "X", " "},
New List(Of String) From {" ", "X", " ", " ", "X", " ", " ", "X", " ", " "},
New List(Of String) From {" ", "X", "X", " ", "X", " ", " ", " ", " ", "X"},
New List(Of String) From {" ", " ", " ", " ", " ", "X", "X", " ", " ", "X"},
New List(Of String) From {"X", "X", " ", "X", " ", " ", " ", "X", " ", " "},
New List(Of String) From {" ", " ", " ", "X", "X", " ", " ", " ", "X", " "}
}
Now you can add a line to end like so:
board.Add(New List(Of String) From {" ", "X", " ", " ", " " ... })
Or add a column like this:
For Each c As List(Of String) In board
c.Add(" ")
Next
This allows you to have 2d array without specifying length explicitly
Public board(,) As Object = {}

How to get default value of a row?

Actually i'm formatting data to insert in my MySQL DB and i'm at the point where i'm managing the NULL values.
So for manage it i've set a try catch inside the if where i look for datatype and now i have to put in the Catch exception the default value of certain type.
But how can i do it in VB.NET?
Here is my code, any suggestion for code improvment will be grateful
For Each row As DataRow In ds.Tables(0).Rows
righe = ""
For Each column As DataColumn In ds.Tables(0).Columns
If column.DataType = Type.GetType("System.DateTime") Then
Try
righe &= "'" & Format(row.Item(column.ColumnName), "yyyy-MM-dd") & "', "
Catch ex As Exception
End Try
ElseIf column.DataType = Type.GetType("System.TimeSpan") Then
righe &= "'" & Format(row.Item(column.ColumnName).ToString, "HH\:mm\:ss") & "', "
ElseIf column.DataType = Type.GetType("System.Int32") Then
righe &= row.Item(column.ColumnName) & ", "
ElseIf column.DataType = Type.GetType("System.Single") Then
righe &= Replace(row.Item(column.ColumnName), ",", ".") & ", "
ElseIf column.DataType = Type.GetType("System.Byte") Then
righe &= row.Item(column.ColumnName) & ", "
ElseIf column.DataType = Type.GetType("System.Double") Then
righe &= Replace(row.Item(column.ColumnName), ",", ".") & ", "
ElseIf column.DataType = Type.GetType("System.String") Then
righe &= "'" & APICI(row.Item(column.ColumnName)) & "', "
End If
Next
righe = Mid(righe, 1, Len(righe) - 2)
Next

how to check if the data being entered exists already

here is my code to submit data to my csv file. How do I check to see if the data already exists before saving it.
Dim csvFile As String = My.Application.Info.DirectoryPath & "\HoseData.csv"
Dim outFile As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(csvFile, True)
'If My.Computer.FileSystem.FileExists(csvFile) Then
'outFile.WriteLine("job number, sales order number, date, requested by, serial number, hose type, hose size, fitting 1, fitting 2, qty, oal, cut off, offset, crimp 1, crimp 2, cleaned, pigged", False)
'End If
outFile.WriteLine(TextBox1.Text & "," & TextBox2.Text & "," & """" & DateTimePicker1.Text & """" & "," & ComboBox1.Text & "," & TextBox3.Text & "," & ListBox2.Text & "," & ListBox3.Text & "," & TextBox11.Text & "," & TextBox12.Text & "," & NumericUpDown1.TextAlign & "," & TextBox4.Text & "," & TextBox5.Text & "," & TextBox6.Text & "," & TextBox9.Text & "," & TextBox10.Text & "," & ListBox4.Text & "," & ListBox5.Text)
MessageBox.Show("INPUT WAS SAVED")
Dim butt As System.Windows.Forms.Button = DirectCast(sender, System.Windows.Forms.Button)
butt.Enabled = False
'butt.Visible = False
outFile.Close()
Console.WriteLine(My.Computer.FileSystem.ReadAllText(csvFile))
Is this what you're looking for?
Dim fn As String = My.Application.Info.DirectoryPath & "\HoseData.csv"
Dim fileText As String
Dim outString As String = TextBox1.Text & "," & TextBox2.Text & "," & """" & DateTimePicker1.Text & """" & "," & ComboBox1.Text & "," & TextBox3.Text & "," & ListBox2.Text & "," & ListBox3.Text & "," & TextBox11.Text & "," & TextBox12.Text & "," & NumericUpDown1.TextAlign & "," & TextBox4.Text & "," & TextBox5.Text & "," & TextBox6.Text & "," & TextBox9.Text & "," & TextBox10.Text & "," & ListBox4.Text & "," & ListBox5.Text
Using reader As StreamReader = File.OpenText(fn)
fileText = reader.ReadToEnd
End Using
Using outFile As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(fn, True)
If fileText.Contains(outString) Then
'do stuff when the string is already present, e.g.:
MessageBox.Show("You've already entered that one.")
Else
'do stuff when it isn't, e.g.:
outFile.WriteLine(outString)
End If
End Using

Extra Line Created Import File

An extra line is being created in my import file which is fed in from another CSV and is going to break my import file. How can I remove the extra line that was created? Thanks!
the code I use to create the file is follows
Friend Function ImportFile(ByVal filename As String) As infoEDIProduct()
Dim retVal() As infoEDIProduct
Try
Dim actualFileName As String = IO.Path.GetFileName(filename)
Catch e1 As IO.IOException
MsgBox("File is already open please check file.")
End Try
Dim streamReader As New IO.StreamReader(filename)
Dim streamWriter As New IO.StreamWriter(filename & ".csv")
streamWriter.AutoFlush = True
streamWriter.WriteLine(TabDeliminatedHeaders)
Dim lineIn As String = ""
Do While Not streamReader.EndOfStream
retVal = Resize(retVal)
retVal(retVal.Length - 1) = TransformToEDIProduct(streamReader)
streamWriter.Write(TransformEDIproductIntoTabDeliminated(retVal(retVal.Length - 1)))
Loop
streamWriter.Close()
streamWriter.Dispose()
streamReader.Close()
streamReader.Dispose()
Return retVal
Private Function TransformEDIproductIntoTabDeliminated(ByVal EDIproduct As infoEDIProduct) As String
Dim retVal As String = ""
With EDIproduct
retVal = .lineNumber & vbTab
retVal &= .UPCcode & vbTab
retVal &= .sketchersStyleNumber & vbTab
retVal &= .colourDescription & vbTab
retVal &= .size & vbTab
retVal &= .sketchersDivisionDescription & vbTab
retVal &= .sketchersColourCode & vbTab
retVal &= .sketchersDivisionCode & vbTab
retVal &= .department & vbTab
retVal &= .subDepartment & vbTab
retVal &= .gender & vbTab
retVal &= .productShortDescription & vbTab
retVal &= .productDescription & vbTab
retVal &= .costPrice & vbTab
retVal &= .retailPrice & vbTab
retVal &= .DiscountPrice & vbTab
retVal &= .GeminiDepartmentId
End With
Return retVal
End Function
Private Function TransformProductToEdi(ByVal EDIproduct As infoEDIProduct) As String
Dim retVal As String = ""
With EDIproduct
retVal = .lineNumber & vbCrLf
retVal &= .UPCcode & vbCrLf
retVal &= .sketchersStyleNumber & vbCrLf
retVal &= .colourDescription & vbCrLf
retVal &= .size & vbCrLf
retVal &= .sketchersDivisionDescription & vbCrLf
retVal &= .sketchersColourCode & vbCrLf
retVal &= .sketchersDivisionCode & vbCrLf
retVal &= .department & vbCrLf
retVal &= .subDepartment & vbCrLf
retVal &= .gender & vbCrLf
retVal &= .productShortDescription & vbCrLf
retVal &= .productDescription & vbCrLf
retVal &= .costPrice & vbCrLf
retVal &= .retailPrice & vbCrLf
retVal &= .DiscountPrice & vbCrLf
retVal &= .GeminiDepartmentId & vbCrLf
End With
Return retVal
End Function
the issue is you have to remove the last vbCrLf produced by TransformProductToEdi for the last entry of the array retval
the last line that produce the vbCrLf in TransformProductToEdi is
retVal &= .GeminiDepartmentId & vbCrLf