Streamreader read next line if empty row - vb.net

I have a problem when i want to use streamreader readline by line my text file. The streamreader i using is by taking the substring, but when it comes to empty rows it pop up this error message "Index and length must refer to a location within the string". It is not exactly empty row or line, but it contains this thing "ÐôÐ#(ôd". How to skip or ignore or delete the row that not have appropriate line in text file?This is my code
For Each finalname In filenames
row2 = 1
Using reader As New StreamReader(finalname)
Do Until reader.EndOfStream
Dim line As String = reader.ReadLine
Dim datetimestring As String = line.Substring(0, 22)
datetimestring = datetimestring.Replace("""", "").Trim()
datetimestring = datetimestring.Replace("-", "").Trim()
Dim format As String = "dd/MM/yyyy hh:mm:ss tt"
Dim time As DateTime = DateTime.Parse(datetimestring)
Dim Dt = time.ToString(format)
'Dim dtime As DateTime = DateTime.Parse(Dt)
Dim c As String = line.Substring(22)
c = c.Replace("-", "").Trim()
Dim cmd1 As New SqlCommand("insert into table5([time] , [process], [oven], [line]) values ( #line , #line1, '" & oven & "', '" & row2 & "')", con1)
cmd1.Parameters.Add("#line", SqlDbType.DateTime).Value = time
cmd1.Parameters.Add("#line1", SqlDbType.NVarChar).Value = c
con1.Open()
cmd1.ExecuteNonQuery()
con1.Close()
row2 = row2 + 1
Loop
oven = oven + 2
Dim str20 As String = "data source=10.239.213.40;uid=sa;pwd=Win32API;database=mbi"
Dim con20 As New SqlConnection(str20)
Dim com20 As String = "select top 9 * from table5 where process like '%Load lot clicked%' or process like '%''QCheck'' Button%' or process like '%''Start'' Button%' or process like '%Run Proc%' or process like '%Run Bin2 clicked%' or process like '%Started with Double click%' and oven ='2' order by oven, line desc"
Dim Adpt20 As New SqlDataAdapter(com20, con1)
Dim ds2 As New DataSet()
Adpt20.Fill(ds2, "table5")
DataGridView1.DataSource = ds2.Tables(0)
End Using
Next

Try this
Dim line As String = reader.ReadLine
If(String.ISNullorWhiteSpace(line) = false Then
Dim datetimestring As String = line.Substring(0, 22)
datetimestring = datetimestring.Replace("""", "").Trim()
datetimestring = datetimestring.Replace("-", "").Trim()
Dim format As String = "dd/MM/yyyy hh:mm:ss tt"
Dim time As DateTime = DateTime.Parse(datetimestring)
Dim Dt = time.ToString(format)
'Dim dtime As DateTime = DateTime.Parse(Dt)
Dim c As String = line.Substring(22)
c = c.Replace("-", "").Trim()
Dim cmd1 As New SqlCommand("insert into table5([time] , [process], [oven], [line]) values ( #line , #line1, '" & oven & "', '" & row2 & "')", con1)
cmd1.Parameters.Add("#line", SqlDbType.DateTime).Value = time
cmd1.Parameters.Add("#line1", SqlDbType.NVarChar).Value = c
con1.Open()
cmd1.ExecuteNonQuery()
con1.Close()
row2 = row2 + 1
End If
If line.Substring is the issue then check with something like this
Dim lineLength As Int = line.Length()
Dim datetimestring As String =If(line.Substring(0, 22) = Null, "", ine.Substring(0, 22))
Dim datetimestring As String =If(lineLength < 22, "", line.Substring(0, 22))

I wouldn't use SubString function, as you noted when a line is shorter than 22 characters the program will crash.
Typically I will subdivide or split the line into different parts by using Split function, for example:
dim ast() as string = line.split("XXX")
then extract the specific string from it:
datetimestring = ast(0)
this, of course, requires the parts are seperated by common delimiters, such as ',' or empty spaces, etc.
Also, add additional condition after reading the line, for example:
Dim line As String = reader.ReadLine
If String.ISNullorWhiteSpace(line) orelse line.length < 10 Then continue do

Do Until reader.EndOfStream
Dim line As String = reader.ReadLine
If Not String.IsNullOrWhiteSpace(line) AndAlso Not line.Length < 22 Then
'put the rest of your do-loop code here
End If
Loop

Related

Performance improvement on vb.net code

I need to write 50 million records with 72 columns into text file, the file size is growing as 9.7gb .
I need to check each and every column length need to format as according to the length as defined in XML file.
Reading records from oracle one by one and checking the format and writing into text file.
To write 5 crores records it is taking more than 24 hours. how to increase the performance in the below code.
Dim valString As String = Nothing
Dim valName As String = Nothing
Dim valLength As String = Nothing
Dim valDataType As String = Nothing
Dim validationsArray As ArrayList = GetValidations(Directory.GetCurrentDirectory() + "\ReportFormat.xml")
Console.WriteLine("passed xml")
Dim k As Integer = 1
Try
Console.WriteLine(System.DateTime.Now())
Dim selectSql As String = "select * from table where
" record_date >= To_Date('01-01-2014','DD-MM-YYYY') and record_date <= To_Date('31-12-2014','DD-MM-YYYY')"
Dim dataTable As New DataTable
Dim oracleAccess As New OracleConnection(System.Configuration.ConfigurationManager.AppSettings("OracleConnection"))
Dim cmd As New OracleCommand()
cmd.Connection = oracleAccess
cmd.CommandType = CommandType.Text
cmd.CommandText = selectSql
oracleAccess.Open()
Dim Tablecolumns As New DataTable()
Using oracleAccess
Using writer = New StreamWriter(Directory.GetCurrentDirectory() + "\FileName.txt")
Using odr As OracleDataReader = cmd.ExecuteReader()
Dim sbHeaderData As New StringBuilder
For i As Integer = 0 To odr.FieldCount - 1
sbHeaderData.Append(odr.GetName(i))
sbHeaderData.Append("|")
Next
writer.WriteLine(sbHeaderData)
While odr.Read()
Dim sbColumnData As New StringBuilder
Dim values(odr.FieldCount - 1) As Object
Dim fieldCount As Integer = odr.GetValues(values)
For i As Integer = 0 To fieldCount - 1
Dim vals As Array = validationsArray(i).ToString.ToUpper.Split("|")
valName = vals(0).trim
valDataType = vals(1).trim
valLength = vals(2).trim
Select Case valDataType
Case "VARCHAR2"
If values(i).ToString().Length = valLength Then
sbColumnData.Append(values(i).ToString())
'sbColumnData.Append("|")
ElseIf values(i).ToString().Length > valLength Then
sbColumnData.Append(values(i).ToString().Substring(0, valLength))
'sbColumnData.Append("|")
Else
sbColumnData.Append(values(i).ToString().PadRight(valLength))
'sbColumnData.Append("|")
End If
Case "NUMERIC"
valLength = valLength.Substring(0, valLength.IndexOf(","))
If values(i).ToString().Length = valLength Then
sbColumnData.Append(values(i).ToString())
'sbColumnData.Append("|")
Else
sbColumnData.Append(values(i).ToString().PadLeft(valLength, "0"c))
'sbColumnData.Append("|")
End If
'sbColumnData.Append((values(i).ToString()))
End Select
Next
writer.WriteLine(sbColumnData)
k = k + 1
Console.WriteLine(k)
End While
End Using
writer.WriteLine(System.DateTime.Now())
End Using
End Using
Console.WriteLine(System.DateTime.Now())
'Dim Adpt As New OracleDataAdapter(selectSql, oracleAccess)
'Adpt.Fill(dataTable)
Return Tablecolumns
Catch ex As Exception
Console.WriteLine(System.DateTime.Now())
Console.WriteLine("Error: " & ex.Message)
Console.ReadLine()
Return Nothing
End Try

How to change value of /GrpPwd/ Cell in all Rows?

I need to perform this SQL Query:
UPDATE Resurses SET GrpPwd = #GrpPwd1 WHERE Resurs = #Resurs1
For all rows in my Access Database using VB.NET
How can i do it?
I use this code but id doesn't work:
Password Generator:
Dim charset As String = nalf
Dim r As New Random()
Dim lenPass As Integer = r.Next(minLength, maxLength)
Dim str As String = String.Empty
For i As Integer = 0 To lenPass - 1
str += charset(r.Next(0, charset.Length))
Next
Return str
End Function
Update rows
If DataGridView1.CurrentRow.Cells(3).Value = "Yes" Then
nres = DataGridView1.CurrentRow.Cells(0).Value
nalf = DataGridView1.CurrentRow.Cells(6).Value
nsym = DataGridView1.CurrentRow.Cells(1).Value
Dim parol1 As String
Dim pwd As String = pass99(nsym, nsym) '
parol1 = pwd
Dim cmd As New OleDbCommand()
Dim Conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.oledb.12.0; Data source=" + bpath)
cmd.Connection = Conn
Conn.Open()
For i As Integer = 0 To k - 1
cmd.CommandText = String.Format("UPDATE Resurses SET GrpPwd = #GrpPwd1{0} WHERE Resurs = #Resurs1{0};", i)
cmd.Parameters.Add(String.Format("#GrpPwd1{0}", i), OleDbType.WChar).Value = parol1
cmd.Parameters.Add(String.Format("#Resurs1{0}", i), OleDbType.Integer).Value = nres
Next
cmd.ExecuteNonQuery()
Conn.Close()
MsgBox("All rows are updated!")
End If
f

Splitting listviews items help needed

I have some code that loops through a listview and puts the values into 'output1' & 'output2'. However, what is happening is that the results are strung together like thus: 0307NG77775660NG7778. How do I split so that I can include in access db.
There are 2 columns in the listview and they contain values in the format of:
1st row 0307 - NG7777, 2nd row 5660 - 7778 etc. This is a desktop winforms code. Thanks
Dim BoxList As New List(Of String)
Dim BoxItem As String
Dim CustRefList As New List(Of String)
Dim CustRef As String
For Each item As ListViewItem In Me.lvSelectedItems.Items
Dim box As String = item.Text
BoxItem = item.SubItems.Item(1).Text
BoxList.Add(box)
output2 += BoxItem
Dim cust As String = item.Text
CustRef = item.SubItems.Item(1).Text
CustRefList.Add(cust)
output1 += CustRef
Next
output = String.Join(" "c, BoxList.ToArray(), CustRefList.ToArray())
Dim cmd As OleDbCommand = New OleDbCommand("Insert into Temp (temp, [class]) Values ('" & output1 & "', '" & output2 & "')", oledbCnn)
dr = cmd.ExecuteReader
Make username a List(Of String), then just do:
usernameList.Add(username) 'instead of output1 += username
When done with the loop, do this:
output1 = String.Join(" "c, usernameList.ToArray())
You can pick a join character of your choice (if you don't like spaces " ").
EDIT: Example:
Dim usernameList As New List(Of String)
Dim session As String
For Each item As ListViewItem In Me.lvSelectedItems.Items
Dim username As String = item.Text
session = item.SubItems.Item(1).Text
usernameList.Add(username)
output2 += session
Next
output1 = String.Join(" "c, usernameList.ToArray())

VB.NET create datatable?

I am trying to create a datatable in VB.NET, but when I output a row from this table it shows: "System.Data.DataRow".
I have compared my code to several websites and it seems correct. However if I instead Response.Write these values they display correctly.
So here is my code:
Sub Page_Load(Sender as Object, E as EventArgs)
If Not IsPostback Then
...
End If
Main
End Sub
Sub Main()
Dim FirstMonthDate As Date = CDate("1/1/" & dYear.SelectedValue)
Dim LastMonthDate As Date = GlobalFunctions.GlobalF.MonthLastDate(dYear.SelectedValue & "-" & dEndMonth.SelectedValue & "-1")
Dim TheGroup As String
Dim LastWeek As String
Dim FirstWeek As String
Dim dLastWeek As Date
Dim dFirstWeek As Date
Dim iWeek As String
Dim commandstring As String = "SELECT cast(DateDiff(wk, '1/1/2011', '" & LastMonthDate & "') as varchar(30)) AS SELECTED_WEEK "
Dim DSDateData As New System.Data.DataSet
DSDateData = GlobalFunctions.GlobalF.GetDevSQLServerDataSet(commandstring)
Dim dRow As DataRow
For Each dRow In DSDateData.Tables(0).Rows
iWeek = dRow("SELECTED_WEEK")
Next
Dim FirstYearDate As Date = CDate("1/1/" & dYear.SelectedValue)
commandstring = "SELECT DateAdd(ww, " & iWeek & ", '" & FirstYearDate & "') AS LAST_WEEK "
Dim DSDateData2 As New System.Data.DataSet
DSDateData2 = GlobalFunctions.GlobalF.GetDevSQLServerDataSet(commandstring)
For Each dRow In DSDateData2.Tables(0).Rows
LastWeek = dRow("LAST_WEEK")
Next
dLastWeek = CDate(LastWeek)
commandstring = "SELECT DATEADD(dd, -1, DATEADD(wk, DATEDIFF(wk,0, dateadd(dd,6-datepart(day,'" & LastMonthDate & "'),'" & LastMonthDate & "') ), 0)) AS FIRST_WEEK "
Dim DSDateData3 As New System.Data.DataSet
DSDateData3 = GlobalFunctions.GlobalF.GetDevSQLServerDataSet(commandstring)
For Each dRow In DSDateData3.Tables(0).Rows
FirstWeek = dRow("FIRST_WEEK")
Next
dFirstWeek = CDate(FirstWeek)
dFirstWeek = DateAdd(DateInterval.WeekOfYear, -1, dFirstWeek)
Dim arrDayYear As New ArrayList()
Dim dFirstDay As New Date
Dim arrList As ArrayList = New ArrayList()
Dim dt2 As New DataTable
Dim sWeek As DataColumn = New DataColumn("sWeek")
sWeek.DataType = System.Type.GetType("System.String")
Dim sDay As DataColumn = New DataColumn("sDay")
sDay.DataType = System.Type.GetType("System.String")
dt2.Columns.Add(sWeek)
dt2.Columns.Add(sDay)
Response.Write("NW1")
Dim dr As DataRow
dr = dt2.NewRow()
dr("sWeek") = "New week"
dr("sDay") = "New day"
dt2.Rows.Add(dr)
Response.Write(dr)
Response.Write("<br>")
dFirstDay = dFirstDay.AddDays(1)
...
End Sub
...
It's this last Response.Write that is causing the "System.Data.DataRow". But the Response.Write before this (NW1) does print. So what am I doing wrong?
Your code is correct.
Calling Response.Write(dr) prints dr.ToString().
Since DataRow doesn't override ToString(), it jsut returns the typename.
You probably want to prin the value of a column.
You must iterate the data rows or select a specific data row. Iterating and outputing the values would be like so:
//c#
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn col in dt.Columns)
Console.WriteLine(row[col]);
}
//VB.Net
For Each (row As DataRow In dt.Rows)
For Each (col As DataColumn In dt.Columns)
Console.WriteLine(row[col])
Next
Next
To print just the first row:
DataRow row = dt.Rows[0];
foreach (DataColumn col in row.Columns)
Console.WriteLine(row[col]);
Response.Write() Needs a string, so it is converting your variable (dr) to a string. The default "toString" method just uses the name of the class. In this case "System.Data.DataRow"
It seems like you might want to loop over the row and output each value individually something like...
For Each element In dr.ItemArray
Response.Write(element)
Next
Response.Write("<br>")

vb.net xls to csv with quotes?

I have a xls file, or a csv without quotes, and using vb.net need to turn it into a csv with quotes around every cell. If I open the xls/csv without quotes in MS Access, set every column to text and then export it, its in the format I need. Is there an easier way? If not, how do I do replicate this in vb.net? Thanks.
If you use the .Net OLE DB provider, you can specify the .csv formatting details in a schema.ini file in the folder your data files live in. For the 'unquoted' .csv the specs
should look like
[noquotes.csv] <-- file name
ColNameHeader=True <-- or False
CharacterSet=1252 <-- your encoding
Format=Delimited(,) <--
TextDelimiter= <-- important: no " in source file
Col1=VendorID Integer <-- your columns, of course
Col2=AccountNumber Char Width 15
for the 'quoted' .csv, just change the name and delete the TextDelimiter= line (put quotes around text fields is the default).
Then connect to the Text Database and execute the statement
SELECT * INTO [quotes.csv] FROM [noquotes.csv]
(as this creates quotes.csv, you may want to delete the file before each experimental run)
Added to deal with "Empty fields must be quoted"
This is a VBScript demo, but as the important things are the parameters for .GetString(), you'll can port it to VB easily:
Dim sDir : sDir = resolvePath( "§LibDir§testdata\txt" )
Dim sSrc : sSrc = "noquotes.csv"
Dim sSQL : sSQL = "SELECT * FROM [" & sSrc & "]"
Dim oTxtDb : Set oTxtDb = New cADBC.openDb( Array( "jettxt", sDir ) )
WScript.Echo goFS.OpenTextFile( goFS.BuildPath( sDir, sSrc ) ).ReadAll()
Dim sAll : sAll = oTxtDb.GetSelectFRO( sSQL ).GetString( _
adClipString, , """,""", """" & vbCrlf & """", "" _
)
WScript.Echo """" & Left( sAll, Len( sAll ) - 1 )
and output:
VendorID;AccountNumber;SomethingElse
1;ABC 123 QQQ;1,2
2;IJK 654 ZZZ;2,3
3;;3,4
"1","ABC 123 QQQ","1,2"
"2","IJK 654 ZZZ","2,3"
"3","","3,4"
(german locale, therefore field separator ; and decimal symbol ,)
Same output from this VB.Net code:
Imports ADODB
...
Sub useGetString()
Console.WriteLine("useGetString")
Const adClipString As Integer = 2
Dim cn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim sAll As String
cn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=M:\lib\kurs0705\testdata\txt\;" _
& "Extended Properties=""text;"""
cn.Open()
rs = cn.Execute("SELECT * FROM [noquotes.csv]")
sAll = rs.GetString( adClipString, , """,""", """" & vbCrLf & """", "" )
cn.Close()
sAll = """" & Left( sAll, Len( sAll ) - 1 )
Console.WriteLine( sAll )
End Sub
Check out the method at this link.
What you can do to make sure quotes go around is append quotes to the beginning and end of each column data in the loop that is putting the column data in the file.
for example make the loop like this:
For InnerCount = 0 To ColumnCount - 1
Str &= """" & DS.Tables(0).Rows(OuterCount).Item(InnerCount) & ""","
Next
Public Class clsTest
Public Sub Test
Dim s as string = "C:\!Data\Test1.csv"
Dim Contents As String = System.IO.File.ReadAllText(s)
Dim aryLines As String() = Contents.Split(New String() { Environment.Newline }, StringSplitOptions.None)
Dim aryParts() As String
Dim aryHeader() As String
Dim dt As System.Data.DataTable
For i As Integer = 0 To aryLines.Length - 1
aryParts = SplitCSVLine(aryLines(i))
If dt Is Nothing And aryHeader Is Nothing Then
aryHeader = CType(aryParts.Clone, String())
ElseIf dt Is Nothing And aryHeader IsNot Nothing Then
dt = DTFromStringArray(aryParts, 1000, "", aryHeader)
Else
DTAddStringArray(dt, aryParts)
End If
Next
dt.dump
End Sub
Public Shared Function SplitCSVLine(strCSVQuotedLine As String) As String()
Dim aryLines As String() = strCSVQuotedLine.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
Dim aryParts As String() = Nothing
For i As Integer = 0 To aryLines.Length - 1
Dim regx As New Text.RegularExpressions.Regex(",(?=(?:[^\""]*\""[^\""]*\"")*(?![^\""]*\""))")
aryParts = regx.Split(aryLines(i))
For p As Integer = 0 To aryParts.Length - 1
aryParts(p) = aryParts(p).Trim(" "c, """"c)
Next
Next
Return aryParts
End Function
Public Shared Function DTFromStringArray(ByVal aryValues() As String, Optional ByVal intDefaultColumnWidth As Integer = 255, Optional ByVal strTableName As String = "tblArray", Optional ByVal aryColumnNames() As String = Nothing) As DataTable
If String.IsNullOrWhiteSpace(strTableName) Then strTableName = "tblArray"
Dim dt As DataTable = New DataTable(strTableName)
Dim colNew(aryValues.GetUpperBound(0)) As DataColumn
If aryColumnNames Is Nothing Then
ReDim aryColumnNames(aryValues.Length)
Else
If aryColumnNames.GetUpperBound(0) < aryValues.GetUpperBound(0) Then
ReDim Preserve aryColumnNames(aryValues.Length)
End If
End If
For x As Integer = aryColumnNames.GetLowerBound(0) To aryColumnNames.GetUpperBound(0)
If String.IsNullOrWhiteSpace(aryColumnNames(x)) Then
aryColumnNames(x) = "Field" & x.ToString
Else
aryColumnNames(x) = aryColumnNames(x)
End If
Next
For i As Integer = 0 To aryValues.GetUpperBound(0)
colNew(i) = New DataColumn
With colNew(i)
.ColumnName = aryColumnNames(i) '"Value " & i
.DataType = GetType(String)
.AllowDBNull = False
.DefaultValue = ""
.MaxLength = intDefaultColumnWidth
.Unique = False
End With
Next
dt.Columns.AddRange(colNew)
Dim pRow As DataRow = dt.NewRow
For i As Integer = aryValues.GetLowerBound(0) To aryValues.GetUpperBound(0)
pRow.Item(i) = aryValues(i)
Next
dt.Rows.Add(pRow)
Return dt
End Function
Public Shared Sub DTAddStringArray(ByRef dt As DataTable, ByVal aryRowValues() As String)
Dim pRow As DataRow
pRow = dt.NewRow
For i As Integer = aryRowValues.GetLowerBound(0) To aryRowValues.GetUpperBound(0)
pRow.Item(i) = aryRowValues(i)
Next
dt.Rows.Add(pRow)
End Sub
End Class