HOWTO: Create a new row from a webservice proxy class
Posted: 12/2/2008 1:20:54 PM
By: Comfortably Anonymous
Times Read: 1,992
0 Dislikes: 0
Topic: Programming: .NET Framework

Quite a few times, I've needed to create a new row in a table obtained from a web service.

However, just doing a 'new WebService.SomeType.DataModel.SomeTypeRow()' would fail because it doesn't have a public constructor. I was really puzzled and would resort to a hack method of just retreiving an existing row, clearing out all the data, and using it as my new row object. Terrible approach, but I didn't know a better way.

Finally found it - it was there all along, but hadn't noticed it.

Say you've got a DataTable called dtSomeTable obtained from a web service that contains data in the format of SomeTableRow and needed to add a row to the in-memory copy of the data. Just use the (Yes, I was blind to this before) method dtSomeTable.NewRow() to obtain a new copy of it.

I simplified that a bit, you have to cast the NewRow to the proper row type (SomeTableRow in this example) before it works.

Here's a rough example:

Middleware.WebService.SomeType.DataModel.SomeTable someTable = bllSomething.GetAll();

Middleware.WebService.SomeType.DataModel.SomeTableRow rowNew = someTable.NewRow() as Middleware.WebService.SomeType.DataModel.SomeTableRow;

There you go, you now have a new row to add to the table. Just populate it with data, then add it to the table as:

someTable.Rows.Add(rowNew);

Much easier than my original hacky solution!

Rating: (You must be logged in to vote)