We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

Medium Priority
332 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

Comment
Watch Question

Project manager
CERTIFIED EXPERT
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
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.

Dirk HaestProject manager
CERTIFIED EXPERT
Commented:

Author

Commented:
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
Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
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"

Author

Commented:
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?

Author

Commented:
No...unfortunately I don't for what I am doing...

Author

Commented:
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.
Dirk HaestProject manager
CERTIFIED EXPERT
Commented:
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"

Author

Commented:
And that edits the DataTable correct...not just the DataRow array element?
Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
Normally it should. Try it out

Author

Commented:
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

Author

Commented:
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...

Author

Commented:
Thanks...great link!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.