473,467 Members | 2,491 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 17515
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 ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.