James123
James123

Reputation: 11652

'Field' is not a member of 'System.Data.DataRow'

I am using VS2005 for vb.net. I am getting compile error at below statement. How to fix this?

    For Each rw As DataRow In data.Rows
        For Each dc As DataColumn In stringColumns
            rw.Field(Of String)(dc).Replace("_x00D_", "") //Error showing here
        Next
    Next

Upvotes: 11

Views: 15984

Answers (3)

James Lawruk
James Lawruk

Reputation: 31337

This error will also occur if you don't explicitly cast the DataRow. (The original poster does this correctly.)

Use this:

For Each rw As DataRow In data.Rows

Not this:

For Each rw In data.Rows

Upvotes: 2

user166390
user166390

Reputation:

Update 2: Up-vote the other more direct/clear answer :-)


Update: while the following still applies, there is a DataRow.Field provided by the DataRowExtensions class; make sure it is referenced.


Because there is no member called Field defined over the DataRow type... perhaps the special Item property was meant instead?

Note in the example where it is used as an indexer (row("Name")). Neat, huh?

(Also, something should be done with the result of the Replace as strings are immutable in .NET.)

Happy coding.

Upvotes: 10

Damith
Damith

Reputation: 63065

Add a reference to System.Data.DataSetExtensions.dll then your code will work. Field is a Extension method you need to add the reference otherwise it will not work.

VS 2005 with .net 2.0 ?

then you can't add reference to this dll. you need to target .net 3.5 or above to use these extension methods.

Upvotes: 28

Related Questions