Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 can't insert data &get a return value at same time

Author  Topic 

darenkov
Yak Posting Veteran

90 Posts

Posted - 2005-01-27 : 10:59:50
here is my pickle:

i have created and tested a nice little stored procedure which inserts some data, and returns the user_id value via sql's @@INDENTIY

the problem is that when i try to run the proc in my asp.net page, it isn't returning the user_id. i am using the command.ExecuteNonQuery method to run the procedure, however the problem is that the only value the method is permitted to return is the number of affected rows. this is useless to me as i want it to return my ouput value from my proc.

i obviously can't use the command.ExecuteReader, as it is only for reading, and my proc inserts data as well as returning a value. i am not having any luck with command.ExecuteScalar either. Any ideas if there is a work-around? this is driving me nuts

SamC
White Water Yakist

3467 Posts

Posted - 2005-01-27 : 12:10:09
Please post your code. The .NET, not the SQL.
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2005-01-27 : 12:30:21
Your SP looks fine, but it seemed a little over-coded the way the COMMIT / ROLLBACK was coded.

I'd ask someone else here about whether it's better to use SCOPE_IDENTITY(). IDENT_CURRENT isn't scoped, and depending on what's going on, could return the wrong value for this SP.

Also - does SET reset @@ERROR? I generally capture @@ERROR immediately so I am not sure if what you have coded here captures the error or not.

CREATE PROCEDURE uspAddFreeMember1
@rank tinyint ,
@business_name varchar(50) ,
@address varchar(75) ,
@city varchar(10) = null,
@state varchar(3) ,
@suburb_town varchar(30) ,
@postcode char(4) ,
@phone varchar(10) ,
@fax varchar(10) = null,
@tot_emps varchar(6) ,
@internet varchar(10) ,
@email_address varchar(55) ,
@pword varchar(25)
AS

DECLARE @Result int
SET @Result = 0 -- Setup a return value

-- Check that email doesn't already exist

IF EXISTS(select * from Login where email_address = @email_address) BEGIN
RETURN @Result
END

BEGIN TRANSACTION TransPage1

-- Insert basic Business details

INSERT INTO Business(rank,business_name,address,city,state,suburb_town,postcode,phone,fax, tot_emps,internet)
VALUES(@rank,@business_name,@address,@city,@state,@suburb_town,@postcode,@phone,@fax,@tot_emps,@internet)

DECLARE @BusinessID INT
SET @BusinessID = SCOPE_IDENTITY()

-- Insert Login Information

INSERT INTO Login(email_address, pword)
VALUES(@email_address, @pword)

DECLARE @LoginID INT
SET @LoginID = SCOPE_IDENTITY()

-- Insert LoginBusiness IDs into Lookup Table

INSERT INTO LoginBusiness(login_id, business_id)
VALUES(@BusinessID, @LoginID)

SET @ERR = @@ERROR -- what if either of the two prior INSERTS failed?


IF @ERR <> 0
ROLLBACK TRANSACTION TransPage1
ELSE BEGIN
COMMIT TRANSACTION TransPage1
SET @Result = @BusinessID

END
Return @Result
GO
Go to Top of Page

darenkov
Yak Posting Veteran

90 Posts

Posted - 2005-01-27 : 12:49:53
Hi Sam. I've been doing some reading and apparently I need to use the command.type method and some other stuff. I've looked at a few examples but I am not sure if I can pull it off as my proc inserts into 3 different tables. Here is my code:


Dim MyConnection As New SqlConnection()
Dim cmd As New SqlCommand
Dim adapter As New SqlDataAdapter

' run stored procedure uspAddFreeMember1

Dim strInsert As String
strInsert = "Declare @return_result As int EXEC @return_result = uspAddFreeMember1 "
strInsert &= "'12', '" & txtBusinessName.Text & "', '" & txtAddress.Text & "', '" & strCity.Text & "', '"
strInsert &= strAppState & "', '" & txtSuburbTown.Text & "', '" & txtPostcode.Text & "', '" & txtTelephone.Text & "', '"
strInsert &= txtFax.Text & "', '" & ddlEmployees.SelectedItem.Text & "', '" & ddlInternet.SelectedItem.Text & "', '"
strInsert &= txtEmailAddress.Text & "', '" & txtPassword1.Text & "' "
strInsert &= "PRINT @return_result"

MyConnection.ConnectionString = ConfigurationSettings.AppSettings("ConnectionString")
cmd.Connection = MyConnection

Dim intResult As Integer
Dim strURL As String

MyConnection.Open()
cmd.CommandText = strInsert
intResult = cmd.Execute()
MyConnection.Close()

If intResult = 0 Then
lblMessage.Text = intResult
'response.redirect("message.aspx?mid=1")
Else
lblMessage.Text = intResult
'strURL = "join_basic2.aspx?bid=" & intResult
'response.redirect(strURL)
End Ifo
Go to Top of Page

chadmat
The Chadinator

1974 Posts

Posted - 2005-01-27 : 12:51:28
Yes, you should be using SCOPE_IDENTITY()

Why don't you return it as an output parameter?

-Chad

http://www.clrsoft.com

Software built for the Common Language Runtime.
Go to Top of Page

darenkov
Yak Posting Veteran

90 Posts

Posted - 2005-01-27 : 12:55:52
Hi again Sam, just had a look at the changes you've made to the sproc. Thanks for the tips. I only taught myself sprocs in the last 3 days so I am still an amateur. I used to manipulate the SQL data purely in .net routines prior to this, and am pretty excited as sprocs are so much better.
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2005-01-27 : 13:00:51
Google ".net ado return_value" and look at some examples. Here's one:

  sFName = "George W." 
sLName = "Bush"

Set oCon = Server.CreateObject("ADODB.Connection")
Set oCom = Server.CreateObject("ADODB.Command")

oCon.CursorLocation = adUseClient
oCon.open "your connection string goes here"

with oCom
.activeconnection = oCon
.commandtext = "MyProcedureToSaveRecord"
.commandtype = adCmdStoredProc
.Parameters.Append .CreateParameter("@RETURN_VALUE", adInteger, adParamReturnValue, 0)
.Parameters.Append .CreateParameter("@FName", adVarWChar, adParamInput, 40, sFName)
.Parameters.Append .CreateParameter("@LName", adVarWChar, adParamInput, 40, sLName)
.Parameters.Append .CreateParameter("@NewID", adInteger, adParamOutput, 0)
end with

oCom.Execute , , adExecuteNoRecords

if err.number = 0 then
Select Case trim(oCom("@RETURN_VALUE"))
Case "0": sNewID = trim(oCom("@NewID"))
Case Else: ' received an error
End Select
else
response.write err.description
end if

If oCon.STATE = adStateOpen Then oCon.Close

Set oCom = nothing
Go to Top of Page

darenkov
Yak Posting Veteran

90 Posts

Posted - 2005-01-27 : 13:13:51
cheers :)
Go to Top of Page
   

- Advertisement -