By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,022 Members | 1,367 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,022 IT Pros & Developers. It's quick & easy.

pb with adding columns to a datatable

P: n/a
Sam
Hi,
I'm adding columns to a datatable as followed. The values are from
textboxes or comboboxes.
The first column is properly field but then all the subsequent columns
just contain "", whereas the source txtboxes or comboboxes values are
correct. Why is that ?

thx

Dim dt As New DataTable
Dim row As DataRow

dt.Columns.Add("FieldName", Type.GetType("System.String"))
row = dt.NewRow
row("FieldName") = txtFieldName.Text
dt.Rows.Add(row) --> contains "the right string"

dt.Columns.Add("EntryInfo", Type.GetType("System.String"))
row = dt.NewRow
row("EntryInfo") = txtEntryInfo.Text
dt.Rows.Add(row) --> contains "" instead of "a string"

Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
First of all - pb is the chemical symbol for lead and this is not an SMS
newsgroup so there is no need to abbreviate things.

The code you have shown will give you a table that looks like this:

FieldName EntryInfo Whatever
--------- --------- --------
Row 0 aaa
Row 1 bbb
Row 2 ccc

I assume that this is probably not what you want.

If, instead, you want this:

FieldName EntryInfo Whatever
--------- --------- --------
Row 0 aaa bbb ccc

then you need:

Dim dt As New DataTable
Dim row As DataRow
dt.Columns.Add("FieldName", Type.GetType("System.String"))
dt.Columns.Add("EntryInfo", Type.GetType("System.String"))
dt.Columns.Add("Whatever", Type.GetType("System.String"))
row = dt.NewRow
row("FieldName") = txtFieldName.Text
row("EntryInfo") = txtEntryInfo.Text
row("Whatever") = txtWhatever.Text
dt.Rows.Add(row)
"Sam" <sa**************@voila.fr> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hi,
I'm adding columns to a datatable as followed. The values are from
textboxes or comboboxes.
The first column is properly field but then all the subsequent columns
just contain "", whereas the source txtboxes or comboboxes values are
correct. Why is that ?

thx

Dim dt As New DataTable
Dim row As DataRow

dt.Columns.Add("FieldName", Type.GetType("System.String"))
row = dt.NewRow
row("FieldName") = txtFieldName.Text
dt.Rows.Add(row) --> contains "the right string"

dt.Columns.Add("EntryInfo", Type.GetType("System.String"))
row = dt.NewRow
row("EntryInfo") = txtEntryInfo.Text
dt.Rows.Add(row) --> contains "" instead of "a string"

Nov 21 '05 #2

P: n/a
Sam
Hehe... sorry for the sms style. And my error was stupid. Thanks a lot.

Nov 21 '05 #3

P: n/a
Sam,

I would change it in this typed in this message so watch typos..

\\\
Dim dt As New DataTable
dt.Columns.Add("FieldName", GetType(System.String))
dt.Columns.Add("EntryInfo", GetType(System.String))
dt.loaddatarow(new Object() {txtFieldName.Text,txtEntryInfo.Text},true)
///

A lot shorter and the rowstate is direct set to unchanged in this case
(otherwise false).

Cor
Nov 21 '05 #4

P: n/a
Sam
Thx Cor, looks good

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.