473,396 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Adding a row to a DataTable before setting its values

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
2 3411
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Shravan | last post by:
Hi, I have a extended datagrid, in which I am setting values at a given cell in the grid using grid = val; The grid has NewRow creation set to false But sometimes setting value at a...
4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
2
by: Viorel | last post by:
Adding new row with default values. In order to insert programmatically a new row into a database table, without direct "INSERT INTO" SQL statement, I use the well-known DataTable.NewRow,...
6
by: Robert Schuldenfrei | last post by:
Dear NG, After being away from C# programming for a spell, I am trying my hand at what should be a simple task. I have been hitting my head against the wall this morning. I have a simple order...
10
by: Trevor | last post by:
Hey, I am trying to do this tutorial on the microsoft site : http://msdn.microsoft.com/library/default.asp? url=/library/en-us/dndotnet/html/usingadonet.asp I can get everything to work up to...
6
by: Dennis | last post by:
I have set a DataTable and one of the columns I set "AutoIncrement" to True. I then populate the Table by setting the columns to values then add the row to the table. I inadverently set the...
3
by: diesel | last post by:
Ok, once again I'm at my wits' end with a VB.Net problem. I have a button which opens a wizard that allows the user to enter a new record to a table. I have a datatable called dtItems that...
4
by: Sam | last post by:
Hi, I'm adding columns to a datatable as followed. The values are from textboxes or comboboxes. The first column is properly field but then all the subsequent columns just contain "", whereas the...
2
by: canoewhiteh2o | last post by:
I am having trouble adding data columns to a disconnected database. I first load a datatable using: private DataTable dtMacros = new DataTable(); private ArrayList macro = new ArrayList();...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.