?
Solved

When setting a DataRow = DataTable.Select(), an error message displays.

Posted on 2009-02-13
14
Medium Priority
?
309 Views
Last Modified: 2013-11-26
I have a piece of code like what is below.  I have also looked it up around the web and it seems people are doing it the same way.  I am curious why it will not compile and shows as an error.
Dim tempRow As DataRow
tempRow = WeekExcelTable.Select("ID = '" & drv.Item("ID") & "' AND Num = '" & _
                                                drv.Item("NR") & "'")

Open in new window

0
Comment
Question by:rgn2121
  • 9
  • 5
14 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 total points
ID: 23631270
It must be:
Dim tempRow() As DataRow
tempRow = WeekExcelTable.Select("ID = '" & drv.Item("ID") & "' AND Num = '" & _
                                                drv.Item("NR") & "'")
' Now you can loop your records

For Each r In tempRow
        For Each c In r.Table.Columns
            Console.Write( "\t {0}", r(c) )
        Next c
        Console.WriteLine()
    Next r
0
 
LVL 12

Author Comment

by:rgn2121
ID: 23631281
If I edit the items in tempRow, will it also edit them in the WeekExcelTable?  I am searching for a certain row of data and then I will need to edit it if it is found.

0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 2000 total points
ID: 23631295
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Author Comment

by:rgn2121
ID: 23631301
I thought tempRow was going to have the usual edit routines...I just realized that since it is an array I don't have those.  Is there a way for me to select a row and edit it without doing:

For Each row as DataRow in WeekExcelTable.Rows
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23631307
Do you have an unique id ?

Dim customerRow() As Data.DataRow
customerRow = DataSet1.Tables("Customers").Select("CustomerID = 'ALFKI'")

customerRow(0)("CompanyName") = "Updated Company Name"
customerRow(0)("City") = "Seattle"
0
 
LVL 12

Author Comment

by:rgn2121
ID: 23631309
OKay...so since I will return only one array item with my Select then I can use index zero if the array and it will update the data in the DataTable correct?
0
 
LVL 12

Author Comment

by:rgn2121
ID: 23631311
No...unfortunately I don't for what I am doing...
0
 
LVL 12

Author Comment

by:rgn2121
ID: 23631318
My Select though is like what I posted above and I will only return one row with that query above.  So then I can still do the index of the Datarow() and be good with that.
0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 2000 total points
ID: 23631323
You can always use the indexer on your select-result
Dim tempRow() As DataRow
tempRow = WeekExcelTable.Select("ID = '" & drv.Item("ID") & "' AND Num = '" & _
                                                drv.Item("NR") & "'")
...
Example:
tempRow (0)("CompanyName") = "Updated Company Name"
tempRow (0)("City") = "Seattle"
0
 
LVL 12

Author Comment

by:rgn2121
ID: 23631329
And that edits the DataTable correct...not just the DataRow array element?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23631341
Normally it should. Try it out
0
 
LVL 12

Author Comment

by:rgn2121
ID: 23631416
Okay...I was able to do the following which is pretty much the same and it seemed to work very well although I didn't want to take the time to verify all the data as there might be a few things I need to tweak yet.


 Dim tempRow() As DataRow
tempRow = WeekExcelTable.Select("ID = '" & drv.Item("ID") & "' AND Num = '" & _
                                                drv.Item("NR") & "'")
If tempRow.Length > 0 Then
   tempRow(0).BeginEdit()
   ...edit the items...
   tempRow(0).EndEdit()
   tempRow(0).AcceptChanges()
Array.Clear(tempRow, 0, tempRow.Length)
End if

Open in new window

0
 
LVL 12

Author Comment

by:rgn2121
ID: 23631422
If you see anything off in that let me know, but at quick glance it seemed to work very well and is a lot less code then what I was doing with the For Each row as DataRow...
0
 
LVL 12

Author Closing Comment

by:rgn2121
ID: 31546496
Thanks...great link!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question