View Light

HOWTO: Create (Instantiate) a new DataTable row

While you'd think something like the following code would work, it doesn't and fails with a frustrating "No constructor defined" error:

Middleware.ProjectName.DAL.DataModel.SomeDataRow rowNew = new MiddleWare.ProjectName.DAL.DataModel.SomeDataRow();

The way to obtain an instantiated row object that belongs to a TableAdapter is as follows:

// First, instantiate a DataTable of the type the new row will belong to:
Middleware.ProjectName.DAL.DataModel.SomeDataTable dtSomeData = new Middleware.ProjectName.DAL.DataModel.SomeDataTable();

// Next, use the DataTable instance to create a new row that is the same "shape" as the DataTable:
Middleware.ProjectName.DAL.DataModel.SomeDataRow rowNew = dtSomeData.NewSomeDataRow();

It's easy as that, intuitive in retrospect, but a mind-twist to get the concept down the first time.

Anyone know a better way?

This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available in our efforts to advance understanding of environmental, political, human rights, economic, democracy, scientific, and social justice issues, etc. We believe this constitutes a 'fair use' of any such copyrighted material as provided for in section 107 of the US Copyright Law. In accordance with Title 17 U.S.C. Section 107, the material on this site is distributed without profit to those who have expressed a prior interest in receiving the included information for research and educational purposes. For more information go to: . If you wish to use copyrighted material from this site for purposes of your own that go beyond 'fair use', you must obtain permission from the copyright owner.