1

I have migrated legacy code from vb to c#. The following migrated line of code raises an error:

using System.Data;
..
..
DataRow targetDataRow = targetDataTable.NewRow();
targetDataRow.SetField("DateValue", calendarRow.DateValue.ToShortDateString);

The error:

=Error  CS1061  'DataRow' does not contain a definition for 'SetField' and no accessible extension method 'SetField' accepting a first argument of type 'DataRow' could be found (are you missing a using directive or an assembly reference?)

I know I can use it like:

targetDataRow["DateValue"] = calendarRow.DateValue.ToShortDateString);

But, is this the best practice to set the value to a field in DataRow object?

1
  • Refactoring the targetDataTable into a typed DataTable would be better. Then you may set field value like this: targetDataRow.DateValue = .... If you are migrating VB code it might be not a big extra work to accomplish this the same time. Commented Apr 19, 2021 at 13:35

1 Answer 1

1

The following migrated line of code raises an error:

 Error  CS1061  'DataRow' does not contain a definition for 'SetField' and no accessible extension method 'SetField' accepting a first argument of type 'DataRow' could be found (are you missing a using directive or an assembly reference?)

This error is because you're trying to use the DataRowExtension.SetField Method in which you don't have referenced in your project.

To add it, you can do so using the PM console:

 Install-Package System.Data.DataSetExtensions -Version 4.5.0-preview1-26216-02

Is this the best practice to set the value to a field in DataRow object

It doesn't matter if you set it or call the method in this case. Under the hood SetField is doing the exact same thing and nothing more.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.