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

Working with DataTables

P: n/a
I would like some example code that shows retrieving and
updating selected column data from a DataTable.

I am sucessful to load and select the whole row of a
DataTable into a datagrid. But can't seem to access just
a specific column on a specific row. Maybe it is a
simple solution that I need.

When I try to receive the data from the
column "strTxtGestHeadWord" into a TextBox like this:

TextBox2.Text = foundRows("strTxtGestHeadWord")

The error I get is:
Value of type 'System.Data.DataRow' cannot be converted
to 'String'.

Nov 20 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"Bob Achgill" <an*******@discussions.microsoft.com> schrieb
I would like some example code that shows retrieving and
updating selected column data from a DataTable.

I am sucessful to load and select the whole row of a
DataTable into a datagrid. But can't seem to access just
a specific column on a specific row. Maybe it is a
simple solution that I need.

When I try to receive the data from the
column "strTxtGestHeadWord" into a TextBox like this:

TextBox2.Text = foundRows("strTxtGestHeadWord")

The error I get is:
Value of type 'System.Data.DataRow' cannot be converted
to 'String'.


Do you have Option Strict On? How is foundRows declared?
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2

P: n/a
I tried turning off Option strict and got this error:
Additional information: Cast from
string "strTxtGestHeadWord" to type 'Integer' is not
valid.

Here are the declarations:

Dim strExpr As String
Dim strSort As String

' strExpr = "intWordCount = 3 & strTxtLangCode =
ASE"
' Sort descending by CompanyName column.
' strSort = "intWordCount ASC"
' Use the Select method to find all rows matching
the filter.
Dim foundRows As DataRow() =
WordsinSentenceTable.Select(strExpr, strSort,
DataViewRowState.Added)

TextBox2.Text = foundRows("strTxtGestHeadWord")

-----Original Message-----
"Bob Achgill" <an*******@discussions.microsoft.com>

schrieb
I would like some example code that shows retrieving and updating selected column data from a DataTable.

I am sucessful to load and select the whole row of a
DataTable into a datagrid. But can't seem to access just a specific column on a specific row. Maybe it is a
simple solution that I need.

When I try to receive the data from the
column "strTxtGestHeadWord" into a TextBox like this:

TextBox2.Text = foundRows("strTxtGestHeadWord")

The error I get is:
Value of type 'System.Data.DataRow' cannot be converted to 'String'.


Do you have Option Strict On? How is foundRows declared?
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

.

Nov 20 '05 #3

P: n/a

Here is a little more of the code so you can see how i am
trying to set up the DataTable.


' Create DataSet named DsnSentenceStuff.
Dim DsnSentenceStuff As New DataSet
("DsnSentenceStuff")
' Add one DataTable object, WordsinSentence
DsnSentenceStuff.Tables.Add(New DataTable
("WordsinSentence"))

' Create a Table in the dataset named
WordsinSentenceTable
Dim WordsinSentenceTable As DataTable
WordsinSentenceTable = New DataTable
("WordsinSentence")

' Add columns to table WordsinSentenceTable
WordsinSentenceTable.Columns.Add("intWordCount",
GetType(Int32))
WordsinSentenceTable.Columns.Add
("strWordInflect", GetType(String))
WordsinSentenceTable.Columns.Add("intKeyMeanid",
GetType(Int16))
WordsinSentenceTable.Columns.Add
("strTxtLangCode", GetType(String))
WordsinSentenceTable.Columns.Add
("strGestLangCode", GetType(String))
WordsinSentenceTable.Columns.Add
("strTransTxtLangCode", GetType(String))
WordsinSentenceTable.Columns.Add
("strTxtGestHeadWord", GetType(String))
WordsinSentenceTable.Columns.Add
("strKeyHeadWord", GetType(String))
WordsinSentenceTable.Columns.Add("strGestBinary",
GetType(String))
WordsinSentenceTable.Columns.Add
("strTransGestHeadword", GetType(String))

' Set PrimaryKey
WordsinSentenceTable.Columns
("intWordCount").Unique = True
WordsinSentenceTable.PrimaryKey = New DataColumn
() {WordsinSentenceTable.Columns("intWordCount")}
' Insert code to fill tables with columns and
data.
' Binds the DataGrid to the DataSet, displaying
the WordsinSentence table.

'Hmmm??? Why is the right datasource a table and
not a dataset??
'DataGrid2.SetDataBinding
(DsnSentenceStuff, "WordsinSentence")

DataGrid2.DataSource = WordsinSentenceTable
Dim myDataRow As DataRow

Dim i As Integer
For i = 0 To 4
myDataRow = WordsinSentenceTable.NewRow()
myDataRow("intWordCount") = i
myDataRow("strWordInflect") = "Item " +
i.ToString()
myDataRow("intKeyMeanid") = i * 2
myDataRow("strTxtLangCode") = "ASE"
myDataRow("strGestLangCode") = "Item " +
i.ToString()
myDataRow("strTransTxtLangCode") = "Item " +
i.ToString()
myDataRow("strTxtGestHeadWord") = "Item " +
i.ToString()
myDataRow("strGestBinary") = "Item " +
i.ToString()
myDataRow("strTransGestHeadword") = "Item " +
i.ToString()

WordsinSentenceTable.Rows.Add(myDataRow)
Next i

' Not sure I need this unless will update a
database using a data adapter.
'WordsinSentenceTable.AcceptChanges()

' YES! The data row has the data but does it make
it in to the dataset (or is it Data Table)??
TextBox2.Text = myDataRow("intWordCount") & " " &
myDataRow("strWordInflect")
Dim strExpr As String
Dim strSort As String

' strExpr = "intWordCount = 3 & strTxtLangCode =
ASE"
' Sort descending by CompanyName column.
' strSort = "intWordCount ASC"
' Use the Select method to find all rows matching
the filter.
Dim foundRows As DataRow() =
WordsinSentenceTable.Select(strExpr, strSort,
DataViewRowState.Added)

TextBox2.Text = foundRows("strTxtGestHeadWord")
-----Original Message-----
"Bob Achgill" <an*******@discussions.microsoft.com>

schrieb
I would like some example code that shows retrieving and updating selected column data from a DataTable.

I am sucessful to load and select the whole row of a
DataTable into a datagrid. But can't seem to access just a specific column on a specific row. Maybe it is a
simple solution that I need.

When I try to receive the data from the
column "strTxtGestHeadWord" into a TextBox like this:

TextBox2.Text = foundRows("strTxtGestHeadWord")

The error I get is:
Value of type 'System.Data.DataRow' cannot be converted to 'String'.


Do you have Option Strict On? How is foundRows declared?
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

.

Nov 20 '05 #4

P: n/a
"Bob Achgill" <an*******@discussions.microsoft.com> schrieb
I tried turning off Option strict and got this error:
Additional information: Cast from
string "strTxtGestHeadWord" to type 'Integer' is not
valid.

Here are the declarations:

Dim strExpr As String
Dim strSort As String

' strExpr = "intWordCount = 3 & strTxtLangCode =
ASE"
' Sort descending by CompanyName column.
' strSort = "intWordCount ASC"
' Use the Select method to find all rows matching
the filter.
Dim foundRows As DataRow() =
WordsinSentenceTable.Select(strExpr, strSort,
DataViewRowState.Added)

TextBox2.Text = foundRows("strTxtGestHeadWord")


foundRows is an array. You want to get the value of the strTxtGestHeadWord
column, but of which row? As foundRows is an array, you must specifiy the
index within the array to access an item in the array: foundrows(7) returns
the 8th row in the array.

Dim row as datarow

row = foundRows(7)
TextBox2.Text = row("strTxtGestHeadWord").ToString

- or in one go -

TextBox2.Text = foundRows(7)("strTxtGestHeadWord").ToString

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #5

P: n/a
Hi Bob,

I have not so much time at the moment (have to go in some minutes) and
cannot check your problem completly, however I can give you some knowledge
of a dataset that is

dataset.tables(x).rows(x).item(x)

In other words
A dataset contains tables which contains rows which contains items
or better
A dataset have references to tables which have references to rows which have
references to items

I hope this helps a little bit.

Cor
Cor
Nov 20 '05 #6

P: n/a
Thanks!

That worked very nicely.

Now I have uncommented the line:
strExpr = "intWordCount = 3 & strTxtLangCode = ASE"

So I can narrow the select results to one line (one row)
in the array.

But the compiler does not like the way I write the line.

Any suggestions?

-----Original Message-----
"Bob Achgill" <an*******@discussions.microsoft.com> schrieb
I tried turning off Option strict and got this error:
Additional information: Cast from
string "strTxtGestHeadWord" to type 'Integer' is not
valid.

Here are the declarations:

Dim strExpr As String
Dim strSort As String

' strExpr = "intWordCount = 3 & strTxtLangCode = ASE"
' Sort descending by CompanyName column.
' strSort = "intWordCount ASC"
' Use the Select method to find all rows matching the filter.
Dim foundRows As DataRow() =
WordsinSentenceTable.Select(strExpr, strSort,
DataViewRowState.Added)

TextBox2.Text = foundRows("strTxtGestHeadWord")


foundRows is an array. You want to get the value of the

strTxtGestHeadWordcolumn, but of which row? As foundRows is an array, you must specifiy theindex within the array to access an item in the array: foundrows(7) returnsthe 8th row in the array.

Dim row as datarow

row = foundRows(7)
TextBox2.Text = row("strTxtGestHeadWord").ToString

- or in one go -

TextBox2.Text = foundRows(7) ("strTxtGestHeadWord").ToString
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

.

Nov 20 '05 #7

P: n/a
"Bob Achgill" <an*******@discussions.microsoft.com> schrieb
Thanks!

That worked very nicely.

Now I have uncommented the line:
strExpr = "intWordCount = 3 & strTxtLangCode = ASE"

So I can narrow the select results to one line (one row)
in the array.

But the compiler does not like the way I write the line.


Really the compiler? My compiler accepts the line. Have a look at the docs
of the Select function. It points us to the Datacolumn.Expression property
documentation. There the whole syntax of these expressions are described.
There you will also find the "And" operator (not "&"). Use also single or
double quotes around strings:

strExpr = "intWordCount = 3 AND strTxtLangCode = 'ASE'"

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #8

P: n/a
The string worked great! The AND did the trick.

Now that I have been able to load a row in a DataTable.
Select a row using a search expression to narrow the
search to a particular row and column.

How do i update that distinct row/column value
programmatically?

It is like I would like to use SQL Update with a where
clause but it seems that DataTables are not that
sophisticated. Maybe I am trying to use the DataTables
functionality for more than what they are intended for.

You really are helping me sooo much! Thank you.

You are helping us toward making a free literacy program
that will help folks to read in any language. It is for
those who find themsleves being born in a developing
country and have no teacher available. It will have 3D
sign language assist for the deaf trying to learn to
read, too.


Dim myDataRow As DataRow

Dim i As Integer
For i = 0 To 4
myDataRow = WordsinSentenceTable.NewRow()
myDataRow("intWordCount") = i
myDataRow("strWordInflect") = "Item " +
i.ToString()
myDataRow("intKeyMeanid") = i * 2
myDataRow("strTxtLangCode") = "ASE"
myDataRow("strGestLangCode") = "Item " +
i.ToString()
myDataRow("strTransTxtLangCode") = "Item " +
i.ToString()
myDataRow("strTxtGestHeadWord") = "Item " +
i.ToString()
myDataRow("strGestBinary") = "Item " +
i.ToString()
myDataRow("strTransGestHeadword") = "Item " +
i.ToString()

WordsinSentenceTable.Rows.Add(myDataRow)
Next i
TextBox2.Text = myDataRow("intWordCount") & " " &
myDataRow("strWordInflect")
Dim strExpr As String
Dim strSort As String

strExpr = "intWordCount = 4 AND strTxtLangCode
= 'ASE'"

' Use the Select method to find all rows matching
the filter.
Dim foundRows As DataRow() =
WordsinSentenceTable.Select(strExpr, strSort,
DataViewRowState.Added)
TextBox2.Text = foundRows(0)
("strTxtGestHeadWord").ToString

' Now ... how to update strTxtGestHeadWord at that
location??

Nov 20 '05 #9

P: n/a
"Bob Achgill" <an*******@discussions.microsoft.com> schrieb
How do i update that distinct row/column value
programmatically?
TextBox2.Text = foundRows(0)
("strTxtGestHeadWord").ToString

' Now ... how to update strTxtGestHeadWord at that
location??


Is
foundRows(0)("strTxtGestHeadWord") = TextBox2.Text
what you're looking for? What does "at that location" mean? Nothing has
changed meanwhile.
--
Armin

Nov 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.