473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

inserting new row in datatable

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

Aug 8 '07 #1
3 17511
On 8 Aug, 08:26, joel <concept_...@hotmail.comwrote:
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...

Aug 8 '07 #2
On 8 Aug, 13:22, Karl <googlegro...@cortexa.co.ukwrote:
On 8 Aug, 08:26, joel <concept_...@hotmail.comwrote:
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...
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

Aug 10 '07 #3
On Aug 10, 11:20 pm, Karl <googlegro...@cortexa.co.ukwrote:
On 8 Aug, 13:22, Karl <googlegro...@cortexa.co.ukwrote:


On 8 Aug, 08:26, joel <concept_...@hotmail.comwrote:
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...

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.
Aug 21 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Eva | last post by:
Hi i am trying to insert a new row into one of my datatabels that i have in my dataset when a button is clicked. here is my code Dim ClientInsRow As DataRow = dtClient.NewRo ...
2
by: aamirghanchi | last post by:
How can I restrict a user from inserting more than certain number of rows into a datagrid?
2
by: a | last post by:
NEW Post Here's my best guess at how to insert this dataset.... the code runs, but no new records are added to the sql table. I've read and split a delimited text file into a dataset. It...
7
by: GaryB | last post by:
I have an untyped datatable that has financial numbers and controls that were populated by code (not a simple fill from a DA). Now I want to insert subtotals into it. I wrote a sub to do so that...
6
by: Pushpendra Vats | last post by:
Hi , I am trying to insert records into database. I am trying to write the following code. On button1 click event i am inserting five records and after that i am calling the update method of...
1
by: Cooper | last post by:
If I have this as my XML file <?xml version="1.0" standalone="yes" ?> - <opml> - <body> - <outline text="RssImporter OPML"> <outline title="CNN" htmlUrl=""...
3
by: rcoco | last post by:
Hi, I want to share this problem. I have a datagrid that will help me Insert data into sql database. So I made a button On my form so that when I press the button a new row on datagrid should be...
6
by: Manikandan | last post by:
Hi, I need to insert the datetime with milliseconds value into a datarow. My code as below DataTable testDataTable=new DataTable(); testDataTable.Columns.Add("updatedDateTime",...
4
by: Manikandan | last post by:
Hi, I'm inserting a datetime values into sql server 2000 from c# SQL server table details Table name:date_test columnname datatype No int date_t DateTime ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.