Creating and manipulating a dataTable

Category : ASP.Net, VB.Net

This structure keeps coming up :

 dsSchools = mydata
 
'Create table to bind to dataGrid
Dim dt As New DataTable
Dim nameCol, activeCol As New DataColumn
dt.Columns.Add(nameCol)
dt.Columns.Add(activeCol)
Dim dRow As DataRow = dt.NewRow
 
For Each dr As DataRow In dsSchools.Tables(0).Rows
       If Not dr("title") Is DBNull.Value Then dRow(nameCol) = dr("title")
       If Not dr("IsActive") Is DBNull.Value Then dRow(activeCol) = dr("isActive").ToString
       dt.Rows.Add(dRow)
Next

Sort a dataTable in VB.Net

Category : ADO / SQL, ASP.Net, VB.Net

I had a datatable that I needed to sort by a certain column.  Here is what I did, if you have a better idea, please let me know!

'Set up the variables for the sort:
Const Sort_Column_Key As String = "Test Order" 'This is your column name
Const Sort_Direction As String = " ASC"
Const Sort_Format As String = "{0} {1}"
 
'Sort the chartData dataTable so we print the data in the right order - This sorts
 chartData.DefaultView.Sort = String.Format(Sort_Format, Sort_Column_Key, Sort_Direction)
 
'Now for me to use it in my future FOR EACH loop, I had to create a new table to put the sorted data into... GOT A BETTER WAY? Let me know!
Dim chartData2 As DataTable = chartData.DefaultView.ToTable

This worked well for me! I hope it helps someone else! t

Setting the dataType of a Column when adding to a dataTable

Category : ADO / SQL, VB.Net

You want to add a column to a dataTable:
Dim testColumn As New DataColumn
Now you want to change the dataType (default is a string):
testColumn.DataType = Type.GetType(“System.Int32″)

Ya, nice and simple right… (would it have been too hard just to say ‘= Integer’???

(“System.String”) is the string…beyond that, you tell me.

Writing a DataTable to a MemoryStream and then out to a TextBox

Category : VB.Net

Here is an example of writing a datatable into a direct Memorystream and then writing that out to a textBox — this can all be done from an .aspx page and does not require a file to be involved.

 
        'CREATE A DATATABLE AND FILL IT WITH SOME DATA
        Dim dt As DataTable = New DataTable
        Dim dcID As New DataColumn("ID")
        Dim dcName As New DataColumn("Name")
 
        dt.Columns.Add(dcID)
        dt.Columns.Add(dcName)
 
        Dim row1, row2, row3 As DataRow
 
        row1 = dt.NewRow()
        row1.Item("ID") = "1232444"
        row1.Item("Name") = "Todd Eric Vance"
        dt.Rows.Add(row1)
 
        row2 = dt.NewRow()
        row2.Item("ID") = "154656232444"
        row2.Item("Name") = "Tod5454d Eric gggVance"
        dt.Rows.Add(row2)
 
        row3 = dt.NewRow()
        row3.Item("ID") = "1232444"
        row3.Item("Name") = "Tgfgfg fggfric Vyttance"
        dt.Rows.Add(row3)
 
 
        'CREATE MEMORYSTREAM AND STREAMWRITER
        Dim ms As MemoryStream = New MemoryStream
        Dim sw As StreamWriter = New StreamWriter(ms)
 
        'WRITE COLUMN NAMES TO STREAM
        Dim iColCount As Integer = dt.Columns.Count
 
        For i As Integer = 0 To iColCount - 1
 
            sw.Write(dt.Columns(i))
 
            If i < iColCount - 1 Then sw.Write(",")
 
        Next
 
        sw.WriteLine()
 
        'WRITE ROWS TO STREAM
        For Each dr As DataRow In dt.Rows
 
            For i As Integer = 0 To iColCount - 1
 
                If Not dr(i) Is DBNull.Value Then sw.Write(dr(i).ToString())
 
                If i < iColCount - 1 Then sw.WriteLine()
 
            Next
 
        Next
 
        sw.WriteLine()
 
        sw.Flush()
 
        'OPEN A READER - SET THE MEMORYSTREAM TO THE BEGINNING
        'WRITE IT TO A TEXTBOX
        Dim sr As New StreamReader(ms)
 
        ms.Seek(0, SeekOrigin.Begin)
 
        Dim mystring As String = sr.ReadToEnd
 
        tb1.Text = mystring
 
        sr.Close()

Random Data and DataTable generator using VB.Net

Category : VB.Net

Simple class here – but I needed just some random data in a datatable and I didn’t want to have to type it all in by hand.

I like this simple little class because one could easily change it to take the type and number of columns they need and then fill them randomly (creating random names could be done but was not implemented here– I have an idea at the bottom).

Anyway I create a class that will contain a table :
Public Class RandomDataTable

Public dt As New DataTable(“random”)
Public name As New DataColumn(“Name”, System.Type.GetType(“System.String”))
Public value As New DataColumn(“Value”, GetType(Integer))
Dim rndm As New Random
Dim dr As DataRow

Now we have a datatable that will have two columns.  Next I create the constructor to take in the number of needed rows (howManyResults)… as well as a minimum range for the data and a maximum range for the data.

Make random data set of 2 items and numbers
Public Sub New(ByVal howManyResults As Integer, ByVal rangeMin As Integer, ByVal     rangeMax As Integer)

Now I add the columns, loop through the number of results and fill in the two columns with Entity# and the Random data number ( I used a random object and called its min max function):

dt.Columns.Add(name)
dt.Columns.Add(value)

For i As Integer = 1 To howManyResults

Dim nameString = “Entity ” + i.ToString

Dim rowValue = rndm.Next(rangeMin, rangeMax)

dr = dt.NewRow()

dr.Item(name) = nameString
dr.Item(value) = rowValue

dt.Rows.Add(dr)

Next

And that is it!  I create the class like this  DIM MyRandomTable as dataTable = new RandomDataTable(50, 2, 100) and I have a nice filled in table of random data that I can then work with!

** ON THE NAME THING — I am right now planning on extending this by making me first names and last names… I plan on making a firstName array of about 5-10 names and a lastName array of 5-10 names and then in my loop – call a random function to grab random firsts and match them with random lasts… should be interesting.

Any thoughts or ideas on how to extend this???? Let me know!