convert multiline datatable column to multiline in html table in vb.net - vb.net

I have added multiline data to "Service Performed" column in datatable, which I am writing to HTML.
Dim row = dt.NewRow()
row("Date") = Display_Date
row("Mileage") = Odometer
For t1 As Integer = 0 To returnData.serviceHistory.displayRecords(d1).text.Count - 1
Text(t1) = returnData.serviceHistory.displayRecords(d1).text(t1).ToString
t_str = t_str + Text(t1) + vbNewLine
row("Service Performed") = t_str
Next
dt.Rows.Add(row)
HTML Part:
Private Function GetBody(ByVal dTable As DataTable) As String
Dim dString As New StringBuilder
dString.Append("<tbody>")
For Each dRow As DataRow In dTable.Rows
dString.Append("<tr align='center' valign='middle'>")
For dCount As Integer = 0 To dTable.Columns.Count - 1
If dCount = 0 Then
dString.AppendFormat("<td>{0}</td>", dRow(dCount).ToShortDateString())
ElseIf dCount = 2 Then
dString.AppendFormat("<td align='left'>{0}</td>", dRow(dCount))
Else
dString.AppendFormat("<td>{0}</td>", dRow(dCount))
End If
Next
dString.Append("</tr>")
Next
dString.Append("</tbody>")
Return dString.ToString()
End Function
I am getting an output for "Service Performed" column as
'Pre-delivery inspection completed Wheel locks installed VIN glass etching vin etched'
HTML
and not in separate line as
'Pre-delivery inspection completed
Wheel locks installed
VIN glass etching vin etched'
What will be the HTML table part of code to display multiline data received from Datatable column in HTML.

You should replace vbNewLine in your code by <br> since you are going to display DataTable values in a html table. Then your string will display in separate lines as you wanted it to.
The only change you need to make is in commented line in code below.
Dim row = dt.NewRow()
row("Date") = Display_Date
row("Mileage") = Odometer
For t1 As Integer = 0 To returnData.serviceHistory.displayRecords(d1).text.Count - 1
Text(t1) = returnData.serviceHistory.displayRecords(d1).text(t1).ToString
t_str = t_str + Text(t1) & "<br>" ''replace vbNewLine with <br>
row("Service Performed") = t_str
Next
dt.Rows.Add(row)

Related

Renumbering a Work Breakdown Structure (WBS) in a datatable

A multi-level WBS the users were able to delete some tasks that are not applicable to their project. Goal is to renumber the entire WBS so that no numbers are skipped. Example: 1.1.1, 1.1.2, 1.1.4 - number 1.1.3 was deleted. Each WBS element may or may not have child elements that need to be renumbered as well.
I have the data in a datatable, fields: WBS_ID, WBSNo, ParentID, ParentNo, SortKey(the original WBS Number) and Notes. WBS_ID is the UID, ParentID is the WBS_ID of the parent WBS element. First element of the WBS has a null value in the ParentID field.
I cannot seem to grasp how to make an iterative function call to create the WBS number.
Dim dTb As New DataTable
Using Conn
Conn.Open()
Using dad As New SqlClient.SqlDataAdapter(vSqlStr, Conn)
dad.SelectCommand.Parameters.Add("#ProjectID", SqlDbType.Int)
dad.SelectCommand.Parameters.Add("#Revision", SqlDbType.Int)
dad.SelectCommand.Parameters("#ProjectID").Value = vProjectID
dad.SelectCommand.Parameters("#Revision").Value = vRevision
dad.Fill(dTb) 'Now we have a table with all the elements .
End Using
Conn.Close()
End Using
'Now work with the datatable
'WBS_ID, WBSNo, WBSLevel, ParentID, ParentNo, SortKey, Notes
dTb.DefaultView.Sort = "SortKey ASC"
dTb = dTb.DefaultView.ToTable
Dim vRowCount As Int16
vRowCount = dTb.Rows.Count
Dim vCurRow As Int16 = 0
For x = 0 To vRowCount - 1
If Not IsDBNull(dTb.Rows(x)("ParentID")) Then
Else
dTb.Rows(x)("WBSNo") = "1"
End If
dTb.Rows(x)("Notes") = dTb.Rows(x)("Notes") & vbCrLf & "<<< Old WBS No. = " & dTb.Rows(x)("SortKey")
Next
Renumber the WBS, starting with "1", each child of an element would be numbered with the parent number, plus a period (.) then sequentially starting with 1; each child could have children.
I wrote a recursive sub-routine to update the datatable, then another routine to update the database.
Sub RenumberChildren(vParentID As Int16, vParentWBSNo As String)
Dim ChildCount As Int16 = 0
For x = 0 To dTb.Rows.Count - 1
If Not IsDBNull(dTb.Rows(x)("ParentID")) Then
If dTb.Rows(x)("ParentID") = vParentID Then
ChildCount += 1
dTb.Rows(x)("WBSNo") = vParentWBSNo & "." & ChildCount
RenumberChildren(dTb.Rows(x)("WBS_ID"), dTb.Rows(x)("WBSNo"))
End If
End If
Next
End Sub

VB - Sorting Alphabetically From a CSV File

I don't know a lot about the subject of sorting but here goes: I am trying to sort a music library (comma seperated in a csv file. Some examples):
1,Sweet Home Alabame,Lynyrd Skynyrd,4:40,Classic Rock
2,Misirlou,Dick Dale,2:16,Surf Rock
I need to sort them alphabetically (by title of track) but I don't know two things: 1. Why my current technique isn't working:
Dim array() As String = {}
sr = New StreamReader("library.csv")
counter = 1
Do Until sr.EndOfStream
array(counter) = sr.ReadLine()
counter += 1
Loop
System.Array.Sort(Of String)(array)
Dim value As String
For Each value In array
Console.WriteLine(value)
Next
Console.ReadLine()
I don't know if this is the best way of sorting. I then need to display them as well. I can do this without sorting, but can't figure out how to do it with sorting.
Help please (from people who, unlike me, know what they're doing).
Right now you're putting all fields in one long string of text (each row).
In order to sort by a particular field, you'll need to build a matrix of rows and columns. For example, a DataTable.
Here's a class that should do the trick for you:
https://www.codeproject.com/Articles/11698/A-Portable-and-Efficient-Generic-Parser-for-Flat-F
Here's the sample usage code from the article, translated to VB:
Public Class CsvImporter
Public Sub Import()
Dim dsResult As DataSet
' Using an XML Config file.
Using parser As New GenericParserAdapter("MyData.txt")
parser.Load("MyData.xml")
dsResult = parser.GetDataSet()
End Using
' Or... programmatically setting up the parser for TSV.
Dim strID As String, strName As String, strStatus As String
Using parser As New GenericParser()
parser.SetDataSource("MyData.txt")
parser.ColumnDelimiter = vbTab.ToCharArray()
parser.FirstRowHasHeader = True
parser.SkipStartingDataRows = 10
parser.MaxBufferSize = 4096
parser.MaxRows = 500
parser.TextQualifier = """"c
While parser.Read()
strID = parser("ID")
strName = parser("Name")
' Your code here ...
strStatus = parser("Status")
End While
End Using
' Or... programmatically setting up the parser for Fixed-width.
Using parser As New GenericParser()
parser.SetDataSource("MyData.txt")
parser.ColumnWidths = New Integer(3) {10, 10, 10, 10}
parser.SkipStartingDataRows = 10
parser.MaxRows = 500
While parser.Read()
strID = parser("ID")
strName = parser("Name")
' Your code here ...
strStatus = parser("Status")
End While
End Using
End Sub
End Class
There's also this from here, demonstrating DataTable usage:
Dim csv = "Name, Age" & vbCr & vbLf & "Ronnie, 30" & vbCr & vbLf & "Mark, 40" & vbCr & vbLf & "Ace, 50"
Dim reader As TextReader = New StringReader(csv)
Dim table = New DataTable()
Using it = reader.ReadCsvWithHeader().GetEnumerator()
If Not it.MoveNext() Then
Return
End If
For Each k As var In it.Current.Keys
table.Columns.Add(k)
Next
Do
Dim row = table.NewRow()
For Each k As var In it.Current.Keys
row(k) = it.Current(k)
Next
table.Rows.Add(row)
Loop While it.MoveNext()
End Using
And this Q&A illustrates how to sort the DataTable by a given column.

Type mismatch error using custom class subroutine in Excel VBA

Working in Excel VBA, I have a class module where I define my class 'Marker'. One of the properties of my class is TextLine(), which is an array that holds up to 5 strings. I have defined the two methods below in my class module. In another (regular) module, I fill markerArr() with my custom Marker objects. Loading each object's properties with data at each array index is working fine... However, after loading data into the object at each index, I try to use markerArr(count).ProcessLines but receive a type mismatch error. Since ProcessLines is a public sub in my class module, and markerArr(count) contains a Marker object, I can't seem to understand why this error is occurring... Am I overlooking something obvious?
'Serial number replacement processing function
Public Sub ProcessLines()
Dim strSerial As String
Dim toggle As Boolean
toggle = False
Dim i As Integer
For i = 0 To 4
If Trim(m_TxtLines(i)) <> "" Then
'Add linefeed char to non-empty text lines
m_TxtLines(i) = m_TxtLines(i) & Chr(10)
'Detect if it is a serialized line
If InStr(1, m_TxtLines(i), "XXXXXX-YYY") > 0 Then
m_Serial(i) = True
toggle = True
End If
End If
Next
'When at least one line on the marker is serialized, create and replace serial text
If toggle = True Then
'Only prompt for input once
If startSerNo < 1 And Num_Sers < 1 Then
startSerNo = InputBox("Enter the serial number to start printing at." & Chr(10) & _
"Entering 1 will result in -001, entering 12 will result in -012, etc.", "Starting Serial #", "1")
Num_Sers = InputBox("Enter the amount of serializations to perform." & Chr(10) & _
"This will control how many copies of the entire marker set are printed.", "Total Serializations", "1")
End If
strSerial = CreateSerial(startSerNo)
Dim j As Integer
For j = 0 To 4
If m_Serial(j) Then
m_TxtLines(j) = Replace(m_TxtLines(j), "XXXXXX-YYY", strSerial)
End If
Next
End If
End Sub
'Creates the string to replace XXXXXX-YYY by concatenating the SFC# with the starting serial number
Private Function CreateSerial(ByVal startNum As Integer)
Dim temp
temp = SFC_Num
Select Case Len(CStr(startNum))
Case 1
temp = temp & "-00" & startNum
Case 2
temp = temp & "-0" & startNum
Case 3
temp = temp & "-" & startNum
Case Else
temp = temp & "-001"
End Select
CreateSerial = temp
End Function
Your CreateSerial function takes an integer as a parameter, but you are attempting to pass a string. I've pointed out some problems:
If startSerNo < 1 And Num_Sers < 1 Then 'Here I assume, you have these semi-globals as a variant - you are using numeric comparison here
startSerNo = InputBox("Enter the serial number to start printing at." & Chr(10) & _
"Entering 1 will result in -001, entering 12 will result in -012, etc.", "Starting Serial #", "1") 'Here startSerNo is returned as a string from the inputbox
Num_Sers = InputBox("Enter the amount of serializations to perform." & Chr(10) & _
"This will control how many copies of the entire marker set are printed.", "Total Serializations", "1") 'here Num_Sers becomes a String too
End If
strSerial = CreateSerial(startSerNo) 'here you are passing a String to the CreateSerial function. Either pass an integer, or allow a variant as parameter to CreateSerial
'......more code.....
Private Function CreateSerial(ByVal startNum As Integer)

consolidate items in datagridview visual basic.net 2008

Dim X As String
Dim V, Q, Y As Double
DGV.ColumnCount = 3
con.Open()
cmd = New SqlCommand("select Name,Price from Items where Name ='" & ListItems.SelectedItem & "'", con)
DR = cmd.ExecuteReader
While DR.Read()
' Q = Val(Qty.Text)
X = (DR("Name").ToString())
Y = Val((DR("Price").ToString()))
V = Q * Y
Dim row As String() = New String() {Q, X, V}
DGV.Rows.Add(row)
i am using visual basic.net and if i have similar items in datagridview as below
for example
1 hot dog 5 $
2 hot dog 10 $
5 hot dog 20 $
how can we consolidate them in one line as
8 hot dog 40 $
So you have columns "Name" and "Price". From your text under your code I see that name is going to be "5 hotdog" and Price to be "20$". I don't know if they are formatted that way, but I am going to assume so.
So what you want to do first is calculate the values you want in your "total" row. Since you have "hot dog" or a string after your initial number, we want to just get that number for each value. We'll loop through, evaluate that number, and sum it up. We'll do the same with the price column, but instead we'll remove the "$" in the string. Again, I'm doing a lot of assuming here.
Dim nameTotal As Integer = 0
Dim priceTotal As Integer = 0
'loop through each row of the DGV
For Each row As DataRow In DGV.Rows
'evaluate value in current row
Dim str_nameValue As String = row.Item("Name").ToString()
str_nameValue = str_nameValue.Remove(" hot dog")
'or if there are other string attached, remove those from the str_nameValue here to get an integer
'processing stringcode here...
'add to name total
nameTotal += CInt(str_nameValue)
'do the same for the other column
Dim str_priceValue As String = row.Item("Price").ToString()
str_priceValue = str_priceValue.Remove("$")
'sum
priceTotal += CInt(str_priceValue)
Next
'for loop is finished, add a new row with the values
'add row to table with a parameter of total values, and an empty one for the second column
DGV.Rows.Add(nameTotal & " hot dog " & priceTotal & "$", " ")
You should be in charge of formatting your strings to meet your needs.

Append text to existing row in datatable

I'm trying to make a calendar in vb.net and I have come across this problem. I want to append some text into an existing datatable row. When I watch my debugger it says:"In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user.".
Dim aantalRijen As Integer = 1
For x = 0 To 6
Dim dttopdrachten As New DataTable
dttopdrachten = opdrachtendao.getOpdrachtenByDate(Today.AddDays(x))
If dttopdrachten.Rows.Count > aantalRijen Then
aantalRijen = dttopdrachten.Rows.Count
End If
Next
For z = 0 To aantalRijen - 1
Dim r As DataRow
r = dttAgenda.NewRow()
dttAgenda.Rows.InsertAt(r, z)
Next
For i = 0 To 6
Dim aantalItems As Integer = 0
Dim dttopdrachten As New DataTable
dttopdrachten = opdrachtendao.getOpdrachtenByDate(Today.AddDays(i))
aantalItems = dttopdrachten.Rows.Count
For j = 0 To aantalItems - 1
Dim info As String = dttopdrachten.Rows(j).Item(0).ToString & vbCrLf & dttopdrachten.Rows(j).Item(2).ToString & vbCrLf & dttopdrachten.Rows(j).Item(3).ToString & vbCrLf & dttopdrachten.Rows(j).Item(4).ToString & vbCrLf & dttopdrachten.Rows(j).Item(5).ToString & vbCrLf & dttopdrachten.Rows(j).Item(6).ToString
dttAgenda.Rows(j).Item(i) = info
Next
Next
dgvAgenda.DataSource = dttAgenda
In the code above, I first count how many rows I have to make. Afterwards I add the amount of rows to the datatable (columns are added before). Until here it works, but then when I keep debugging I get the error. I tried googling but nothing could help me so far.
Seem problem has been solved without changing anything. So if someone want to make a calendar. Here's the solution ;)