• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

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

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
rgn2121
Asked:
rgn2121
  • 9
  • 5
3 Solutions
 
DhaestCommented:
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
 
rgn2121Author 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.

0
 
DhaestCommented:
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
rgn2121Author 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
0
 
DhaestCommented:
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
 
rgn2121Author 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?
0
 
rgn2121Author Commented:
No...unfortunately I don't for what I am doing...
0
 
rgn2121Author 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.
0
 
DhaestCommented:
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
 
rgn2121Author Commented:
And that edits the DataTable correct...not just the DataRow array element?
0
 
DhaestCommented:
Normally it should. Try it out
0
 
rgn2121Author 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

0
 
rgn2121Author 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...
0
 
rgn2121Author Commented:
Thanks...great link!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 9
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now