trouble returning datatable within a try statement - vb.net

I have the following piece of code that I am using to try and rip a csv file and turn it into a datatable. My problem is that the debugger never makes it to the return statement. Everything is appending to the datatable correctly, so I know that part works. Any idea's on what I can do to trouble shoot this further. Also, if you know of a simpler way to turn a import a csv file to a datatable I'd be very interested in learning about it.
Thanks!
Public Function loadCSVTableII() As DataTable
Dim dt As New DataTable("TableII")
Dim line As String = String.Empty
Dim counter As Integer = 0
Dim reader As New StreamReader(pathTableTwo)
Try
While Not IsNothing(line)
line = reader.ReadLine()
Dim lineSep As String() = line.Split(New Char() {","c})
If Not counter = 0 Then
dt.Rows.Add(lineSep)
counter += 1
Else
For Each value As String In lineSep
dt.Columns.Add(value)
Next
counter += 1
End If
End While
'cursor never gets to this code block...
Dim primarykey(0) As DataColumn
primarykey(0) = dt.Columns("Ages")
dt.PrimaryKey = primarykey
Return dt
Catch ex As Exception
Throw
End Try
End Function
Update: It is erroring out on this line in the code.
Dim lineSep As String() = line.Split(New Char() {","c})
It say that the Object reference is not set to an instance of an object. What's weird though is that it works through the whole data table fine. Could it be that the while loop is not terminating at the end of the file?

Try changing your While loop to handle the end of stream condition. It's not very clear what the IsNothing function is doing in your code.
While Not reader.EndOfStream
line = reader.ReadLine
'// Dim lineSep As String() = line.Split(New Char() {","c})
For your line split, in VB.Net, it's simple to just do this:
Dim lineSep As String() = line.Split(",")

You can use OLEDB provider for this.
string query = "SELECT Symbol, [Name of Company], FROM [just file name with extension]";
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + [csv file path without file name] + ";" + "Extended Properties=’text;HDR=YES;’";
//create dataadapter object
OleDbDataAdapter adapter = new OleDbDataAdapter(query, connStr);
// create table
DataTable dtSymbolDetails = new DataTable("ScriptDetails");
dtSymbolDetails.Columns.Add("Symbol");
dtSymbolDetails.Columns.Add("Name of Company");
// fill the table with data using adapter
adapter.Fill(dtDetails);

Related

how to read a specific csv line vb.net

ask permission,
I created a bot to input data to the web using vb.net and selenium.
Retrieve data from csv .
How to retrieve data from csv as needed, for example, there are 100 rows, only 30-50 rows are taken, for example. The loop code should not be looped at all.
Dim textFieldParser As TextFieldParser = New TextFieldParser(TextBox1.Text) With
{
.TextFieldType = FieldType.Delimited,
.Delimiters = New String() {","}
}
drv = New ChromeDriver(options)
While Not textFieldParser.EndOfData
Try
Dim strArrays As String() = textFieldParser.ReadFields()
Dim name As String = strArrays(0)
Dim alamat As String = strArrays(1)
Dim notlp As String = strArrays(2)
drv.Navigate().GoToUrl("URL")
Dim Nm = drv.FindElement(By.XPath("/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input"))
Nm.SendKeys(name)
Threading.Thread.Sleep(3000)
Catch ex As Exception
MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
End Try
End While
Thank you
Here's an example of using TextFieldParser to read one specific line and a specific range of lines. Note that I am using zero-based indexes for the lines. You can adjust as required if you want to use 1-based line numbers.
Public Function GetLine(filePath As String, index As Integer) As String()
Using parser As New TextFieldParser(filePath) With {.Delimiters = {","}}
Dim linesDiscarded = 0
Do Until linesDiscarded = index
parser.ReadLine()
linesDiscarded += 1
Loop
Return parser.ReadFields()
End Using
End Function
Public Function GetLines(filePath As String, startIndex As Integer, count As Integer) As List(Of String())
Using parser As New TextFieldParser(filePath) With {.Delimiters = {","}}
Dim linesDiscarded = 0
Do Until linesDiscarded = startIndex
parser.ReadLine()
linesDiscarded += 1
Loop
Dim lines As New List(Of String())
Do Until lines.Count = count
lines.Add(parser.ReadFields())
Loop
Return lines
End Using
End Function
Simple loops to skip and to take lines.

Opening a CSV file as a OLEDB Connection converts file text to double

I'm opening up a couple of CSV files and reading them in as a DataTable per the example I found here. The issue I am running into it the basic query I'm using to import the data is converting the column of IP addresses into Doubles. So I want to read in 10.0.0.1 and it shows up as 10.001. How can I get this column to read in as a string? I would like to not double process the file if I can.
Query I'm using is basic and is as follows:
SELECT * FROM [ComputerList.csv]
Here is my function to open and read the CSV file into a DataTable
Public Function OpenFile(ByVal strFolderPath as String, ByVal strQuery as String) as DataTable
Dim strConn as String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFolderPath & ";Extended Propteries=""text; HDR=Yes;FMT=Delimited"""
Dim conn as OleDb.OleDbConnection = New OleDb.OleDbConnection(strConn)
Try
conn.Open()
Dim cmd as OleDb.OleDbCommand = New OleDb.OleDbCommand(strQuery, conn)
Dim da as OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter()
da.SelectCommand = cmd
Dim ds as DataSet = New DataSet()
da.Fill(ds)
da.Dispose()
return ds.Tables(0)
Catch
return Nothing
Finally
conn.Close()
End Try
End Function
i use this to read csv's and force all to string.
Public Function convert_csv_to_data_table(ByVal File As String, ByVal separator As String) As DataTable
Dim dt As New DataTable
Dim firstLine As Boolean = True
If IO.File.Exists(File) Then
Using sr As New StreamReader(File)
While Not sr.EndOfStream
If firstLine Then
firstLine = False
Dim cols = sr.ReadLine.Split(separator)
For Each col In cols
dt.Columns.Add(New DataColumn(col, GetType(String)))
Next
Else
Dim data() As String = sr.ReadLine.Split(separator)
dt.Rows.Add(data.ToArray)
End If
End While
End Using
End If
Return dt
End Function
EDIT:- this will only work with a separator btw
ok I tried variations of everyone's suggested and have settled on a hybrid between all of them. My goal was to read in a CSV file manipulate it in a DataTable form and them write it back out. Some of my CSV files had multiple lines with in a cell and some had deliminators within a cell. Below you can find my hybrid solution that utilized TextFieldParser to read in the file and break it up.
Public Function OpenFile(ByVal File as String, NyVal delim as String) as DataTable
Dim dt as New DataTable()
Dim firstline as Boolean = True
Using MyReader as New Microsoft.VisualBasic.FileIO.TextFieldParser(File)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(delim)
Dim currentRow as String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
If firstline
firstline = false
For Each col in currentRow
dt.Columns.Add(New DataColumn(col.ToString(), System.Type.GetType("System.String")))
Next
Else
dt.Rows.Add(currentRow.ToArray())
End If
Catch ex as Microsoft.VisualBasic.FileIO.MalformedLineException
Console.WriteLIne("Line " + ex.Message + " is not valid and will be skipped")
End Try
End While
End Using
return dt
End Function

How to return a string from a text file with condition met?

Public Sub openDB()
Dim Lines As New List(Of String)
Try
' Open the file using a stream reader.
Using sr As New StreamReader("Config.txt")
Dim line As String
' Read the stream to a string and write the string to the console.
line = sr.ReadLine()
Do Until String.IsNullOrEmpty(line)
Lines.Add(line)
line = sr.ReadLine
Loop
End Using
Catch e As Exception
Console.WriteLine("The file could not be read:")
Console.WriteLine(e.Message)
End Try
Dim dbname As String = g_DatabaseName
Dim server As String = Lines.Where(Function(str) str.Contains("server =")).ToString
Dim user As String = ""
Dim password As String = ""
conn = New MySqlConnection
conn.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false; Convert Zero Datetime=True", server, user, password, dbname)
conn.Open()
End Sub
Im try to return some string from a text file, so I use StreamReader to read the file and store them into a list. Now I try to declare a variable to get "localhost" from list of string, but the code below is not work for me.
Dim server As String = Lines.Where(Function(str) str.Contains("server
=")).ToString
Enumerable.Where does not return a single string but possibly multiple, using ToString gives you not the first matching line but just the name of the type which is System.Linq.Enumerable+WhereArrayIterator1[System.String].
Either declare it as IEnumerable(Of String) or use First/ FirstOrDefault to get the first line that matches the condition:
Dim serverLine As String = Lines
.Where(Function(str) str.Contains("server ="))
.FirstOrDefault()
You can also use the overload of FirstOrDefault(Nothing if there was no such line):
Dim serverLine As String = Lines.FirstOrDefault(Function(str) str.Contains("server ="))
To extract Localhost:
Dim server As String = serverLine.Substring(serverLine.IndexOf("server =") + "server =".Length).Trim(""""c, " "c)

How to convert csv file content to DataTable?

I convert CSV file to DataTable using following code. It is working fine but sometimes it adds invalid character between the string.
Private Shared Function convertToDT(filePath As String) As DataTable
Dim detailDataTable As New DataTable()
Using csvReader As New TextFieldParser(filePath)
csvReader.SetDelimiters(New String() {","})
csvReader.HasFieldsEnclosedInQuotes = True
Dim colFields As String() = csvReader.ReadFields()
For Each column As String In colFields
Dim datecolumn As New DataColumn(column.ToLowerInvariant)
datecolumn.AllowDBNull = True
detailDataTable.Columns.Add(datecolumn)
Next
While Not csvReader.EndOfData
Dim rowData As String() = csvReader.ReadFields //sometimes rowData convert invalid name
For i As Integer = 0 To rowData.Length - 1
//perform operation
Next
If rowData IsNot Nothing AndAlso rowData(0) IsNot Nothing Then
If Not (rowData(0).StartsWith("#") Or String.IsNullOrEmpty(rowData(0))) Then
detailDataTable.Rows.Add(rowData)
End If
End If
End While
End Using
Return detailDataTable
End Function
Sometime it add invalid name. Can anybody please suggest

vb.net access database

I am trying to read fields from a query into a text string arrary. In vb6 I can simply declare the array and then read the fields into it without it caring what type of values are in it. Now when I try to do that same thing I get an "unable to cast com object of type 'dao.fieldclass' to type 'system.string". Do I need to read the field value into a seperarte variable and then convert it to a string? The seqNum is what I am having the problem with
Public dbEngine As dao.DBEngine
Public db As dao.Database, recSet As dao.Recordset
dbEngine = New dao.DBEngine
Dim seqNum As Long
scExportTemplatePath = "M:\robot\scTemplates\"
db = dbEngine.OpenDatabase(scExportStuffPath & "scExport.mdb")
tsOut = fso.CreateTextFile(wildePath & dte & "-" & fle.Name & ".csv", True)
With recSet
.MoveFirst()
Do While Not .EOF
seg = .Fields("segmentID")
If seg <> segHold Then
seqNum = 1
End If
arrOut(0) = .Fields("jobnum_AM")
Loop
End With
You have several problems with this code. In addition to the points mentioned by Jeremy:
What was Long in VB6 is now Integer in VB.NET. Long is a 64 bit integer now.
Use System.IO.Path.Combine in order to combine path strings. Combine automatically adds missing backslashes and removes superfluous ones. Path.Combine(scExportTemplatePath, "scExport.mdb")
The Field property does not have a default property any more. Non-indexed properties are never default properties in VB.NET. Get the field value with .Fields("segmentID").Value.
Convert its value to the appropriate type: seg = Convert.ToInt32(.Fields("segmentID").Value)
Note: VB's Integer type is just an alias for System.Int32.
You are always adding to the same array field. I don't know exactly what you have in mind. If you want to add one field only, you could just use a List(Of String). If you are adding several fields for each record, then a List(Of String()) (i.e. a list of string arrays) would be appropriate. Lists have the advantage that they grow automatically.
Dim list As New List(Of String())
Do While Not .EOF
Dim values = New String(2) {}
values(0) = Convert.ToString(.Fields("field_A").Value)
values(1) = Convert.ToString(.Fields("field_B").Value)
values(2) = Convert.ToString(.Fields("field_C").Value)
list.Add(values)
recSet.MoveNext()
Loop
But it is more comprehensible, if you create a custom class for storing your field values:
Console.WriteLine("{0} {1} ({2})", user.FirstName, user.LastName, user.DateOfBirth)
... reads much better than:
Console.WriteLine("{0} {1} ({2})", values(0), values(1), values(2))
In VB.NET you have other possibilities to work with databases:
Dim list As New List(Of String())
Using conn = New OleDb.OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MyPath\MyDb.mdb")
Dim sql = "SELECT myStuff FROM myTable"
Dim command = New OleDbCommand(sql, conn)
conn.Open()
Using reader As OleDbDataReader = command.ExecuteReader()
While reader.Read()
Dim values = New String(reader.FieldCount - 1) {}
For i = 0 To reader.FieldCount - 1
values(i) = Convert.ToString(reader.GetValue(i))
Next
list.Add(values)
End While
End Using
End Using
Note that the Using statement closes the resources automatically at the end. Even if an error occurs and the code is terminated prematurely.
In VB.NET you can write to files like this (without using fso, which is not .NET like)
Using writer As New StreamWriter("myFile.txt", False)
writer.WriteLine("line 1")
writer.WriteLine("line 2")
writer.WriteLine("line 3")
End Using
1) You dont show how you open the Recordset, eg:
Set recSet = db.OpenRecordset("query_name or SQL")
2) You dont have a .MoveNext in the While Loop:
With recSet
.MoveFirst()
Do While Not .EOF
seg = .Fields("segmentID")
If seg <> segHold Then
seqNum = 1
End If
arrOut(0) = .Fields("jobnum_AM")
.MoveNext()
loop