473,785 Members | 2,824 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ExecuteNonQuery insert error

Jim
I'm trying to take the data from strSQL below and "append / insert" that
into the Access database. The problem is: although the listbox displays
correctly, the Access table gets the first record inserted to the table
multiple times - as many rows as there are in the source table.

Obviously the Access parameters aren't getting updated properly.

I've tried putting the .parameters.Add outside the for - next loop. No joy.

Also, if there is an easier or more elegant way to do this please advise.

Jim

Dim THGConnect As String = _
"Provider=SQLOL EDB;" & _
<.... etc......>
Dim objConnection As New OleDbConnection (THGConnect)
Dim strSQL As String = "select top 10 docs_id, docs_document
from thg_docs_docume nt_store"
Dim objCommand As New OleDbCommand(st rSQL, objConnection)
Dim objDataAdapter As New OleDbDataAdapte r(objCommand)
Dim objDataTable As New Data.DataTable( "temp_table ")
Dim objDataRow As DataRow

Dim AConnect As String = _
"Provider=Micro soft.Jet.OLEDB. 4.0;" & _
"Data Source=C:\Acses sDB.mdb;"
Dim AccessConnectio n As New OleDbConnection (AConnect)
Dim AccessCommand As New OleDbCommand
AccessCommand.C ommandText = "usp_InsertDocs "
AccessCommand.C ommandType = CommandType.Sto redProcedure
AccessCommand.C onnection = AccessConnectio n

AccessConnectio n.Open()

Try
objConnection.O pen()
objDataAdapter. Fill(objDataTab le)

For Each objDataRow In objDataTable.Ro ws
ListBox1.Items. Add(objDataRow. Item("docs_id") )
ListBox1.Items. Add(objDataRow. Item("docs_docu ment"))

'add parametes to query
AccessCommand.P arameters.Add(" @field1", OleDbType.Char,
32).Value = objDataRow.Item ("docs_id")
AccessCommand.P arameters.Add(" @field2", OleDbType.Char,
50).Value = objDataRow.Item ("docs_document ")

RowsAffected = AccessCommand.E xecuteNonQuery( )
Next
<snip>
May 11 '07 #1
1 3555
Jim
I did get it to work through trial and error. The AccessCommand and
AccessAdapter must me Dim'd within the dataRow loop.

See the correction below:

Jim wrote:
I'm trying to take the data from strSQL below and "append / insert" that
into the Access database. The problem is: although the listbox displays
correctly, the Access table gets the first record inserted to the table
multiple times - as many rows as there are in the source table.

Obviously the Access parameters aren't getting updated properly.

I've tried putting the .parameters.Add outside the for - next loop. No
joy.

Also, if there is an easier or more elegant way to do this please advise.

Jim

Dim THGConnect As String = _
"Provider=SQLOL EDB;" & _
<.... etc......>
Dim objConnection As New OleDbConnection (THGConnect)
Dim strSQL As String = "select top 10 docs_id, docs_document
from thg_docs_docume nt_store"
Dim objCommand As New OleDbCommand(st rSQL, objConnection)
Dim objDataAdapter As New OleDbDataAdapte r(objCommand)
Dim objDataTable As New Data.DataTable( "temp_table ")
Dim objDataRow As DataRow

Dim AConnect As String = _
"Provider=Micro soft.Jet.OLEDB. 4.0;" & _
"Data Source=C:\Acses sDB.mdb;"
Dim AccessConnectio n As New OleDbConnection (AConnect)
REMOVE== Dim AccessCommand As New OleDbCommand
REMOVE== AccessCommand.C ommandText = "usp_InsertDocs "
REMOVE== AccessCommand.C ommandType = CommandType.Sto redProcedure
REMOVE== AccessCommand.C onnection = AccessConnectio n

REMOVE== AccessConnectio n.Open()

Try
objConnection.O pen()
objDataAdapter. Fill(objDataTab le)

For Each objDataRow In objDataTable.Ro ws
ListBox1.Items. Add(objDataRow. Item("docs_id") )
ListBox1.Items. Add(objDataRow. Item("docs_docu ment"))

INSERT== Dim AccessCommand As New OleDbCommand
INSERT== AccessCommand.C ommandText = "usp_InsertDocs "
INSERT== AccessCommand.C ommandType = CommandType.Sto redProcedure
INSERT== AccessCommand.C onnection = AccessConnectio n

INSERT== AccessConnectio n.Open()
'add parametes to query
AccessCommand.P arameters.Add(" @field1", OleDbType.Char,
32).Value = objDataRow.Item ("docs_id")
AccessCommand.P arameters.Add(" @field2", OleDbType.Char,
50).Value = objDataRow.Item ("docs_document ")

RowsAffected = AccessCommand.E xecuteNonQuery( )
Next
<snip>
May 16 '07 #2

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

Similar topics

10
3401
by: Mike | last post by:
I know this sounds strange but I am at a loss. I am calling a simple funtion that opens a connection to a SQL Server 2000 database and executes an Insert Statement. private void AddMinimunWageStipen(string payrollid,double amount) { System.Data.SqlClient.SqlConnection cn = null; System.Data.SqlClient.SqlCommand cm = null;
1
4274
by: Matthew Louden | last post by:
I just tried to create a simple ASP.NET application to add record to the SQL Server database. However, it has run time error on Line 88: objCommand.ExecuteNonQuery() Any ideas?? Please help!! Thanks!! Sub AddNewRecord() Dim objConnection As SqlConnection Dim strConnection As String = "Data Source=20.11.12.91;Network Library=DBMSSOCN;Initial Catalog=testtable;User ID=sa;Password=iw;"
2
1736
by: Marcel | last post by:
Hi, I'm trying to insert a number of records in a SQL server table using the executenonquery method. This works fine except when the records already exist which results in a runtime error. Is there a way to prevent this runtime error and just ignore that the records can't be added ?
0
1385
by: Winshent | last post by:
I have some code which attempts to load the contents of a dataset into a sql server db. the code fills the dataadapter with the datset no prob, but fails on 'ExecuteNonQuery'. the function arr here returns the following where 'XLData' is my dataset. arr(1) = INSERT INTO UP_1_Results_Hourly ( SessionID, mDate, HourID,
4
6506
by: MDW | last post by:
Hey all. I'm confused. I'm trying to add a single record into an Access 2000 database using ASP.Net. Here is the code: objConn = New OleDbConnection(strConnect) objConn.Open objCommand = New OleDbCommand("INSERT INTO LOGIN_MASTER (LOGIN_ID, PWD, F_NAME, L_NAME, TYPE_ID) VALUES ('" & strEmail & "','" & strPwd & "','" &
2
1528
by: jdb | last post by:
Hi, I am adding a record to the database using an ExecuteNonQuery, which adds without problem. Now after the record is added I run a method passing in some info as well as the curretnly opened connection (byRef cn as OleDbConnection). Am using an Access 2000 database. Now in this new method I create a command object using the passed in connection object. I then create a DataReader to read the database that has the record added in the...
0
348
by: Damon | last post by:
I have an "InsertNewEmployee" function which has quite a few parameters in its INSERT statement. When I call the ExecuteNonQuery method, I get an OleDBException overflow exception. Any idea what gives? I checked the string lengths for the record to be inserted that's failing, and none of them are over the length limit in the Access database I'm using. The two integer parameters aren't overflowing either. So it's not that.
2
3026
by: jzogg7272 | last post by:
In my code I am executing a stored procedure to do a single row insert. I check the return value of the execution and I am getting -1, whereas a few weeks ago it was returning 0. Actually, I found that all of my insert stored procs are returning -1. The stored proc/insert statement is still executing successfully but the return code is different. If I execute the procedure from SQL Query Analyzer with the same params, it shows a return...
22
4329
by: b_r | last post by:
Hi, I'm trying to make a simple operation (insert into DB) in VB 2005 and SQL Server. The code is as follows: Dim sConnectionString As String = _ "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB.mdf....
0
10324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9949
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.