By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,589 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

Adding a row to a DataTable before setting its values

P: n/a
Something I've found quite useful to do in my code is to select a record
from my database which may or may not exist, and then if I find that it
doesn't exist to add it to the datatable before I set the field values. For
example:
Dim dt As DataTable
dim dr As DataRow
[...]
'Does the row already exist in the table?
If dt.Rows.Count = 0 Then
'No, so add a new row to the datatable
dr = dt.NewRow
dt.Rows.Add(dr)
'Set the primary key
dr("PKField") = PKValue
Else
'The row exists so get a reference to it
dr = dt.Rows(0)
End If
'Set the other fields
dr("OtherField") = OtherValue
'Update the database
da.Update(dt)
This is convenient because it keeps all the "row doesn't exist" processing
at the start of the code (in the If statement). If I were to add the row to
the datatable after I've set all the field values, I'd need a second If
statement at the end of the code too (just before the call to da.Update)
which checked to ensure the row was actually new, and not an existing row
being updated.

This seems to work fine for me, but every single example I've seen that adds
new rows to a datatable has added it after populating all the values instead
of before. Is there are reason why I should avoid using the above code?

--

(O)enone
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Oenone,

No, nothing wrong with the way you're doing it, but you have to find it
again, now that it exists, in order to add the other column information.
Seems like a bit of double work to me. And, you'll now surely have to find
it on its PK, because anything else might return more than one row.

HTH,

Bernie Yaeger

"Oenone" <no***@nowhere.com> wrote in message
news:Oe**************@tk2msftngp13.phx.gbl...
Something I've found quite useful to do in my code is to select a record
from my database which may or may not exist, and then if I find that it
doesn't exist to add it to the datatable before I set the field values.
For example:
Dim dt As DataTable
dim dr As DataRow
[...]
'Does the row already exist in the table?
If dt.Rows.Count = 0 Then
'No, so add a new row to the datatable
dr = dt.NewRow
dt.Rows.Add(dr)
'Set the primary key
dr("PKField") = PKValue
Else
'The row exists so get a reference to it
dr = dt.Rows(0)
End If
'Set the other fields
dr("OtherField") = OtherValue
'Update the database
da.Update(dt)
This is convenient because it keeps all the "row doesn't exist" processing
at the start of the code (in the If statement). If I were to add the row
to the datatable after I've set all the field values, I'd need a second If
statement at the end of the code too (just before the call to da.Update)
which checked to ensure the row was actually new, and not an existing row
being updated.

This seems to work fine for me, but every single example I've seen that
adds new rows to a datatable has added it after populating all the values
instead of before. Is there are reason why I should avoid using the above
code?

--

(O)enone

Nov 21 '05 #2

P: n/a
Bernie Yaeger wrote:
No, nothing wrong with the way you're doing it, but you have to find
it again, now that it exists, in order to add the other column
information. Seems like a bit of double work to me. And, you'll now
surely have to find it on its PK, because anything else might return
more than one row.


Oh sure -- I'd only do it like this when I'm selecting based on the primary
key. For example:

SELECT * FROM Users WHERE Username = 'MyUser'

(Username being the entire primary key). If this returned a row then I'd
want to update it, if it didn't I'd want to add it. That's the kind of
scenario I'm using this for.

Thanks for your comments,

--

(O)enone
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.