VB.NET 2008: Import CSV & Extract Only Certain Named Columns - vb.net

I'm using VB.NET 2008 and what I'm trying to do is very simple, yet I can't seem to figure it out.
I have an CSV file with around 200 columns and 50,000 rows that has the column headers in the first row. I need to read this file and export three named columns (Sample ID No, Analysis Date, P Count) to a tab delimited .TXT file. The columns may be in different orders depending on the machine that creates the original CSV, so I definitely need it to be exported by named column.
I can do this all day in console C with my eyes closed; however, my boss knows that I'm leaving in a few months and is asking that I do this in VB.NET so another guy can make tweaks in the future. I've spent several days looking through this site and others trying to find a solution. I've managed to get the CSV into a DataGridView (albeit without column headers, ugh), but I can't figure out how to get a data out of these. I'm just not used to this OOP stuff to be honest. If anyone would be willing to assist with this or even steer me in the right direction, I would be very grateful.

Not free, but almost ($5, free trail) - I user Kellerman Software's .Net CSV Reports.
Sample downloading a CSV from the web, works for local files to:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Using client As New WebClient ' Imports System.Net needed
' Download the web page as a string.
Dim value As String = client.DownloadString("http://www.jotform.com/csv/##&*2131238*((")
Dim rdr As New CsvReader ' Imports KellermanSoftware.CsvReports needed
Dim dt As DataTable = rdr.CsvStringToDataTable(value)
dgv1.DataSource = dt
End Using
End Sub
Once the data is in a DataTable you'll have the named columns assuming the source has headers.
Note that this is licensed software and would require an included dll - I'm sure there are open source versions out there somewhere. I use this type of software for short term tasks, not great for production unless you buy the source and compile it in.

Related

Extract file using SevenZip

I'm trying to add a file unzipper to my application, so I googled a little and stumbled on the sevenzipsharp library that is able to extract the most common archive formats.
So I for testing I created a simple application with a windows form.
So the entered data is the file location C:\Users\jeee\Desktop\CriticalSubPrintout.rar and the extract location C:\Users\jeee\Desktop\Test Extract
I added some code, without any documentation.. not my strong side apparently..
Imports SevenZip
Public Class Archiver
Private Sub btnExtractArchive_Click(sender As Object, e As EventArgs) Handles btnExtractArchive.Click
Dim Extractor As New SevenZipExtractor(tbExtractFile.Text)
Extractor.ExtractArchive(tbExtractPath.Text)
End Sub
End Class
This causes an error when I try and run the code
Can anyone provide a sample code, or a link to a good example how-to-use SevenZipSharp? Because I searched and can't find any VB.NET samples.
Or maybe just help me figure out what I need to do.
Thanks.
You need to call SevenZipBase.SetLibraryPath with the path to 7z.dll, and make sure that you are using the correct version for your application (32- or 64-bit). e.g.
SevenZipBase.SetLibraryPath("C:\Dev\7z.dll")
Dim Extractor As New SevenZipExtractor(tbExtractFile.Text)
Extractor.ExtractArchive(tbExtractPath.Text)

vb.net storing large amounts of data

I need to store a large list of id's and load them into my .NET project the id's are like -
1:1:1:1
9:2:5:3
0:2:6:4
the list is roughly 15k lines.
ive tried storing the list online and reading it line by line but it takes 3-4 minutes just to get all the information, also tried downloading the text file and then reading it line by line but that's also incredibly slow.
can anyone suggest a better way ?
edit - code for reading from text file
Private Sub frmIkovItemID_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim fStream As New System.IO.FileStream("c:\proxy.txt", IO.FileMode.Open)
Dim sReader As New System.IO.StreamReader(fStream)
Do While sReader.Peek >= 0
itemIDs.Add(sReader.ReadLine)
Loop
Catch o As Exception
MsgBox(o.Message)
End Try
I need to store a large list of [...]
What you really mean is that you want to persist data.
Reading and writing in a file is a kind of persistence, yes, but a better solution would be to use a database. Especially if you are using a large amount of data. (Why use a database instead of just saving your data to disk?)
If you don't know how to use a database in vb.net, have a look at the Microsoft support page.

How to add a record to a database VB.NET, using datasets?

I have a Microsoft Access database containing three tables - "Class 1", "Class 2" and "Class 3". In each table, there are three columns: "ID", "Name" and "Score". In Visual Basic, I have two variables - name and score. I would like to add the name and score on a new row in the database, using datasets. I looked at the MSDN tutorial, but I don't really understand it:
https://msdn.microsoft.com/en-us/library/5ycd1034.aspx
Dim newCustomersRow As NorthwindDataSet.CustomersRow
newCustomersRow = NorthwindDataSet1.Customers.NewCustomersRow()
newCustomersRow.CustomerID = "ALFKI"
newCustomersRow.CompanyName = "Alfreds Futterkiste"
NorthwindDataSet1.Customers.Rows.Add(newCustomersRow)
Where does the "NorthwindDataSet1" come from? I understand where "NorthwinDataSet" comes from = that is the name of the data set created via the data source tab.
I am not sure how to add the record to the table. I started like this:
Dim newScoreRow As scoresDataSet.Class_1Row
I am not sure how to write the next line of code because I don't understand the second line of code in the MSDN documentation.
After that second line of code has been written, I understand that I write something like:
newScoreRow.Name = name
newScoreRow.Score = Str(Score)
In summary, where does the "NorthwindDataSet1" come from? How would I finish my code?
NorthwindDataSet1 is an instance of a Typed DataSet called NorthWindDataSet. Take a look at these MSDN Pages Working with Datasets in Visual Studio and How to: Create a Typed Dataset
Following the second link I posted above I created a new DataSet called scoresDataSet and I added a table called Class_1 and under the table I created the columns ID, Name, and Scorejust like you had in your question.
The Typed DataSet Class file that was generated for me is way to large to post here on SO, but here is the code that I used to add a new row to the Class_1 table in the DataSet. The addNewRowButton is just a button on the form. I also added two textboxes on the form called nameTextBox and scoreTextbox.
Private ScoresDataSet1 As New scoresDataSet()
Private Sub addNewRowButton_Click(sender As Object, e As EventArgs) Handles addNewRowButton.Click
Dim name As String = nameTextBox.Text
Dim score As String = scoreTextbox.Text
Dim newScoreRow As scoresDataSet.Class_1Row
newScoreRow = ScoresDataSet1.Class_1.NewClass_1Row
newScoreRow.Name = name
newScoreRow.Score = score
ScoresDataSet1.Class_1.Rows.Add(newScoreRow)
End Sub
The trick here is that your data actually exists in an Access Database so rather than manually creating an DataSet like I did, you'll want to Add a Data Source to your project using the Data Source Configuration Wizard and choosing Microsoft Access Database File.
In visual Studio 2012, just go to:
Project
Add New Data Source...
Database
Dataset
New Connection...
Change...
And choose the appropriate option and fill out the other fields in those windows. Once you've completed that and you build your project you should have a new DataSet object and associated TableAdapters in the toolbox under your application's Components from the form designer.
Once you add a copy of your newly created DataSet and associated TableAadapter to your form you can use the TableAdapters to populate data into your dataset from the access database and you can use the TableAdapters to store data into your access database.

Report Viewer VB.NET

I'm just trying to figure out how to use Report Viewer in VB.NET.
The report has only one text box with the data element name set to ReportName.
The code is simple.
Private Sub frmCalibrationPreviewReport_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If _CalibrationReportID <> -1 Then
With rvCalibrationReport
.LocalReport.DataSources.Clear()
.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local
Dim tmpData As DataTable = modDeclare.SelectSQL("SELECT ReportName FROM tblReportTypes")
.LocalReport.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("tmpData", tmpData))
End With
End If
Me.rvCalibrationReport.RefreshReport()
End Sub
Nothing is appearing on the report, it should contain two records.
Where am I going wrong?
Jim
Here's a great article covering Report Viewer
I mention this link as it appears your new to this. I would recommend reading this first.
Try Changing
.LocalReport.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("tmpData", tmpData))
To
.LocalReport.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("tmpData", tmpData.defaultview))
I would also suggest looking at this question, very similar to yours...
Bind DataTable to RDLC and ReportViewer
The problem with this question is there are a lot of gears at work when using reporting in visual studio. The problem could also be in the report file itself (.RDLC) if the field is not referencing the data source correctly. With the limited amount of information all I could suggest is using a working template and slowly adding your desired elements one at a time.

Visual Basic (2010) - Using variables in embedded text files?

Ive always been able to just search for what I need on here, and I've usually found it fairly easily, but this seems to be an exception.
I'm writing a program in Visual Basic 2010 Express, it's a fairly simple text based adventure game.
I have a story, with multiple possible paths based on what button/option you choose.
The text of each story path is saved in its own embedded resource .txt file. I could just write the contents of the text files straight into VB, and that would solve my problem, but that's not the way I want to do this, because that would end up looking really messy.
My problem is that I need to use variable names within my story, here's an example of the contents of one of the embedded text files,
"When "+playername+" woke up, "+genderheshe+" didn't recognise "+genderhisher+" surroundings."
I have used the following code to read the file into my text box
Private Sub frmAdventure_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim thestorytext As String
Dim imageStream As Stream
Dim textStreamReader As StreamReader
Dim assembly As [Assembly]
assembly = [assembly].GetExecutingAssembly()
imageStream = assembly.GetManifestResourceStream("Catastrophe.CatastropheStoryStart.png")
textStreamReader = New StreamReader(assembly.GetManifestResourceStream("Catastrophe.CatastropheStoryStart.txt"))
thestorytext = textStreamReader.ReadLine()
txtAdventure.Text = thestorytext
End Sub
Which works to an extent, but displays it exactly as it is in the text file, keeps the quotes and the +s and the variable names instead of removing the quotes and the +s and replacing the variable names with what's stored within the variables.
Can anyone tell me what I need to change or add to make this work?
Thanks, and apologies if this has been answered somewhere and I just didn't recognise it as the solution or didn't know what to search to find it or something.
Since your application is compiled, you cannot just put some of your VB code in the text file and have it executed when it is read.
What you can do, and what is usually done, is that you leave certain tags inside your text file, then locate them and replace them with the actual values.
For example:
When %playername% woke up, %genderheshe% didn`t recognise %genderhisher% surroundings.
Then in your code, you would find all the tags:
Dim matches = Regex.Matches(thestorytext, "%(\w+?)%")
For Each match in matches
' the tag name is now in: match.Groups(1).Value
' replace the tag with the value and replace it back into the original string
Next
Of course the big problem still remains - which is how to fill in the actual values. Unfortunately, there is no clean way to do this, especially using any local variables.
You can either manually maintain a Dictionary of tag names and their values, or use Reflection to get the values directly at the runtime. While it should be used carefully (speed, security, ...), it will work just fine for your case.
Assuming you have all your variables defined as properties in the same class (Me) as the code that reads and processes this text, the code will look like this:
Dim matches = Regex.Matches(thestorytext, "%(\w+?)%")
For Each match in matches
Dim tag = match.Groups(1).Value
Dim value = Me.GetType().GetField(tag).GetValue(Me)
thestorytext = thestorytext.Replace(match.Value, value) ' Lazy code
Next
txtAdventure.Text = thestorytext
If you don't use properties, but only fields, change the line to this:
Dim value = Me.GetType().GetField(tag).GetValue(Me)
Note that this example is rough and the code will happily crash if the tags are misspelled or not existing (you should do some error checking), but it should get you started.