DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Create data table in ASP.NET 2.0(VB.NET)

This tutorial will show you how to create data table without SQL server in ASP.NET and VB.NET 2.0. This is very useful for storing temporary data. Add a GridView control, two lable controls, three textbox controls and button control in the page

Create datatable structure.

Private Function CreateDataTable() As DataTable
Dim myDataTable As DataTable = New DataTable()

Dim myDataColumn As DataColumn

myDataColumn = New DataColumn()
myDataColumn.DataType = Type.GetType("System.String")
myDataColumn.ColumnName = "id"
myDataTable.Columns.Add(myDataColumn)

myDataColumn = New DataColumn()
myDataColumn.DataType = Type.GetType("System.String")
myDataColumn.ColumnName = "username"
myDataTable.Columns.Add(myDataColumn)

myDataColumn = New DataColumn()
myDataColumn.DataType = Type.GetType("System.String")
myDataColumn.ColumnName = "firstname"
myDataTable.Columns.Add(myDataColumn)

myDataColumn = New DataColumn()
myDataColumn.DataType = Type.GetType("System.String")
myDataColumn.ColumnName = "lastname"
myDataTable.Columns.Add(myDataColumn)

Return myDataTable
End Function

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

Insert data into datatable.

Private Function AddDataToTable(ByVal username As String, ByVal firstname As String, ByVal lastname As String, ByVal myTable As DataTable)
Dim row As DataRow

row = myTable.NewRow()

row("id") = Guid.NewGuid().ToString()
row("username") = username
row("firstname") = firstname
row("lastname") = lastname

myTable.Rows.Add(row)
End Function

Add data to datatable which we have created

Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
If txtUserName.Text.Trim() = "" Then
Me.lblTips.Text = "You must fill a username."
Return
Else
AddDataToTable(Me.txtUserName.Text.Trim(), Me.txtFirstName.Text.Trim(), Me.txtLastName.Text.Trim(), CType(Session("myDatatable"), DataTable))

Me.GridView1.DataSource = CType(Session("myDatatable"), DataTable).DefaultView

Me.GridView1.DataBind()

Me.txtFirstName.Text = ""
Me.txtLastName.Text = ""
Me.txtUserName.Text = ""
Me.lblTips.Text = ""
End If
End Sub

Be noted "Session["myDatatable"] = myDt;" is important to ensure we can add new data continually until the page be closed.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
If Me.IsPostBack = False Then
myDt = New DataTable()
myDt = CreateDataTable()
Session("myDatatable") = myDt

Me.GridView1.DataSource = (CType(Session("myDatatable"), DataTable)).DefaultView
Me.GridView1.DataBind()
End If
End Sub

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!


Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!