Adding Multiple CSV Fields within a singular ListBox - vb.net

I've been trying to create a leaderboard a little maze game I'm creating and im using a CSV File to store my data. I have two fields per row, something akin to
James, 20
Luke, 34
Which is "playername" and "score".
Whenever i run the code [below], my listbox displays;
James
20
Luke
34
I'm attempting to display it as something such as
James 20
Luke 34
I've tried to store the first field as a string and grab the next field and concatenate the two together to display it on one line, however i was unable to grab the next field as it would result in a recoding of the reading function.
FileIO.TextFieldParser(
"C:\Users\zamks1622\Desktop\Computing\SAT\Leaderboard.csv")
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
ListLeaderboard.Items.Add(currentField)
Next
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
MsgBox("Line " & ex.Message &
"is not valid and will be skipped.")
End Try
End While
End Using

I think this may help
Try
currentRow = MyReader.ReadFields()
ListLeaderboard.Items.Add(Join(currentRow , " "))
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException

Related

txt file to stream reader comma separated individual values

I am wanting to take a txt file that I have that is comma separated. For example:
706202212011417.G001,1024,20221201,172300,3600,35479,....
I am trying to figure out how to make an ArrayList and assign each individual comma separated value as an it's own item put it anywhere in the list.
For example, I want to assign the first comma-separated value to an A1 variable, and then use this variable in a MessageBox: "A1 is ******.***"
I have been able to separate everything out, but I cannot seem to set the array to assign each item to its own variable.
Using MyReader As New Microsoft.VisualBasic.
FileIO.TextFieldParser(
"C:\TestFolder\test.txt")
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
MsgBox(currentField)
Next
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
MsgBox("Line " & ex.Message &
"is not valid and will be skipped.")
End Try
End While
End Using
Look more closely at the currentRow variable. This provides an index variable you can use to target individual values in the row: currentRow(0), currentRow(1), etc.

Is there a way to avoid getting an error when I use one of two textboxes to search from database?

I have two textboxes that I can use to search from database but it is not all the time when I use two textboxes to search.
Sometimes I use one but when i do so I get an error:
"Syntax error missing operator in query expression"
because the other textbox is empty. This only happens when I am using >=.
It seems to want all textboxes to have text.
Sub search()
Try
DataGridRecords.Rows.Clear()
conn.Open()
Dim cmd As New OleDb.OleDbCommand("Select * from tblDies where `IDSIZE` >= " & txtIdSize.Text & " OR `ODSIZE` >= " & txtOdSize.Text & " ", conn)
dr = cmd.ExecuteReader
While dr.Read
DataGridRecords.Rows.Add(dr.Item("ID"), dr.Item("DIENUMBER"), dr.Item("DESCRIPT"), dr.Item("OLDNUMBER"), dr.Item("CODE"), dr.Item("QUANTITY"), dr.Item("IDSIZE"), dr.Item("ODSIZE"), dr.Item("HEIGHT"), dr.Item("FLANGE HT"), dr.Item("FLANGE DIA"), dr.Item("CuRef"), dr.Item("CUTEL"), dr.Item("CuContact"), dr.Item("Price-selling"), dr.Item("P/no"), dr.Item("Stoksize"), dr.Item("Material"), dr.Item("Shore"), dr.Item("DieChkd"), dr.Item("DIECOST"), dr.Item("DATE"), dr.Item("REMARKS"), dr.Item("PRREF"))
End While
dr.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
conn.Close()
End Sub

VB.net Try Catch. Keep the Loop after the Catch

I'm using Try Catch and I want to add into Form3.ListBoxes the items that are into Form2.ListBoxes in other type. But it stop adding after the Catch the exception. So I want to keep the Loop after the exception be caught!
My program get products and show the same products but in another type (Like: I have a T-shirt with a brand, but I want the "same" T-shirt in another brand).
ListBox5 are the quantity that I add in Form1. I load Images to be clearly. Form2 Listboxes are in order (ListBox1,ListBox2...). Form2 and Form3 have the same design.
Dim ConnectionString As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Application.StartupPath & "\Tabela_Precos.xlsx; Extended Properties=Excel 12.0;")
ConnectionString.Open()
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da
For i = 0 To Form1.ListBox1.Items.Count - 1
Dim str As String = Form1.ListBox1.Items(i).ToString
Dim prod As String = str.Substring(2, 3)
da = New OleDbDataAdapter("SELECT * FROM [Mesa$] WHERE Format([Ref], ""000000000"") like '%" & prod & "%'", ConnectionString)
da.Fill(dt)
Try
ListBox1.Items.Add(dt.Rows(i).Item(0))
ListBox2.Items.Add(dt.Rows(i).Item(1))
ListBox3.Items.Add(dt.Rows(i).Item(3))
ListBox4.Items.Add(dt.Rows(i).Item(5))
ListBox5.Items.Add(Form1.ListBox5.Items(i))
ListBox6.Items.Add(ListBox4.Items(i) * ListBox5.Items(i))
Catch ex As Exception
End Try
Next
I need the Try-Catch. I'm doing a Query and if doesn't exist the line in DataBase it stop. How can I keep doing the Loop, after stop?
These are the images with program running (they are edited):
Form1
Form2
Form3
You have answered the question yourself when you say "keep the loop after the catch". You want your loop to continue, resume or move to the next iteration. All of these are VB keywords and I recommend you review For Next Statement to understand how a For loop works.
Here is one of a few ways you can accomplish this.
With no error handling, just ignore and continue:
Try
...
Catch ex As Exception
Continue For
End Try

Return array of ListViewItem to Sub

I want to make a chart with several series on it. Every series belongs to a particular person.
First I read out the number of different persons in my table in Access:
connection.Open()
command.CommandText = "SELECT kontoverlauf_kunden_id FROM kontoverlauf GROUP BY kontoverlauf_kunden_id"
Dim kunden_id_array As New ArrayList()
reader = command.ExecuteReader()
Do While reader.Read()
kunden_id_array.Add(reader("kontoverlauf_kunden_id").ToString())
Loop
reader.Close()
connection.Close()
Now I have the different persons (the ID of them).
Second I add the series i need to the chart:
For counter As Integer = 0 To kunden_id_array.Count - 1
chartKontoverlauf.Series.Add(New Series("Konto [" & kunden_id_array.Item(counter) & "]"))
chartKontoverlauf.Series(counter).ChartType = SeriesChartType.Line
chartKontoverlauf.Series(counter).Color = Color.Red
chartKontoverlauf.Series(counter).BorderWidth = 3
Next
Now I just need to fill in the points I need, but that's kinda tricky.
I've made a function that will call every time and it reads out the values (money and time) for the particular person. Here's the function first:
Function DatenAuslesen(ByVal kunden_id As Integer) As ListViewItem
Dim lstViewItem As New ListViewItem
Try
connection.Open()
command.CommandText = "SELECT * FROM kontoverlauf WHERE kontoverlauf_kunden_id = " & kunden_id
reader = command.ExecuteReader()
Do While reader.Read()
lstViewItem.SubItems.Add(reader("kontoverlauf_wert").ToString())
lstViewItem.SubItems.Add(reader("kontoverlauf_datum").ToString())
Loop
reader.Close()
connection.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
connection.Close()
Return lstViewItem
End Function
To add the points now, I wrote this little thing:
For counter As Integer = 0 To kunden_id_array.Count - 1
For counter_2 As Integer = 0 To DatenAuslesen(kunden_id_array.Item(counter)).ListView.Items.Count - 1
chartKontoverlauf.Series(counter).Points.AddXY(DatenAuslesen(kunden_id_array.Item(counter)).ListView.Items(counter_2).SubItems(0).Text, DatenAuslesen(kunden_id_array.Item(counter)).ListView.Items(counter_2).SubItems(1).Text)
Next
Next
The problem with the thing is that it just gives me one of the values I need back, and not all. I can't access alle values properly. I'm still not sure if it's even the best variable to do this. I would need an array of ListViewItems to add all of them but I didn't found how to do this.
Maybe anyone has an idea?
Thank you!

A loop in a try..catch

Okay, I have a try..catch such that :
Try
Dim dr As OleDbDataReader = performSelect("SELECT * FROM " & table, myConn)
Catch ex As Exception
sheet = InputBox("Re-enter table:")
Dim dr As OleDbDataReader = performSelect("SELECT * FROM " & table, myConn)
End Try
The above will only work for the first instance.
What I want to do is keep displaying the Input box until the dr is a success (i.e. values have been retrieved from the database.
I know I need some sort of while in the catch, but I am unsure how to proceed
Remove the performSelect from inside the catch block, otherwise you could throw an exception there too and it would need to be caught, etc. Put the entire try catch block inside the while loop and exit when you have a value for sheet.
Have a look here why this is not a good idea.
If you are still interested, you can try this:
Dim retries As Integer = 3
While True
Try
Dim sheet as String = InputBox("Enter table:")
Dim dr As OleDbDataReader = performSelect("SELECT * FROM " & sheet, myConn)
Exit While
Catch
retries -= 1
If retries = 0 Then
Throw
Else
System.Threading.Thread.Sleep(1000)
End If
End Try
End While
Change the retries if you want more(infinite-> <=0), change or remove the thread.sleep if you want to retry immediately.
This might work:
Dim inputOk As Boolean = False
Do Until inputOk = True
Try
Dim dr As OleDbDataReader = performSelect("SELECT * FROM " & table, myConn)
inputOk = True
Catch ex As Exception
sheet = InputBox("Re-enter table:")
End Try
End
Loop
Catch block has been designed to handle exceptions, not to execute database queries... You should use Jeff's way. The same I was writing, but he's quicker than I