473,406 Members | 2,336 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,406 software developers and data experts.

Working with DataTables

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
9 5558
"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
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

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
"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
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
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
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: William Ryan | last post by:
At the risk of sounding like a Big 5 consultant, "It depends". 1) Strongly typed datasets rock, they are faster than untyped, use intellisense... but your reason for wanting to use them is...
2
by: Jade | last post by:
Hi, I just wanted to ask a quick question regarding datasets. I am creating 3 tables using a dataadapter. what i want to know is that is the relationship created between these datatables...
0
by: Gene Ariani | last post by:
I have one dataset that contains two DataTables: DataTable1: Customer_ID 1
7
by: Luis Esteban Valencia | last post by:
I have this i need to show the datagrid with columns of 2 datatables Dim myadap As New SqlDataAdapter myconn.Open() myadap.TableMappings().Add("Table", "vistaUsuarios")
3
by: cj | last post by:
I've used datatables and datasets before. Datasets being able to hold more than one table and datatables being only one table. My mind keeps coming up with recordsets. I can't remember how they...
3
by: bbdobuddy | last post by:
Hi, I have two datatables that I want to left outer join and then do some queries on but I having a hard time figuring out how to join the datatables together. One of the datatables comes from...
2
by: riltim | last post by:
Hi, I have a situation where I need to take multiple DataTables and convert them to a single Datatable which would be simple if I didn't need to convert them into a matrix where like items will...
0
by: StefanPienaar | last post by:
Hi Guys Is there any way in c# (or vb.net) to extract a datatable of data from a dataset with multiple datatables which has relationships set up (containing combined data from the datatables)? ...
4
by: stephen | last post by:
Hi all, I am working with Excel. I read an excel document using ExcelReader and lets say it has 10 columns. I have to read each record and based on a specified column peform some activites and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...

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.