dataset not commiting data

G

Guest

I am currently using VS.NET 2005 to read in a text file into a sql server
database table. The code cycles through the file and using a .add with a
dataset seems to add all the rows just fine.
When the program finishes and I go do a query on the table it is empty. I
am new to using VS so I am wondering what I am missing. Below is the code I
use to load the test file. Any help would be greatly appreciated.

** Code Example
Using MyReader As New
Microsoft.VisualBasic.FileIO.TextFieldParser(filename)
MyReader.TextFieldType =
Microsoft.VisualBasic.FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim currentRow As String()
Dim ai As Integer
Dim ls_array(1) As String
Dim maxfields As Integer = 0

If tablenm = "agCRT" Then
maxfields = 149
ReDim ls_array(149)
ElseIf tablenm = "agWRT" Then
maxfields = 71
ReDim ls_array(71)
End If
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentfield As String
Dim fieldnum As Integer = 0
ai = 0
For Each currentfield In currentRow
ls_array(ai) = currentfield
ai = ai + 1
If ai > maxfields Then
ds.Tables(tablenm).Rows.Add(ls_array)
End If
Next


Catch ex As
Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & _
" is invalid. Skipping")
End Try
End While
End Using
** End Code Example
 
M

Mr. Arnold

skl said:
I am currently using VS.NET 2005 to read in a text file into a sql server
database table. The code cycles through the file and using a .add with a
dataset seems to add all the rows just fine.

How do you know this?
When the program finishes and I go do a query on the table it is empty. I
am new to using VS so I am wondering what I am missing. Below is the code
I
use to load the test file. Any help would be greatly appreciated.

** Code Example

I myself think you need to put the TRY before the Using to cover all of the
code.
Using MyReader As New
Microsoft.VisualBasic.FileIO.TextFieldParser(filename)
MyReader.TextFieldType =
Microsoft.VisualBasic.FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim currentRow As String()
Dim ai As Integer
Dim ls_array(1) As String
Dim maxfields As Integer = 0

If tablenm = "agCRT" Then
maxfields = 149
ReDim ls_array(149)
ElseIf tablenm = "agWRT" Then
maxfields = 71
ReDim ls_array(71)
End If
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentfield As String
Dim fieldnum As Integer = 0
ai = 0
For Each currentfield In currentRow
ls_array(ai) = currentfield
ai = ai + 1
If ai > maxfields Then
ds.Tables(tablenm).Rows.Add(ls_array)
End If
Next


You may need to just Catch ex as Exception to catch everything that could be
happening.
 
H

Hardono Arifanto

Hi skl,

I believe you're trying to read CSV file (^_^). I have written a
simple CSV parser which loads it into a DataTable. You can find it in
my blog. I hope it helps.

Regards,
Hardono Arifanto
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Data rows not commiting to database. 2
WriteAllText 1
textFieldParser to text 1
txt files 2
"using statments" 5
Handling Quotes in a CSV file 1
Multidimensional arrays & Parsing 1
Macro not running anymore 2

Top