Passing comma delimited parameter to stored procedure in SQL - c#

If I pass call my stored procedure from T-SQL:
exec [dbo].[StoredProcedureName] '''Vijay'', ''Rana'', 1, 0'
in SQL Server Mgmt Studio, it works fine but when I call it from my application it gives me error
Unclosed quotation mark after the character string ''Vijay','Rana',1,0'.
I searched on the google and find this EXEC sp_executesql #FinalQuery but its not working for me
EDIT
I am calling it like
public virtual IDataReader ImportFirefighter(String query)
{
Database database = DatabaseFactory.CreateDatabase();
DbCommand command = database.GetStoredProcCommand("[StoreProcedureName]");
database.AddInParameter(command, "#query", DbType.String, query);
IDataReader reader = null;
try
{
reader = database.ExecuteReader(command);
}
catch (DbException ex)
{
throw new DataException(ex);
}
return reader;
}
EDIT My complete Store Procedure
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
--[dbo].[ImportIntoFirefighter] '''Vijay'',''Rana'',''AC'',''AC'',''VOL'',1,0,0,1,1,''NA'','''',''VOL'','''','''',0,'''','''',0,1,1,'''',0&''Vijay21'',''Rana2'',''AC'',''AC'',''VOL'',1,0,0,1,1,''NA'','''',''VOL'','''','''',0,'''','''',0,1,1,'''',0&''Vijay32'',''Rana3'',''AC'',''AC'',''VOL'',1,0,0,1,1,''NA'','''',''VOL'','''','''',0,'''','''',0,1,1,'''',0&''Vijay42'',''Rana4'',''AC'',''AC'',''VOL'',1,0,0,1,1,''NA'','''',''VOL'','''','''',0,'''','''',0,1,1,'''',0'
ALTER PROCEDURE [dbo].[ImportIntoFirefighter]
#query VARCHAR(MAX)
AS
BEGIN
DECLARE #TotalRecord int
DECLARE #loopcount int
DECLARE #TempQueryList TABLE
(
[ID] INT IDENTITY(1,1),
[VALUE] VARCHAR(1000)
)
DECLARE #Result TABLE
(
[iff_id] INT IDENTITY(1,1),
[last_name] VARCHAR(50),
[first_name] VARCHAR(50),
[email] VARCHAR(50),
[mobile_number] VARCHAR(50),
[error] VARCHAR(max)
)
insert into #TempQueryList (VALUE) (
SELECT SUBSTRING('&' + #query + '&', Number + 1,
CHARINDEX('&', '&' + #query + '&', Number + 1) - Number -1)AS VALUE
FROM master..spt_values
WHERE Type = 'P'
AND Number <= LEN('&' + #query + '&') - 1
AND SUBSTRING('&' + #query + '&', Number, 1) = '&' )
Set #TotalRecord = (select count(*) FROM #TempQueryList)
--select * from #TempQueryList
--Loop For Each Repeated Schedule
set #loopcount = 1
WHILE #loopcount <= #TotalRecord
BEGIN
Declare #SingleQuery varchar(1000)
select #SingleQuery = Value from #TempQueryList where id = #loopcount
BEGIN TRY
--print '[AddFirefighter] ' + #SingleQuery
--SELECT 1/0;
--execute (#SingleQuery)
declare #FinalQuery varchar(max)
-- Select #SingleQuery = LEFT(RIGHT(#SingleQuery, len(#SingleQuery)-1),len(#SingleQuery)-2)
set #FinalQuery = '[AddFirefighter] ' + #SingleQuery
print #FinalQuery
EXEC (#FinalQuery)
END TRY
BEGIN CATCH
insert into #Result (last_name,first_name,email,mobile_number,error) values ( '','','','',ERROR_MESSAGE() )
-- Execute the error retrieval routine.
END CATCH
--print #loopcount
SET #loopcount = #loopcount + 1
END
select * from #Result
--execute (#query)
END

Well ' is the delimiter so it seems to me your string becomes 'Vijay','Rana',1,0 I think you are mixing strings and numerics in the same "string" what you need to do is pass 'Vijay','Rana','1','0' (a string of strings) and then sort things out inside your procedure. To do this your passed string should be something like ' '' Vijay'',''Rana'',''1'',''0'' '. Depending on how you handle things inside your stored proc you may even need '' '''' Vijay'''',''''Rana'''',''''1'''',''''0'''' '' .Best create a simple proc which just returns the string as a test bed

If you are using c# and asp.net, you should set up your parameters in code rather then building a dynamic sql statement. If you already have the stored procedure setup then I'm not seeing a reason to call a dynamic sql statement and building out the parameters in a string.
Here is a example of a parameterized call to sql with a stored procedure.
http://msdn.microsoft.com/en-us/library/yy6y35y8(v=vs.110).aspx
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Create the command and set its properties.
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "SalesByCategory";
command.CommandType = CommandType.StoredProcedure;
// Add the input parameter and set its properties.
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "#CategoryName";
parameter.SqlDbType = SqlDbType.NVarChar;
parameter.Direction = ParameterDirection.Input;
parameter.Value = categoryName;
// Add the parameter to the Parameters collection.
command.Parameters.Add(parameter);
// Open the connection and execute the reader.
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
Console.WriteLine("{0}: {1:C}", reader[0], reader[1]);
}
}
else
{
Console.WriteLine("No rows found.");
}
reader.Close();
}

If your stored procedure takes four parameters as it seems to based on your question, you can add the parameters to a SqlCommand and then execute the command.
//Build your command
SqlConnection conn = new SqlConnection(yourConnectionString);
SqlCommand cmd = new SqlCommand("stored_procedure_name", conn);
cmd.CommandType = CommandType.StoredProcedure;
//Define the parameters to pass to the stored procedure
cmd.Parameters.Add("#firstParameter", SqlDbType.NVarChar, 255);
cmd.Parameters.Add("#secondParameter", SqlDbType.NVarChar, 255);
cmd.Parameters.Add("#thridParameter", SqlDbType.Int);
cmd.Parameters.Add("#fourthParameter", SqlDbType.Int);
//Assign Values to the parameters
cmd.Parameters["#firstParameter"].Value = "Vijay";
cmd.Parameters["#secondParameter"].Value = "Rana";
cmd.Parameters["#thirdParameter"].Value = 1;
cmd.Parameters["#fourthParameter"].Value = 0;
//Execute the command
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

Related

calling stored procedure from asp.net page returns Parameter <Column_Name> not found in the collection.

I am Trying to call a stored procedures that returns a single value from a table where the table name is parameterised. on calling the SP it returns the following error
**
> system.argumentexception: Parameter 'tableName' not found in the collection.
**
my stored procedure is :
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `selectDynamicTable`(IN tableName VARCHAR(6))
BEGIN
SET #table1 := tableName;
SET #sql_text := concat('SELECT MAX(Sl_No) FROM ',#table1,';');
PREPARE stmt FROM #sql_text;
EXECUTE stmt;
END
and my c# code that calls it is :
MySqlCommand cmdSelect = new MySqlCommand("selectDynamicTable", con);
cmdSelect.CommandType = CommandType.StoredProcedure;
cmdSelect.Parameters.AddWithValue("#table1", comp_code.ToString().ToLower());
//cmdSelect.ExecuteNonQuery();
MySqlDataReader dr1 = cmdSelect.ExecuteReader();
while (dr1.Read())
{
int query1 = dr1.GetInt32(0);
query1 += 1;
}
I am not an expert in Mysql but I think it should look something like this. Others may correct if I made an error somewhere.
Additionaly please read this tutorial about variables: http://www.mysqltutorial.org/variables-in-stored-procedures.aspx .
Lastly if you are using dr1 as a variable name for anything more than testing, I would recommend changing the name to something reconizable.
DELIMITER |
CREATE DEFINER=`root`#`localhost` PROCEDURE `selectDynamicTable`(IN tableName VARCHAR(6))
(
IN tableName VARCHAR(6)
)
BEGIN
SET #sql_text := concat('SELECT MAX(Sl_No) FROM ', tableName, ';');
PREPARE stmt FROM #sql_text;
EXECUTE stmt;
END |
delimiter ;
MySqlCommand cmdSelect = new MySqlCommand("selectDynamicTable", con);
cmdSelect.CommandType = CommandType.StoredProcedure;
cmdSelect.Parameters.AddWithValue("tableName", comp_code.ToString().ToLower());
cmd.Parameters["tableName"].Direction = ParameterDirection.Input;
//cmdSelect.ExecuteNonQuery();
MySqlDataReader dr1 = cmdSelect.ExecuteReader();
while (dr1.Read())
{
int query1 = dr1.GetInt32(0);
query1 += 1;
}

Conversion failed when converting the varchar value 'AL-DMK2_1' to data type int

For a stored procedure, I get the following exception/error.
"Conversion failed when converting the varchar value AL-DMK2_1 to data type int."
Stored procedure:
ALTER PROCEDURE dbo.sp_generateNewRequestId
(
#Customer varchar(50),
#Network_Type varchar(25),
#Market_Name varchar(50),
#NewRequestId varchar(50) OUTPUT
)
AS
Begin
Declare #MarketCode as varchar(10);
Declare #CustomerPrefix as varchar(10);
SET #MarketCode = (select Market_Code from Customer_Market_Details where Customer = #Customer and Network_Type = #Network_Type and Market_Name = #Market_Name);
SET #CustomerPrefix = (select Customer_Prefix from Customer_Market_Details where Customer = #Customer and Network_Type = #Network_Type and Market_Name = #Market_Name);
Declare #RequestIDPrefix as varchar(20);
SET #RequestIdPrefix = #CustomerPrefix + #MarketCode + '_';
-- Count how many requests you already have with the same request ID Prefix.
Declare #RequestCount as int;
SET #RequestCount = (Select count (*) from request_details where request_id like #RequestIdPrefix + '%');
SET #NewRequestId = #RequestIdPrefix + CAST ((#RequestCount + 1) AS varchar);
While (Exists (Select * from request_details where request_id = #NewRequestId ) )
Begin
SET #RequestCount = #RequestCount + 1;
SET #NewRequestId = #RequestIdPrefix + CAST ((#RequestCount + 1) AS varchar);
end
RETURN #NewRequestId;
END
Calling stored procedure:
using (SqlConnection connection = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["dbString"].ConnectionString))
{
using (SqlCommand command = new SqlCommand("dbo.sp_generateNewRequestId", connection))
{
command.Parameters.AddWithValue("#Customer", customer);
command.Parameters.AddWithValue("#Network_Type", technology);
command.Parameters.AddWithValue("#Market_Name", market);
command.Parameters.Add("#NewRequestId", SqlDbType.VarChar).Direction = ParameterDirection.Output;
command.Parameters["#NewRequestId"].Size = 50;
command.CommandType = CommandType.StoredProcedure;
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
Observations:
when the output parameter is set to #RequestCount, the procedure successfully returns #RequestCount.
But if any other varchar value is attempted to output, the exception with the aforementioned error is thrown. The execution fails at the line command.ExecuteNonQuery().
None of the solutions provided online worked for this error.
Appreciate your help!
Thanks!
If you return anything from a stored procedure, it must be able to be cast to an int as the return type of SqlCommand.ExecuteNonQuery() is an int (http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx) and the RETURN statement in SQL Server stored procs should only be use to return integer values (http://msdn.microsoft.com/en-us/library/ms174998.aspx), hence why ExecuteNonQuery has a return type of int, otherwise it would be object.
You're almost there, simply get rid of the return statement in the stored proc so it looks like this:
ALTER PROCEDURE dbo.sp_generateNewRequestId
(
#Customer varchar(50),
#Network_Type varchar(25),
#Market_Name varchar(50),
#NewRequestId varchar(50) OUTPUT
)
AS
Begin
Declare #MarketCode as varchar(10);
Declare #CustomerPrefix as varchar(10);
SET #MarketCode = (select Market_Code from Customer_Market_Details where Customer = #Customer and Network_Type = #Network_Type and Market_Name = #Market_Name);
SET #CustomerPrefix = (select Customer_Prefix from Customer_Market_Details where Customer = #Customer and Network_Type = #Network_Type and Market_Name = #Market_Name);
Declare #RequestIDPrefix as varchar(20);
SET #RequestIdPrefix = #CustomerPrefix + #MarketCode + '_';
-- Count how many requests you already have with the same request ID Prefix.
Declare #RequestCount as int;
SET #RequestCount = (Select count (*) from request_details where request_id like #RequestIdPrefix + '%');
SET #NewRequestId = #RequestIdPrefix + CAST ((#RequestCount + 1) AS varchar);
While (Exists (Select * from request_details where request_id = #NewRequestId ) )
Begin
SET #RequestCount = #RequestCount + 1;
SET #NewRequestId = #RequestIdPrefix + CAST ((#RequestCount + 1) AS varchar);
end
END
And modify your C# code to this:
string newRequestID = "";
using (SqlConnection connection = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["dbString"].ConnectionString))
{
using (SqlCommand command = new SqlCommand("dbo.sp_generateNewRequestId", connection))
{
command.Parameters.AddWithValue("#Customer", customer);
command.Parameters.AddWithValue("#Network_Type", technology);
command.Parameters.AddWithValue("#Market_Name", market);
command.Parameters.Add("#NewRequestId", SqlDbType.VarChar).Direction = ParameterDirection.Output;
command.Parameters["#NewRequestId"].Size = 50;
command.CommandType = CommandType.StoredProcedure;
connection.Open();
command.ExecuteNonQuery();
newRequestID = (string)command.Parameters["#NewRequestId"].Value;
connection.Close();
}
}

I want to insert a record in DB and then need to return a row

I want to insert a record in DB and then need to return a row. I created the query but how to use that in .cs (C# code)
This is my query
#ledgerId numeric(18,0) ,
#voucherNo varchar(MAX) ,
#invoiceNo varchar(MAX) ,
#date datetime ,
#month datetime ,
#totalAmount decimal(18,5) ,
#narration varchar(MAX) ,
-- #extraDate datetime ,
#extra1 varchar(MAX) ,
#extra2 varchar(MAX) ,
#suffixPrefixId numeric(18,0) ,
#voucherTypeId numeric(18,0)
AS
DECLARE #UpdatedVoucherNo DECIMAL(18,0)
SET #UpdatedVoucherNo = (SELECT ISNULL( MAX(CAST (voucherNo AS NUMERIC(18,0))),0) + 1
FROM tbl_SalaryVoucherMaster
WHERE voucherTypeId=#voucherTypeId)
IF (#UpdatedVoucherNo = #voucherNo )
INSERT INTO tbl_SalaryVoucherMaster
(
/*salaryVoucherMasterId,*/
ledgerId,
voucherNo,
invoiceNo,
date,
month,
totalAmount,
narration,
extraDate,
extra1,
extra2,
suffixPrefixId,
voucherTypeId )
VALUES
(
/*#salaryVoucherMasterId, */
#ledgerId,
#voucherNo,
#invoiceNo,
#date,
#month,
#totalAmount,
#narration,
getDate(),
#extra1,
#extra2,
#suffixPrefixId,
#voucherTypeId)
SELECT SCOPE_IDENTITY() AS [Identity],0 AS [UpdatedVoucherNo]
One way to do what you want is to modify #voucherNo to be an OUTPUT parameter, and add a new OUTPUT parameter to your query to return the value of SCOPE_IDENTITY().
#voucherNo varchar(max)
#ScopeIdentity numeric(38,0)
And modify that last SELECT statement to set the value of #ScopeIdentity parameter.
SELECT #ScopeIdentity = SCOPE_IDENTITY()
Then use SqlCommand.ExecuteNonQuery to execute the query, and on completion, read the values from the output parameters.
using (SqlCommand command = new SqlCommand(connection, commandText))
{
int rowsAffected = command.ExecuteNonQuery();
if (rowsAffected > 0)
{
var voucherNo = (string)command.Parameters["#voucherNo].Value;
var identity = (decimal)command.Parameters["#ScopeIdentity"].Value;
}
}
Command.ExecuteNonQuery(); is what you need, it returns the number(int) of rows affected in inserting and updating data,
here is an example to use it,
string connetionString = null;
SqlConnection cnn ;
SqlCommand cmd ;
string sql = null;
connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
sql = "Your SQL Statemnt Here";
cnn = new SqlConnection(connetionString);
cnn.Open();
cmd = new SqlCommand(sql, cnn);
int rowsAffected = cmd.ExecuteNonQuery();
if (rowsAffected > 0)
{
//do anything
}
cmd.Dispose();
cnn.Close();
MessageBox.Show (" ExecuteNonQuery in SqlCommand executed !!");
more information here

Conversion Failing

I currently have an update statement running in c# and the front end to edit some values in my sql server database. But I am getting a Conversion Failed error. I can not seem to find the source.
ERROR message
Conversion failed when converting the nvarchar value 'UPDATE T_ROLLUP_SYSTEM_EXCEPT
SET DEPT_ID = ' to data type int.
14
Sql
ALTER PROCEDURE [dbo].[USP_UPDATE_SYS_MAPPING]
-- Add the parameters for the stored procedure here
#SYSTEM VARCHAR(50),
#UNIT VARCHAR(50),
#MEDCTRLEVEL VARCHAR(50),
#MEDCTR VARCHAR(50),
#FACID VARCHAR(50),
#ENTN VARCHAR(50),
#DEPT_ID INT,
#ROLLUP_TYPE_ID INT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE #SQL VARCHAR(MAX);
DECLARE #MEDCTRID INT;
SELECT #MEDCTRID = MED_CTR_ID FROM T_ROLLUP_MED_CTR WHERE MED_CTR = #MEDCTR
PRINT (#MEDCTRID);
-- Insert statements for procedure here
SET #SQL = N'UPDATE T_ROLLUP_SYSTEM_EXCEPT
SET DEPT_ID =
'''+#DEPT_ID''' , ROLLUP_TYPE_ID = '''+#ROLLUP_TYPE_ID+'''
, UPDATE_DT = GETDATE()
WHERE SYSTEM = '''+#SYSTEM+'''
AND ENTN = '''+#ENTN+'''
AND MED_CTR_ID = '+CONVERT(VARCHAR,#MEDCTRID)+'
AND MED_CTR_LEVEL = '''+#MEDCTRLEVEL+'''
AND FAC_ID = '''+#FACID+'''
AND UNIT = '''+#UNIT+''''
PRINT (#SQL);
EXEC (#SQL);
HTML
string[] mdctrvalue = medctr.Text.Split('[', ']');
string[] mpvalue = mpSearch.Text.Split('(', ')');
string sys = acctsys.ToString();
string unit = txtunit.ToString();
string mdctrlvl = mdctrvalue[1].ToString();
string mdctr = mdctrvalue[0].ToString();
string facid = fac.ToString();
string entn = txtentn.ToString();
string dept_id = dept.SelectedValue.ToString();
string rollup_type_id = rolluptype.SelectedValue.ToString();
SqlConnection myconn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Rollup2ConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = myconn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "USP_UPDATE_SYS_MAPPING";
cmd.Parameters.Add("#SYSTEM", SqlDbType.VarChar).Value = acctsys.ToString();
cmd.Parameters.Add("#UNIT", SqlDbType.VarChar).Value = unit.ToString();
cmd.Parameters.Add("#MEDCTRLEVEL", SqlDbType.VarChar).Value = mdctrlvl.ToString();
cmd.Parameters.Add("#MEDCTR", SqlDbType.VarChar).Value = mdctr.ToString();
cmd.Parameters.Add("#FACID", SqlDbType.VarChar).Value = facid.ToString();
cmd.Parameters.Add("#ENTN", SqlDbType.VarChar).Value = entn.ToString();
cmd.Parameters.Add("#DEPT_ID", SqlDbType.Int).Value = dept_id.ToString();
cmd.Parameters.Add("#ROLLUP_TYPE_ID", SqlDbType.Int).Value = rollup_type_id.ToString();
myconn.Open();
int retVal = cmd.ExecuteNonQuery();
Maybe change:
+#ROLLUP_TYPE_ID+
To:
+ CONVERT(VARCHAR(12), #ROLLUP_TYPE_ID) +
You should always specify a length for your varchar columns/variables...
That said, you can re-write your stored procedure to not use dynamic SQL at all - why is it being used?
ALTER PROCEDURE [dbo].[USP_UPDATE_SYS_MAPPING]
#SYSTEM VARCHAR(50),
#UNIT VARCHAR(50),
#MEDCTRLEVEL VARCHAR(50),
#MEDCTR VARCHAR(50),
#FACID VARCHAR(50),
#ENTN VARCHAR(50),
#DEPT_ID INT,
#ROLLUP_TYPE_ID INT
AS
BEGIN
SET NOCOUNT ON;
DECLARE #MEDCTRID INT;
SELECT #MEDCTRID = MED_CTR_ID FROM dbo.T_ROLLUP_MED_CTR
WHERE MED_CTR = #MEDCTR;
PRINT (#MEDCTRID);
UPDATE dbo.T_ROLLUP_SYSTEM_EXCEPT
SET DEPT_ID = CASE
WHEN #DEPT_ID > 1 THEN #DEPT_ID
WHEN #DEPT_ID = 1 THEN NULL
ELSE REG_DEPT_ID1 END
, ROLLUP_TYPE_ID = #ROLLUP_TYPE_ID
, UPDATE_DT = GETDATE()
WHERE SYSTEM = #SYSTEM
AND ENTN = #ENTN
AND MED_CTR_ID = #MEDCTRID
AND MED_CTR_LEVEL = #MEDCTRLEVEL
AND FAC_ID = #FACID
AND UNIT = #UNIT;
END
GO
If it needs to be dynamic SQL for some reason you haven't yet shared, you are still better off parameterizing as much of this as possible using sp_executesql instead of EXEC().

Getting return value from stored procedure in ADO.NET

I have a stored procedure, which returns the unique identifier after insertion ##identity. I tried it in the server explorer and it works as expected #RETURN_VALUE = [identifier].
In my code I added a parameter called #RETURN_VALUE, with ReturnValue direction first, than any other parameters, but when I run my query with ExecuteNonQuery() that parameter remains empty. I don't know what I've done wrong.
Stored Procedure
ALTER PROCEDURE dbo.SetAuction
(
#auctionID int,
#itemID int,
#auctionType tinyint,
#reservationPrice int,
#maxPrice int,
#auctionEnd datetime,
#auctionStart datetime,
#auctionTTL tinyint,
#itemName nchar(50),
#itemDescription nvarchar(MAX),
#categoryID tinyint,
#categoryName nchar(50)
) AS
IF #auctionID <> 0
BEGIN
BEGIN TRAN T1
UPDATE Auction
SET AuctionType = #auctionType,
ReservationPrice = #reservationPrice,
MaxPrice = #maxPrice,
AuctionEnd = #auctionEnd,
AuctionStart = #auctionStart,
AuctionTTL = #auctionTTL
WHERE AuctionID = #auctionID;
UPDATE Item
SET
ItemName = #itemName,
ItemDescription = #itemDescription
WHERE
ItemID = (SELECT ItemID FROM Auction WHERE AuctionID = #auctionID);
COMMIT TRAN T1
RETURN #auctionID
END
ELSE
BEGIN
BEGIN TRAN T1
INSERT INTO Item(ItemName, ItemDescription, CategoryID)
VALUES(#itemName, #itemDescription, #categoryID);
INSERT INTO Auction(ItemID, AuctionType, ReservationPrice, MaxPrice, AuctionEnd, AuctionStart, AuctionTTL)
VALUES(##IDENTITY,#auctionType,#reservationPrice,#maxPrice,#auctionEnd,#auctionStart,#auctionTTL);
COMMIT TRAN T1
RETURN ##IDENTITY
END
C# Code
cmd.CommandText = cmdText;
SqlParameter retval = new SqlParameter("#RETURN_VALUE", System.Data.SqlDbType.Int);
retval.Direction = System.Data.ParameterDirection.ReturnValue;
cmd.Parameters.Add(retval);
cmd.Parameters.AddRange(parameters);
cmd.Connection = connection;
connection.Open();
cmd.ExecuteNonQuery();
return (int)cmd.Parameters["#RETURN_VALUE"].Value;
Just tried on my box and this works for me:
In SQL Server:
DROP PROCEDURE TestProc;
GO
CREATE PROCEDURE TestProc
AS
RETURN 123;
GO
In C#
string cnStr = "Server=.;Database=Sandbox;Integrated Security=sspi;";
using (SqlConnection cn = new SqlConnection(cnStr)) {
cn.Open();
using (SqlCommand cmd = new SqlCommand("TestProc", cn)) {
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter returnValue = new SqlParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(returnValue);
cmd.ExecuteNonQuery();
Assert.AreEqual(123, (int)returnValue.Value);
}
}
I solved the problem:
you have to set SqlCommand.CommandType to CommandType.StoredProcedure in order to get return values and/or output parameters. I haven't found any documentation about that, but now everything works.
Do you get the value of you EXEC in TSQL? I wonder if refactoring the TSQL would help (and using SCOPE_IDENTITY():
so change:
COMMIT TRAN T1
RETURN ##IDENTITY
to:
SET #auctionID = SCOPE_IDENTITY()
COMMIT TRAN T1
RETURN #auctionID
(I would also change the other ##IDENTITY to SCOPE_IDENTITY())
As a minor optimisation, you could also use:
return (int)retval.Value;
but this side of things should have worked "as is" from what I can see (hence why I'm focusing on the TSQL).
Some one can also use this simple and short method to calculate return value from SP
In SQL:
Create Table TestTable
(
Int Id
)
CREATE PROCEDURE Proc_TestProc
#Id
AS
Begin
Set NOCOUNT ON //Use this line if you don't want to return any message from SQL
Delete from TestTable where Id = #Id
return 1
Set NOCOUNT OFF //NOCOUNT OFF is Optional for NOCOUNT ON property
End
Sql Server always returns Int type value only.
and in C#
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["TestConnectionString"].ToString()))
using (SqlCommand cmd = new SqlCommand("Proc_TestProc", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#Id", 1);
var returnParameter = cmd.Parameters.Add("#ReturnVal", SqlDbType.Int);
returnParameter.Direction = ParameterDirection.ReturnValue;
conn.Open();
cmd.ExecuteNonQuery();
var result = returnParameter.Value;
}
You can also check your return value in SQL by using this command:
DECLARE #return_status int;
EXEC #return_status = dbo.[Proc_TestProc] 1;
SELECT 'Return Status' = #return_status;
print 'Returned value from Procedure: ' + Convert(varchar, #return_status); // Either previous or this line both will show you the value of returned value
you can use standart ways that you use before in normal queries but in Sql command you must write EXEC before your store procedure name and dont use commandtype like this :
SqlConnection con = new SqlConnection(["ConnectionString"])
SqlCommand com = new SqlCommand("EXEC _Proc #id",con);
com.Parameters.AddWithValue("#id",["IDVALUE"]);
con.Open();
SqlDataReader rdr = com.ExecuteReader();
ArrayList liste = new ArrayList();
While(rdr.Read())
{
liste.Add(rdr[0]); //if it returns multiple you can add them another arrays=> liste1.Add(rdr[1]) ..
}
con.Close();

Categories