inserting new row in datatable

J

joel

Hi guys,

Im new to vb.net and i have this problem

how do i insert another row into my datatable... (i guess it's like
creating a for loop or something i really have no idea)... in this
code i can only insert a single row.

help pls...

very much appreciate,

Joel

Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSave.Click
Dim WarehouseDataTable As DataTable
WarehouseDataTable = New DataTable("Warehousing")

'creating a table named Customers
Dim Row1 As DataRow


'declaring three rows for the table

Try

Dim Department As DataColumn = New
DataColumn("Department")
'declaring a column named Name
Department.DataType = System.Type.GetType("System.String")
'setting the datatype for the column
WarehouseDataTable.Columns.Add(Department)
'adding the column to table

Dim SalesGLCode As DataColumn = New DataColumn("Sales GL
Code")
SalesGLCode.DataType =
System.Type.GetType("System.String")
WarehouseDataTable.Columns.Add(SalesGLCode)

Dim COSGLCode As DataColumn = New DataColumn("Cost Of
Sales")
COSGLCode.DataType = System.Type.GetType("System.String")
WarehouseDataTable.Columns.Add(COSGLCode)

Dim InventoryGLCode As DataColumn = New
DataColumn("Inventory")
InventoryGLCode.DataType =
System.Type.GetType("System.String")
WarehouseDataTable.Columns.Add(InventoryGLCode)

Dim ExpenseGLCode As DataColumn = New DataColumn("Expense
GL Code")
ExpenseGLCode.DataType =
System.Type.GetType("System.String")
WarehouseDataTable.Columns.Add(ExpenseGLCode)

Dim ReadersFeeGLCode As DataColumn = New
DataColumn("Reader's Fee GL Code")
ReadersFeeGLCode.DataType =
System.Type.GetType("System.String")
WarehouseDataTable.Columns.Add(ReadersFeeGLCode)

Row1 = WarehouseDataTable.NewRow
Row1.Item("Department") = Me.txtDepartment.Text
Row1.Item("Sales GL Code") = Me.txtSalesGLCode.Text
Row1.Item("Cost of Sales") = Me.txtCOSGLCode.Text
Row1.Item("Inventory") = Me.txtInventoryGLCode.Text
Row1.Item("Expense GL Code") = Me.txtExpenseGLCode.Text
Row1.Item("Reader's Fee GL Code") =
Me.txtReadersFeeGLCode.Text
WarehouseDataTable.Rows.Add(Row1)


Catch
End Try

Dim dsWarehouse As New DataSet
dsWarehouse = New DataSet
'creating a dataset

dsWarehouse.Tables.Add(WarehouseDataTable)
''adding the table to dataset

Var_Warehouse.SetDataBinding(dsWarehouse, "warehousing")
''binding the table to datagrid



End Sub
 
K

Karl

Hi guys,

Im new to vb.net and i have this problem

how do i insert another row into my datatable... (i guess it's like
creating a for loop or something i really have no idea)... in this
code i can only insert a single row.

help pls...

very much appreciate,

Joel

Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSave.Click
Dim WarehouseDataTable As DataTable
WarehouseDataTable = New DataTable("Warehousing")

'creating a table named Customers
Dim Row1 As DataRow

'declaring three rows for the table

Try

Dim Department As DataColumn = New
DataColumn("Department")
'declaring a column named Name
Department.DataType = System.Type.GetType("System.String")
'setting the datatype for the column
WarehouseDataTable.Columns.Add(Department)
'adding the column to table

Dim SalesGLCode As DataColumn = New DataColumn("Sales GL
Code")
SalesGLCode.DataType =
System.Type.GetType("System.String")
WarehouseDataTable.Columns.Add(SalesGLCode)

Dim COSGLCode As DataColumn = New DataColumn("Cost Of
Sales")
COSGLCode.DataType = System.Type.GetType("System.String")
WarehouseDataTable.Columns.Add(COSGLCode)

Dim InventoryGLCode As DataColumn = New
DataColumn("Inventory")
InventoryGLCode.DataType =
System.Type.GetType("System.String")
WarehouseDataTable.Columns.Add(InventoryGLCode)

Dim ExpenseGLCode As DataColumn = New DataColumn("Expense
GL Code")
ExpenseGLCode.DataType =
System.Type.GetType("System.String")
WarehouseDataTable.Columns.Add(ExpenseGLCode)

Dim ReadersFeeGLCode As DataColumn = New
DataColumn("Reader's Fee GL Code")
ReadersFeeGLCode.DataType =
System.Type.GetType("System.String")
WarehouseDataTable.Columns.Add(ReadersFeeGLCode)

Row1 = WarehouseDataTable.NewRow
Row1.Item("Department") = Me.txtDepartment.Text
Row1.Item("Sales GL Code") = Me.txtSalesGLCode.Text
Row1.Item("Cost of Sales") = Me.txtCOSGLCode.Text
Row1.Item("Inventory") = Me.txtInventoryGLCode.Text
Row1.Item("Expense GL Code") = Me.txtExpenseGLCode.Text
Row1.Item("Reader's Fee GL Code") =
Me.txtReadersFeeGLCode.Text
WarehouseDataTable.Rows.Add(Row1)

Catch
End Try

Dim dsWarehouse As New DataSet
dsWarehouse = New DataSet
'creating a dataset

dsWarehouse.Tables.Add(WarehouseDataTable)
''adding the table to dataset

Var_Warehouse.SetDataBinding(dsWarehouse, "warehousing")
''binding the table to datagrid

End Sub

It looks like youre creating a new table everytime the cmdSave button
is clicked.
Surely you want to create the table once, then add rows only when the
user clicks the button?

I hope this helps...
 
K

Karl

It looks like youre creating a new table everytime the cmdSave button
is clicked.
Surely you want to create the table once, then add rows only when the
user clicks the button?

I hope this helps...

Hi Joel...

I thought I'd try to be a bit more helpful and post some code...

I'm assuming this is windows forms, but the code would work just as
well if you made a few subtle changes for an asp page...

Create your form and call it frmDataTable...

Replace ALL the code in it (apart from the designer) with the code
shown below. run it and debug it and you'll see whats happening...

Imports System.Data

Public Class frmDataTable

Private oDataTable As New DataTable("Warehousing")

Private Sub frmDataTable_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load

Try

Dim Department As DataColumn = New DataColumn("Department")
Department.DataType = System.Type.GetType("System.String")
Dim SalesGLCode As DataColumn = New DataColumn("SalesGLCode")
SalesGLCode.DataType = System.Type.GetType("System.String")
Dim COSGLCode As DataColumn = New DataColumn("CostOfSales")
COSGLCode.DataType = System.Type.GetType("System.String")
Dim InventoryGLCode As DataColumn = New DataColumn("Inventory")
InventoryGLCode.DataType = System.Type.GetType("System.String")
Dim ExpenseGLCode As DataColumn = New DataColumn("ExpenseGLCode")
ExpenseGLCode.DataType = System.Type.GetType("System.String")
Dim ReadersFeeGLCode As DataColumn = New DataColumn("Reader's Fee
GL Code")
ReadersFeeGLCode.DataType = System.Type.GetType("System.String")
oDataTable.Columns.Add(Department)
oDataTable.Columns.Add(SalesGLCode)
oDataTable.Columns.Add(COSGLCode)
oDataTable.Columns.Add(InventoryGLCode)
oDataTable.Columns.Add(ExpenseGLCode)
oDataTable.Columns.Add(ReadersFeeGLCode)
Catch
MessageBox.Show("Failed to add columns to DataTable 'Warehousing'")
End Try
End Sub

Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSave.Click

Try
Dim oRow As DataRow
oRow = oDataTable.NewRow
oRow("Department") = Me.txtDepartment.Text
oRow("Sales GL Code") = Me.txtSalesGLCode.Text
oRow("Cost of Sales") = Me.txtCOSGLCode.Text
oRow("Inventory") = Me.txtInventoryGLCode.Text
oRow("Expense GL Code") = Me.txtExpenseGLCode.Text
oRow("Reader's Fee GL Code") = Me.txtReadersFeeGLCode.Text()
oDataTable.Rows.Add(oRow)
Catch ex As Exception
MessageBox.Show("Failed to add new row to DataTable 'Warehousing'")
End Try

End Sub
End Class
 
J

joel

Hi Joel...

I thought I'd try to be a bit more helpful and post some code...

I'm assuming this is windows forms, but the code would work just as
well if you made a few subtle changes for an asp page...

Create your form and call it frmDataTable...

Replace ALL the code in it (apart from the designer) with the code
shown below. run it and debug it and you'll see whats happening...

Imports System.Data

Public Class frmDataTable

Private oDataTable As New DataTable("Warehousing")

Private Sub frmDataTable_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load

Try

Dim Department As DataColumn = New DataColumn("Department")
Department.DataType = System.Type.GetType("System.String")
Dim SalesGLCode As DataColumn = New DataColumn("SalesGLCode")
SalesGLCode.DataType = System.Type.GetType("System.String")
Dim COSGLCode As DataColumn = New DataColumn("CostOfSales")
COSGLCode.DataType = System.Type.GetType("System.String")
Dim InventoryGLCode As DataColumn = New DataColumn("Inventory")
InventoryGLCode.DataType = System.Type.GetType("System.String")
Dim ExpenseGLCode As DataColumn = New DataColumn("ExpenseGLCode")
ExpenseGLCode.DataType = System.Type.GetType("System.String")
Dim ReadersFeeGLCode As DataColumn = New DataColumn("Reader's Fee
GL Code")
ReadersFeeGLCode.DataType = System.Type.GetType("System.String")
oDataTable.Columns.Add(Department)
oDataTable.Columns.Add(SalesGLCode)
oDataTable.Columns.Add(COSGLCode)
oDataTable.Columns.Add(InventoryGLCode)
oDataTable.Columns.Add(ExpenseGLCode)
oDataTable.Columns.Add(ReadersFeeGLCode)
Catch
MessageBox.Show("Failed to add columns to DataTable 'Warehousing'")
End Try
End Sub

Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSave.Click

Try
Dim oRow As DataRow
oRow = oDataTable.NewRow
oRow("Department") = Me.txtDepartment.Text
oRow("Sales GL Code") = Me.txtSalesGLCode.Text
oRow("Cost of Sales") = Me.txtCOSGLCode.Text
oRow("Inventory") = Me.txtInventoryGLCode.Text
oRow("Expense GL Code") = Me.txtExpenseGLCode.Text
oRow("Reader's Fee GL Code") = Me.txtReadersFeeGLCode.Text()
oDataTable.Rows.Add(oRow)
Catch ex As Exception
MessageBox.Show("Failed to add new row to DataTable 'Warehousing'")
End Try

End Sub
End Class- Hide quoted text -

- Show quoted text -

Thaks guys for the help i really appreciate it.
 

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

Top