Please help.....

M

M. Ali Qureshi

Hi,

I have following stored procedure that should return @UserID.

=======================
CREATE PROCEDURE [dbo].[CreateUserProfile]
@UserType int,
@BusinessStatus int,
@BusinessBranch int,
@BusinessForm int,
@BusinessType int,
@TotalEmployees nvarchar(50),
@TurnOver nvarchar(50)

AS
BEGIN
declare @UserID as int

SET NOCOUNT ON;

INSERT into Users (UserTypeID, BusinessStatusID, BusinessBranchID,
BusinessFormID, BusinessTypeID, TotalEmployees, TurnOver, Status)
values (@UserType, @BusinessStatus, @BusinessBranch, @BusinessForm,
@BusinessType, @TotalEmployees, @TurnOver, 0)
SELECT @UserID = scope_identity()
END
========================

I added the stored procedure in the table adapter, and called it through a
function in BLL class file.

When i run the stored procedure through an aspx page, i get only "0" value
back. Here is the code from aspx page.

======================
If Not HasError Then
Dim UserAdapter As New DBAccessBLL
Dim UserID As Integer
UserID = UserAdapter.InsertUserProfile(UserType.SelectedValue,
BusinessStatus.SelectedValue, BusinessBranch.SelectedValue,
BusinessForm.SelectedValue, BusinessType.SelectedValue, _Employees,
_TurnOver)
Response.Redirect("Start.aspx?u=" & UserID.ToString())
End If
=======================

What am i doing wrong? Could anyone please help me please?

Thanks alot.
 
M

Mick Walker

M. Ali Qureshi said:
Hi,

I have following stored procedure that should return @UserID.

=======================
CREATE PROCEDURE [dbo].[CreateUserProfile]
@UserType int,
@BusinessStatus int,
@BusinessBranch int,
@BusinessForm int,
@BusinessType int,
@TotalEmployees nvarchar(50),
@TurnOver nvarchar(50)

AS
BEGIN
declare @UserID as int

SET NOCOUNT ON;

INSERT into Users (UserTypeID, BusinessStatusID, BusinessBranchID,
BusinessFormID, BusinessTypeID, TotalEmployees, TurnOver, Status)
values (@UserType, @BusinessStatus, @BusinessBranch, @BusinessForm,
@BusinessType, @TotalEmployees, @TurnOver, 0)
SELECT @UserID = scope_identity()
END
========================

I added the stored procedure in the table adapter, and called it through
a function in BLL class file.

When i run the stored procedure through an aspx page, i get only "0"
value back. Here is the code from aspx page.

======================
If Not HasError Then
Dim UserAdapter As New DBAccessBLL
Dim UserID As Integer
UserID =
UserAdapter.InsertUserProfile(UserType.SelectedValue,
BusinessStatus.SelectedValue, BusinessBranch.SelectedValue,
BusinessForm.SelectedValue, BusinessType.SelectedValue, _Employees,
_TurnOver)
Response.Redirect("Start.aspx?u=" & UserID.ToString())
End If
=======================

What am i doing wrong? Could anyone please help me please?

Thanks alot.
Ok,

I have no idea of your database layout, so I will create a imaginary
table (People) to show my point.

The table is as follows

ID int
FirstName varchar(100)
Surname varchar(100)

I create a stored proceedure called GetUserCount which consists of the
following:

BEGIN
Declare @Count int
Select @Count = count(*) from dbo.People
Return @Count

END

Thats my stored proceedure done, now for my code:

C#

int ReturnValue;

SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlParameter Param;

Param = cmd.Parameters.Add("@Count", SqlDbType.Int);
Param.Direction = ParameterDirection.ReturnValue;

conn.ConnectionString = _ConnString;
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "dbo.GetUserCount";
try {
conn.Open();
cmd.ExecuteNonQuery();
ReturnValue =
Convert.ToInt32(cmd.Parameters["@Count"].Value);
}
catch (Exception ex) {
throw new InvalidDataException(ex.Message);
}
finally {
conn.Close();
cmd.Dispose();
}

VB.NET

Dim ReturnValue As Integer

Dim conn As New SqlConnection()
Dim cmd As New SqlCommand()
Dim Param As SqlParameter

Param = cmd.Parameters.Add("@Count", SqlDbType.Int)
Param.Direction = ParameterDirection.ReturnValue

conn.ConnectionString = _ConnString
cmd.Connection = conn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "dbo.GetUserCount"
Try
conn.Open()
cmd.ExecuteNonQuery()
ReturnValue = Convert.ToInt32(cmd.Parameters("@Count").Value)
Catch ex As Exception
Throw New InvalidDataException(ex.Message)
Finally
conn.Close()
cmd.Dispose()
End Try

In both cases, the variable ReturnValue will contain the return value
from the stored proceedure.

I hope this helps,

Regards
Mick Walker
 
J

Jeff Dillon

You are only putting scope_identity() into a variable, but not returning the
variable.

Either use an OUT param for the variable, or simply

SELECT @UserID

Sample code to demonstrate:


declare @test int

select @test = 5

select @test
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top