DataTable.NewRow and DataTable.LoadDataRow

  • Thread starter Thread starter Ashish Sheth
  • Start date Start date
A

Ashish Sheth

Hi ADO.NET gurus,

What is the difference between DataTable.NewRow and DataTable.LoadDataRow?

Actually I want to find a row in the datatable. If the row is not there in
the datatable then I will create a row and add it to the datatable, but I am
not going to specify all the values of the columns in that newly added
Datarow. In this scenario which method should I use? Also when should I use
the BeginDataLoad and EndDataLoad method of the DataTable?
 
LoadDataRow will take a set of values and first try to match them with an
existing row by comparing PK values. If none are found, then it attempts to
add the row. If they are found, the values of the original row will be
changed. NewRow isn't for you if you want to find an existing row. This
will simply create a new row with the schema of the datatable that you based
it on. It will have a DataRowState of Detached if you just create it but
don't call DataTable.Rows.Add(RowName)

As far as the second part, you usually use those in conjunction with
LoadDataRow although you aren't physically prohibited from doing it
otherwise

HTH,

Bill

--

W.G. Ryan, eMVP

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
 
Ashish,

I use the Select method of the DataTable to find rows. Then if the
resulting array of rows has length <=0 I create a new row, populate the
fields and add it to the table.

It gets around all of the issues with primary keys and such.

-Sam Matzen
 
Back
Top