473,287 Members | 1,492 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,287 software developers and data experts.

ado.net adding a new record without having a dataset

I am adding a new record to a table via a dataset/adapter.

I have got the following to work for me but I am wondering if there is
a better way to do this.

I am having to have something in my DS before I can add records, since
DS does not exist. I'd rather not have a SELECT since I really don't
need any records in my dataset that I will be adding a new record to.

Dim dsNewRow As DataRow
Dim DS As New DataSet
Dim DA As New OleDb.OleDbDataAdapter

con.Open()
'part I have a question about
sql = "SELECT * FROM lookupTable WHERE Lname = ''"
DA = New OleDb.OleDbDataAdapter(sql, con)
DA.Fill(DS, "lookupTable")
'end
dsNewRow = DS.Tables("lookupTable").NewRow()
dsNewRow.Item(1) = "test string"
DS.Tables("lookupTable").Rows.Add(dsNewRow)
DA.Fill(DS, "lookupTable")
In my SQL command I have also used something like "SELECT * FROM
lookupTable WHERE LName = ''.

I am new to VB.net and want to make sure I learn the right method.

Mar 1 '07 #1
1 2489
Why do you think this is true:
I am having to have something in my DS before I can add records, since
DS does not exist.
Do you mean that you want to open a dataset just to insert records into a
table in your database, i.e. without selecting any records?

If you aren't going to use the table of inserted records, and you're just
going to use it to insert records, I would create an INSERT query and
execute it using ExecuteNonQuery for each record you want to insert.

Otherwise, your idea of selecting from the table with a primarykey = a
value it couldn't possibly be" will work fine. It's a bit tacky, but
effective.

If you choose the INSERT method, I'd check the performance. In my
experience working with Access with VB6 and ADO, if you are inserting a lot
of records (hundreds or thousands), this is slower than using a dataset. I
haven't tried it with ADO.Net, but I'd check it out if you were willing to
use INSERT queries.

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
----------------------------------------------------------------
"vbDavidC" <ch*************@yahoo.comwrote in message
news:11*********************@z35g2000cwz.googlegro ups.com...
>I am adding a new record to a table via a dataset/adapter.

I have got the following to work for me but I am wondering if there is
a better way to do this.

I am having to have something in my DS before I can add records, since
DS does not exist. I'd rather not have a SELECT since I really don't
need any records in my dataset that I will be adding a new record to.

Dim dsNewRow As DataRow
Dim DS As New DataSet
Dim DA As New OleDb.OleDbDataAdapter

con.Open()
'part I have a question about
sql = "SELECT * FROM lookupTable WHERE Lname = ''"
DA = New OleDb.OleDbDataAdapter(sql, con)
DA.Fill(DS, "lookupTable")
'end
dsNewRow = DS.Tables("lookupTable").NewRow()
dsNewRow.Item(1) = "test string"
DS.Tables("lookupTable").Rows.Add(dsNewRow)
DA.Fill(DS, "lookupTable")
In my SQL command I have also used something like "SELECT * FROM
lookupTable WHERE LName = ''.

I am new to VB.net and want to make sure I learn the right method.

Mar 4 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ilan Sebba | last post by:
I am trying to add a record using SQL. My problem is that the primary keys are foreign keys, and these foreign keys are autonumbers. I therefore do not know the primary keys of the record I am...
2
by: Jim | last post by:
In my Win App, I have a datagrid that's bound to a dataset. When the form loads, the datagrid fills. How can I add an empty row to the end of the datagrid during a button click (similar to...
0
by: Patrick | last post by:
I'm working on a contact management application, and need a hand with one aspect... Here's what I want to create: ------------------------------------ A form split into two parts. There is a...
4
by: Mike Hnatt | last post by:
My goal is to get data from an XML file into a couple of tables in an Access database. The XML file is a little complex so I need control over what I do (I can't just read it into a dataset). ...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
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...
9
by: Greg | last post by:
Binding Manager & dataset - won't add record I've got an untyped dataset with controls bound through code. The user can select a question number from a bound combobox, and the question number and...
6
by: Rudy | last post by:
Hi all, I know this is easy, just can't seem to get it. I have a windows form, and a text box, with a value already in it. I need to add that value to a table. It's just one value, so the entire...
1
by: DH | last post by:
I have an untyped dataset with a table. I am trying to programmatically add a row to this table. This was working in VS 2003 / .net 1.1 I am receiving an error "Object reference not set to an...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.