Hi Dan,
Your code and approach have a number of problems. Please stick with me; I'm
not being critical... just pointing out the facts:
1. In every line where you have [comd.CommandText = ...] you are completely
changing the value of comd.CommandText. That is, each line *overwrites* the
previous value of CommandText. So, when you finally get around to
comd.ExecuteScalar(), the value of comd.CommandText is simply "select
@orderid".
2. The solution to the above problem (of overwriting the value of
CommandText in each successive line) is to concatenate the incremental
values, possibly via +=, and ensuring you add a blank space between each).
But you DON'T want to do that in your situation because of #3 below:
3. It appears that you are trying to create a stored procedure without
creating one (and instead putting all of the T-SQL in your CommandText.
There's no way that's going to work the way you are attempting.
What will work is to do the following:
1. Create a stored procedure that does the INSERT, followed immediately by
SET @orderid = SCOPE_IDENTITY, and returning exactly one result set via
SELECT @OrderID.
Then In your client code
2. Set CommandText = name of the stored procedure
3. Set ComandType = CommandType.StoredProcedure
4. Add to the Command.Parameters collection one SqlParameter object for each
of the parameters in the stored procedure.
5. Finally execute the stored procedure via the ExecuteScalar method (as you
were already trying to do).
The above assumes you have opened a connection etc..
-HTH
> Hi,
>
[quoted text clipped - 20 lines]
> Thanks
> Dan
Dan - 09 Jul 2007 00:25 GMT
Thanks, you're right of course with the concatenation.
But, instead of using a stored procedure (which i know is beter), would it
be posiible to do that in code-behind, more or less like this:
comd.CommandText = "DECLARE @orderid int," _
& "SET @orderid = SCOPE_IDENTITY()," _
& "select @orderid"
Dim x As Integer
x = Convert.ToInt32(comd.ExecuteScalar())
because i get the error:
Incorrect syntax near the keyword 'SET'.
Incorrect syntax near ',
Thanks again
> Hi Dan,
>
[quoted text clipped - 61 lines]
>> Thanks
>> Dan
Göran Andersson - 09 Jul 2007 00:37 GMT
> Thanks, you're right of course with the concatenation.
>
[quoted text clipped - 9 lines]
> Incorrect syntax near the keyword 'SET'.
> Incorrect syntax near ',
Use semicolon to separate the SQL statements, then it might work.
comd.CommandText = "DECLARE @orderid int;" _
& "SET @orderid = SCOPE_IDENTITY();" _
& "select @orderid"
But why not simply:
comd.CommandText = "select SCOPE_IDENTITY()"

Signature
Göran Andersson
_____
http://www.guffa.com
Bob Johnson - 09 Jul 2007 01:46 GMT
RE:
<< then it might work >>
Right - can you (op) please let us know if you get this to work? I'm
curious.
Bob Johnson - 09 Jul 2007 01:51 GMT
>> Thanks, you're right of course with the concatenation.
>>
[quoted text clipped - 19 lines]
>
> comd.CommandText = "select SCOPE_IDENTITY()"
A couple of other thoughts:
1. set comd.CommandType = CommandType.Text
2. In all those strings you are concatenating for the .CommandText value, be
sure to add white space where appropriate.
3. to test this, first get the script to work in query analyzer (SS2K) or
Management Studio (2005). Once it works there, then move it to your client
code.
-HTH
Dan - 09 Jul 2007 09:40 GMT
Yes, it works like this:
comd.CommandText = "insert into [mytable] (field1, field2) values(@datbeg
,@datend');" _
& " select SCOPE_IDENTITY()"
Thanks
Bob Johnson - 09 Jul 2007 14:27 GMT
How are you populating @datbeg and @datend? That query won't work unless you
send parameters. Is that your actual query?
Just curious. Thanks!
> Yes, it works like this:
>
[quoted text clipped - 3 lines]
>
> Thanks