"unsupported parameter" when mapping a stored procedure? (EF) - asp.net

I have a stored procedure that I return the new ID number. The code looks like this:
ALTER PROCEDURE [dbo]. [Save Contact]
#FirstName varchar (50),
#LastName varchar (50),
#Email varchar (50)
AS
BEGIN
- SET NOCOUNT ON added to Prevent extra result sets from
- Interfering with SELECT statements.
SET NOCOUNT ON;
INSERT INTO Contacts (FirstName, LastName, Email)
VALUES (#FirstName, #LastName, #Email)
SELECT SCOPE_IDENTITY() as newid
END
When I try to map my stored procedures I get the following error:
Error 1 Error 2047: A mapping function binding specifier a function demoModel.Store.SaveContact with an unsupported parameter: id. Output parameters May only be mapped through the RowsAffectedParameter property. Use result bindings to return values ​​from a function invocation.
Is there anyone here who can see what I'm doing wrong?
Need more code, let me know.

With the SQL as it is above remove id as a parameter for the insert statement. Its not needed. To get SCOPE_IDENTITY() you only need to map newid it in the Result Column Bindings.
Also ensure that the column name in Result Column Bindings is the same case as the stored procedure is returning.
Source: http://blogs.msdn.com/b/adonet/archive/2008/03/26/stored-procedure-mapping.aspx

Related

Return value to textbox from stored procedure

Having trouble returning the value ID value I need for output back to the textbox in the form. Webforms and ADO.net
I tried adding a param identity as an int and OUT clause, while setting identity = scope_identity and returning the value then using the pattern my team is currently using for ExecuteNonQuery with anonymous parameter classes passing in values and tried passing the #identity value to the textbox.text for the id.
DataManager.Db.ExecuteNonQuery("DefaultConnection", "usp_CreateNewSalesTerritory",
new SqlParameter("#orgId", orgId),
new SqlParameter("#identity", salesTerritoryIdTextBox.Text),
new SqlParameter("#salesTerritoryName", salesTerritories.Name),
new SqlParameter("#createdBy", salesTerritories.CreatedBy),
new SqlParameter("#createdDate", salesTerritories.CreatedDate),
new SqlParameter("#updatedBy", salesTerritories.UpdatedBy),
new SqlParameter("#updatedDate", salesTerritories.UpdatedDate),
new SqlParameter("#isActive", salesTerritories.IsActive));
CREATE PROCEDURE dbo.usp_CreateNewSalesTerritory
#orgId VARCHAR(255),
#salesTerritoryName VARCHAR(255),
#createdBy VARCHAR(255),
#createdDate DATETIME,
#updatedBy VARCHAR(255),
#updatedDate DATETIME,
#isActive BIT,
#identity INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO SalesTerritory (OrganizationId, Name, IsActive,
CreatedBy, CreatedDate, UpdatedBy, UpdatedDate)
VALUES (#orgId, #salesTerritoryName, #isActive,
#createdBy, #createdDate, #updatedBy, #updatedDate);
--SELECT SCOPE_IDENTITY();
--RETURN SCOPE_IDENTITY();
--SELECT ##IDENTITY;
SET #identity = SCOPE_IDENTITY()
END;
RETURN #identity
I expected to get the new inserted ID value for that record, instead, I get the default value of 0 on the screen
Normally, you would call such a stored procedure in "pure" ADO.NET using the .ExecuteNonQuery() method on the SqlCommand object - since it's an INSERT statement.
But now, your stored procedure is actually returning some data - so you really need to treat this like a "normal" SELECT stored procedure.
Assuming you're always returning just the SCOPE_IDENTITY() value - preferably like this:
SELECT SCOPE_IDENTITY();
which is just one single value - you can use the .ExecuteScalar() method on the SqlCommand object - something like this:
object returned = sqlCmd.ExecuteScalar();
if (returned != null)
{
int newIdValue = Convert.ToInt32(returned);
}
// else -> nothing was returned, so most likely no row has been inserted -> handle it appropriately
So maybe you already have a "wrapper" method for .ExecuteScalar() on your DataManager.Db object - or maybe you need to add it. Give it a try - I'm pretty sure this will solve the issue.
I would avoid using the RETURN ... statement - SQL Server stored procedure by default will always return the number of rows that were affected by your stored procedure - don't change that "standard" behavior, if you can.

Entity Framework shows error when called stored procedure

In my project EF calls a stored procedure which is shown below. It returns either 1 or scope identity.
On EF function imports, the stored procedure is listed with a return type of decimal.
When the stored procedure returns scope identity, everything is ok.
But when if condition of sp satisfies, ef throws error as
The data reader returned by the store data provider does not have enough columns for the query requested.
Pls help..
This is my stored procedure:
#VendorId int,
#ueeareaCode varchar(3),
#TuPrfxNo varchar(3),
#jeeSfxNo varchar(4),
#Tjode varchar(3),
#uxNo varchar(3),
#TyufxNo varchar(4),
#Iyuy bit
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
SET NOCOUNT ON;
IF EXISTS (Select dfen_id
from dbo.efe_phfedwn_eflwn
where
[yu] = #Tyuode and
[uy] = #TuyxNo and
[yuno] = #Tuo)
return 1
ELSE
Begin
INSERT INTO dbo.yu
....................
Select Scope_Identity()
End
END
The error tells us that EF is expecting a result set and when we use RETURN we don't get a result set. Your error means that the stored procedure is returning an integer but EF is expecting a decimal, so we just CAST the selected values to a decimal.
So modify the SQL so that we SELECT instead of RETURN, like so (not forgetting to use CAST):
IF EXISTS (Select cntct_ctr_phn_ln_id
from dbo.cntct_ctr_phn_ln
where
[toll_free_phn_area_cd] = #TollfreeareaCode and
[toll_free_phn_prfx_no] = #TollfreePrfxNo and
[toll_free_phn_sfx_no] = #TollfreeSfxNo)
SELECT CAST(1 AS decimal)
Then also CAST the result of SCOPE_IDENTITY() to a decimal:
SELECT CAST(SCOPE_IDENTITY() AS decimal)

Error in stored procedure with string return type

ALTER PROCEDURE [dbo].[Signin_Check]
#uid varchar(50), #pwd varchar(50), #uname varchar(50) output
AS
IF EXISTS (SELECT * FROM threeLayer_user WHERE uid = #uid and pass = #pwd)
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT #uname = name
FROM threeLayer_user
WHERE uid = #uid and pass = #pwd
RETURN #uname
END
This stored procedure is throwing an error :
Conversion failed when converting the varchar value 'Saurav' to data type int.
Your code contains the line
return #uname
the return statement can only accept integer values. However, your #uname parameter is an output parameter, so you do not need to also return it. Just make sure that you specify OUTPUT when calling the sproc:
EXEC dbo.Signin_Check #uid, #password, #uname OUTPUT;
The SP doesn't seem to have bad code related to the error, could you check well the parameters being passed to the stored procedure (i.e. their order and their type)? The problem can be the values that you are passing and not the actual procedure.
Could you append to your post the code you are using when you invoke the stored procedure?
Are you sure that it's varchar column?
#uid varchar(50) / int
uid = #uid

scope identity in stored procedure

I have got the following stored procedure.
ALTER PROCEDURE dbo.insert_movie
(
#first_name varchar(50),
#movie_name varchar(50),
#date date,
#last_id_inserted int,
#last_name varchar(50)
)
AS
INSERT INTO movies (movie_name,movie_date) VALUES (#movie_name,#date);
SELECT SCOPE_IDENTITY() AS scope;
INSERT INTO customer (first_name,last_name,movie_id)
VALUES (#first_name,#last_name,#last_id_inserted)
I want to insert the scope variable into #last_id_inserted.
Basically, I want to insert the last inserted id (movie_id) to the customer table..
not sure about the syntax though
You can assign the result of the function to the variable.
SET #last_id_inserted = SCOPE_IDENTITY();
There is no need to have #last_id_inserted as a parameter. Declare it as a variable in the SP.
DECLARE #last_id_inserted int;
SET #last_id_inserted = SCOPE_IDENTITY();
Or you can use it directly in the insert statement.
INSERT INTO customer (first_name,last_name,movie_id)
VALUES (#first_name,#last_name,SCOPE_IDENTITY())
SELECT #last_id_inserted = SCOPE_IDENTITY()
or
SET #last_id_inserted = SCOPE_IDENTITY()
You need to declare the variable in the stored procedure and assign the value of the SCOPE_IDENTITY function:
DECLARE #last_id_inserted int;
SET #last_id_inserted = SCOPE_IDENTITY();
I just want to add to other answers that you can add OUTPUT clause (SQLServer 2005 and higher) to your INSERT instead of using SCOPE_IDENTITY

Stored Procedure IDENTITY_INSERT

I'm recently change my data table, I remove column and add a new column that define as identity = True and identity seed = 1, identity increment = 1.
When i tried to insert data to this table by STORE PROCEDURE i get this exception:
An explicit value for the identity column in table 'AirConditioner' can only be specified when a column list is used and IDENTITY_INSERT is ON.
I saw that i need to add this lines:
SET IDENTITY_INSERT [dbo].[AirConditioner] ON and finally OFF
I added and its still throw an exception...
My store procedure is attached as a picture
SQL Server will take care of assigning a value to your identity column. You should remove #ID from the insert statement and remove the IDENTITY_INSERT statements.
can only be specified when a column
list is used and IDENTITY_INSERT is
ON.
You're forgetting the first condition: the column list. The column list is a (usually) optional element between the table name and values. You should specify it like:
INSERT INTO AirConditioner (ID, CategoricNumber, ...)
VALUES(#ID, #CategoricNumber, ...)
Do NOT use set identity insert on. If you have an identity, you should be letting SQL server decide what value to put in there.
ALTER PROCEDURE [dbo].[AddAirConditioner] #CategoricNumber int, #Name nvarchar(50),
#PicName nvarchar(100), #ShortDetails nvarchar(200), #Details nvarchar(2000),
#Price int, #ImagePath nvarchar(500), #AirConditionerType nvarchar(50), #COP float,
#BTU float, #ProdType nvarchar(20), #ProdIndex int
AS
INSERT INTO AirConditioner VALUES( #CategoricNumber, #Name, #PicName,
#ShortDetails, #Details, #Price, #ImagePath, #AirConditionerType, #COP,
#BTU, #ProdType, #ProdIndex)
If you need to get the ID back for using in child tables, then use scope_identity or the output clause. Look these up in Books online to see how to use.
Try placing
SET IDENTITY_INSERT [dbo].[AirConditioner] ON
GO
Before the alter procedure

Resources