Thursday, March 22, 2012

Simple Datatable Question

Can someone please tell me how I add information to a datatable!

I already have some information that has been pulled from an Active
Directory Database but I want to add additional information from an SQL
Server Database...

How do I achieve this?

Thanks

Private Sub createTable()
Dim tbcontacts As DataTable = New DataTable("contacts")
tbcontacts.Columns.Add(" ", System.Type.GetType("System.String"))
tbcontacts.Columns.Add("Name", System.Type.GetType("System.String"))
tbcontacts.Columns.Add("Dept.", System.Type.GetType("System.String"))
tbcontacts.Columns.Add("Ext", System.Type.GetType("System.String"))
tbcontacts.Columns.Add("Email", System.Type.GetType("System.String"))
ds.Tables.Add(tbcontacts)

End SubYou want to add DataRows in DataTable which is created in your sample code?

I have always added rows like this (C#)

tbcontacts.Rows.Add(new object[]{[column 1 value], [column 2
value],...[column n value]})


"Tim::.." wrote:

> Can someone please tell me how I add information to a datatable!
> I already have some information that has been pulled from an Active
> Directory Database but I want to add additional information from an SQL
> Server Database...
> How do I achieve this?
> Thanks
> Private Sub createTable()
> Dim tbcontacts As DataTable = New DataTable("contacts")
> tbcontacts.Columns.Add(" ", System.Type.GetType("System.String"))
> tbcontacts.Columns.Add("Name", System.Type.GetType("System.String"))
> tbcontacts.Columns.Add("Dept.", System.Type.GetType("System.String"))
> tbcontacts.Columns.Add("Ext", System.Type.GetType("System.String"))
> tbcontacts.Columns.Add("Email", System.Type.GetType("System.String"))
> ds.Tables.Add(tbcontacts)
> End Sub
Thanks... but not exactly what I wanted to know!

I have a database and usually use a data reader to access info from a
database but I want to add aditional info to an existing datatable...

Any ideas?

"Jouni Karppinen" wrote:

> You want to add DataRows in DataTable which is created in your sample code?
> I have always added rows like this (C#)
> tbcontacts.Rows.Add(new object[]{[column 1 value], [column 2
> value],...[column n value]})
>
>
>
>
> "Tim::.." wrote:
> > Can someone please tell me how I add information to a datatable!
> > I already have some information that has been pulled from an Active
> > Directory Database but I want to add additional information from an SQL
> > Server Database...
> > How do I achieve this?
> > Thanks
> > Private Sub createTable()
> > Dim tbcontacts As DataTable = New DataTable("contacts")
> > tbcontacts.Columns.Add(" ", System.Type.GetType("System.String"))
> > tbcontacts.Columns.Add("Name", System.Type.GetType("System.String"))
> > tbcontacts.Columns.Add("Dept.", System.Type.GetType("System.String"))
> > tbcontacts.Columns.Add("Ext", System.Type.GetType("System.String"))
> > tbcontacts.Columns.Add("Email", System.Type.GetType("System.String"))
> > ds.Tables.Add(tbcontacts)
> > End Sub
Why not just pull the data from the database into a 2nd datable, and then
copy all the rows from this new datatable to the tbContacts one?

you can do this something like:
for i as integer = 0 to databaseTable.Rows.Count - 1
tbContacts.Rows.Add(databaseTable.Rows(i).ItemArra y)
next

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)

"Tim::.." <myatix_at_hotmail.com> wrote in message
news:9D5BBF7B-EB6F-4812-BC5A-37E63B77E29E@.microsoft.com...
> Can someone please tell me how I add information to a datatable!
> I already have some information that has been pulled from an Active
> Directory Database but I want to add additional information from an SQL
> Server Database...
> How do I achieve this?
> Thanks
> Private Sub createTable()
> Dim tbcontacts As DataTable = New DataTable("contacts")
> tbcontacts.Columns.Add(" ", System.Type.GetType("System.String"))
> tbcontacts.Columns.Add("Name",
> System.Type.GetType("System.String"))
> tbcontacts.Columns.Add("Dept.",
> System.Type.GetType("System.String"))
> tbcontacts.Columns.Add("Ext", System.Type.GetType("System.String"))
> tbcontacts.Columns.Add("Email",
> System.Type.GetType("System.String"))
> ds.Tables.Add(tbcontacts)
> End Sub

0 comments:

Post a Comment