473,625 Members | 2,600 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic Insert Statment

I have an application that create an insert statment on the fly (it would be to complex to create stored procedures for all senarios) like:
insert into Table (field1, field2,field3 ,field4 ) VALUES ('Test','Test', 0,0)
so fare so good but I would like my statement to return the new PK_ID

So I tryed this:

Dim lNewVariable As String = "DECLARE @ID INT insert into insert into Table (field1, field2,field3 ,field4 ) VALUES ('Test','Test', 0,0) SET @ID = @@Identity"

Dim objCmd As New SqlClient.SqlCo mmand(lNewVaria ble, objConnection)

objCmd.Paramete rs.Add("@ID", SqlDbType.BigIn t).Direction = ParameterDirect ion.ReturnValue

objCmd.ExecuteN onQuery()

TextBox1.Text = objCmd.Paramete rs("@ID").Valu e

But I get 0 as return value.

Any ideas?

I cannot use the keywords OUTPUT NOR RETURN; I get errors.

any help would be apreciated.

thank you all in advance.

Feb 6 '06 #1
2 1608
Denis,

One option is to execute an Insert statememt using ExecuteNonQuery and then
execute a select @@Identity using ExecuteScalar:

'Execute the Insert statement:
objCmd.ExecuteN onQuery()

'Get the auto-generated primary key
objCmd.CommandT ext = "Select @@IDENTITY"
TextBox1.Text = objCmd.ExecuteS calar

Kerry Moorman
"Denis Correard" wrote:
I have an application that create an insert statment on the fly (it would be to complex to create stored procedures for all senarios) like:
insert into Table (field1, field2,field3 ,field4 ) VALUES ('Test','Test', 0,0)
so fare so good but I would like my statement to return the new PK_ID

So I tryed this:

Dim lNewVariable As String = "DECLARE @ID INT insert into insert into Table (field1, field2,field3 ,field4 ) VALUES ('Test','Test', 0,0) SET @ID = @@Identity"

Dim objCmd As New SqlClient.SqlCo mmand(lNewVaria ble, objConnection)

objCmd.Paramete rs.Add("@ID", SqlDbType.BigIn t).Direction = ParameterDirect ion.ReturnValue

objCmd.ExecuteN onQuery()

TextBox1.Text = objCmd.Paramete rs("@ID").Valu e

But I get 0 as return value.

Any ideas?

I cannot use the keywords OUTPUT NOR RETURN; I get errors.

any help would be apreciated.

thank you all in advance

Feb 6 '06 #2
You have "insert into" twice in your sqlcommand.

In article <#t************ **@TK2MSFTNGP09 .phx.gbl>,
de************@ shaw.co.uk says...
I have an application that create an insert statment on the fly (it would be to complex to create stored procedures for all senarios) like:
insert into Table (field1, field2,field3 ,field4 ) VALUES ('Test','Test', 0,0)
so fare so good but I would like my statement to return the new PK_ID

So I tryed this:

Dim lNewVariable As String = "DECLARE @ID INT insert into insert into Table (field1, field2,field3 ,field4 ) VALUES ('Test','Test', 0,0) SET @ID = @@Identity"

Dim objCmd As New SqlClient.SqlCo mmand(lNewVaria ble, objConnection)

objCmd.Paramete rs.Add("@ID", SqlDbType.BigIn t).Direction = ParameterDirect ion.ReturnValue

objCmd.ExecuteN onQuery()

TextBox1.Text = objCmd.Paramete rs("@ID").Valu e

But I get 0 as return value.

Any ideas?

I cannot use the keywords OUTPUT NOR RETURN; I get errors.

any help would be apreciated.

thank you all in advance.


Feb 7 '06 #3

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

Similar topics

1
4105
by: Maria | last post by:
Hello I have not used db2 in a long time, and I have this very long insert script, which is giving me an error of duplicated inserts. My question is how can I put something in the statment so that it will ignore this message ? Thank you all Maria
2
6429
by: 73blazer | last post by:
Perhaps my thinking is wrong but this is what I have: 1 table (Tab1) with 1 attribute (Attr1) Attr1 char(16) for bit data ----------------------------------------------- create trigger check no cascade before insert on Tab1 referencing new as N
8
2042
by: 73blazer | last post by:
Hello, I'm looking for a way to make some of my insert templates more readable by placing comments in between the values. I cannot seem to find a way to do this with DB2, is there a way? I'm probably missing something basic. I have insert into ken.snyder id1,id2,id3 values (
1
925
by: Agnes | last post by:
SELECT @sql = 'where voucherno =' + quotename(@voucherno,'''') + ' and recid = ' + str(@recid) As I print the above statment to check , the syntax is correct Where voucherno ='GL123' and recid = 123 However, I got this error "Unclosed quotation mark before the character string 'and recid = 123' Please help From AGnes
7
3383
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always either AND or OR but never mixed together. We can use Northwind database for my question, it is very similar to the structure of the problem on the database I am working on. IF(SELECT OBJECT_ID('REPORT')) IS NOT NULL DROP TABLE REPORT_SELECTION
1
5552
by: italian464 | last post by:
Have some mercy on me, I am a NEWBIE! :rolleyes: I was wondering if in VB.NET it was possible to take a combobox and use that as part of a select statement to change a label. Obviously this is done through an event handler, but I am not sure how to set up the code to use the selected value of the combo box and insert it in a sql statment. I have no sample code, for I am lost in even starting this part of the project. Please help anybody?!
1
1580
by: zeebiggie | last post by:
Good morning I have a form with the controls in the insert statment below. table1 has an Auto increment primary key hence is omitted in the insert statment and form. Am getting the error It didnt work " .mysql_insert_id(); and not any other php or mysql error to tell me if anything is wrong the insert statment. To top it up, I have an echo statment of the insert statment before it executes, when I copy and paste the insert statment from...
8
3666
by: slb813 | last post by:
Hello, I am having a problem that I can't seem to work out. I am running Apache 2.2 on a Windows XP platform, using PHP5. I am attempting to insert a row into a MS Access data base with a PHP script. I have a good connection into the data base, because select's work well. The error that I am getting is: Warning: odbc_exec(): SQL error: Too few parameters. Expected 5., SQL state 07001 in SQLExecDirect in C:\xampp\htdocs\test.php on...
0
3491
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button ,another row will be created with the same control (I mean another dropdown and 2 button) and so on. and by...
0
8251
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8352
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7178
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5570
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
4085
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...
0
4188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1800
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1496
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.