I have Perl code to pass a parameter value to an Oracle stored procedure like below,
$func->bind_param(":testParam", undef);
Now I'm trying to call the same stored procedure from C#.
How can I pass undef in C#?
I already tried below the ideas below, but I am getting an empty result set,
objCmd.Parameters.Add("testParam", OracleDbType.Varchar2).Value = null;
objCmd.Parameters.Add("testParam", OracleDbType.Varchar2).Value = string.Empty;
objCmd.Parameters.Add("testParam", OracleDbType.Varchar2).Value = DBNull.Value;
Related
I have a simple c# application that uses the Entity Framework 5.0 and should call a stored procedure with output parameters. The problem is that it doesn't work, and instead, it returns -1.
The c# code that I have looks like that:
// Define the output paramaters
SqlParameter operatorID = new SqlParameter("#operatorID", SqlDbType.Int);
operatorID.Direction = ParameterDirection.Output;
SqlParameter operatorCode = new SqlParameter("#operatorCode", SqlDbType.Int);
operatorCode.Direction = ParameterDirection.Output;
var parameters = new List<object>();
parameters.Add(operatorID);
parameters.Add(operatorCode);
var noOutput = context.Database.ExecuteSqlCommand("EXEC my_sp_name #operatorID, #operatorCode", parameters.ToArray());
The stored procedure looks like that:
CREATE PROCEDURE [dbo].[my_sp_name]
(
#operatorID int OUTPUT
,#operatorCode int OUTPUT
)
AS
SET #operatorID = 123
I know it doesn't make sense, but it's just for a test.
The problem after executing the c# code is that the noOutput value is -1 and the operatorID value is empty, where I expect it to be 123.
Does anybody have an idea what I do wrong?
Your parameters in query must be with OUTPUT keyword
var noOutput = context.Database.ExecuteSqlCommand("EXEC my_sp_name #operatorID OUTPUT, #operatorCode OUTPUT", parameters.ToArray());
Check the "operatorCode.Value" property. the result will be there after execution.
If not try to use the CommandType.StoredProcedure and then execute the command with the store procedure name along with the parameters. you will get the output
I'm trying to get just a single value from the database.
In SQL-Server it works fine and I get a value which is not null. But whenever I try to do it from VS, "status" variable in the code always becomes null even if it gets the parameter and connects the database correctly.
Here is my code:
string id = textboxOrderDate.SelectedValue;
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["iremConnection"].ConnectionString);
con.Open();
SqlCommand command = new SqlCommand("SELECT_ORDERSTATUS_BY_ORDERID", con);
command.CommandType = CommandType.StoredProcedure;
SqlParameter paramID = new SqlParameter("#ORDERID", id);
command.Parameters.Add(paramID);
string status= (string)command.ExecuteScalar();
//string status=command.ExecuteScalar().ToString();
con.Close();
I also tried using a dataadapter to fill the dataset with a datatable to retrieve the value I want. But, unfortunately datatable row count is equal to 0. My stored procedure works fine I do not understand why it does not work the same way with the VS. Here is my stored procedure:
ALTER PROCEDURE [dbo].[SELECT_ORDERSTATUS_BY_ORDERID]
#ORDERID VARCHAR(50)
AS
SELECT dbo.OrderTable.Status
FROM OrderTable
WHERE #ORDERID=OrderID
Okay I found the problem. I was using DateTime type as OrderID and it was not matching with the one stored in the database. Database stores it with zeros at the end so it was not matching what my c# code sends.
Thank you everyone.
This is the signature of my stored procedure:
Create Procedure sp_Member_Account_Select_By_User_And_Membership_Year
(
#Username varchar(150),
#Membership_Year varchar(10)
)
AS
The code in asp.net c#
command.Parameters.Add("#Username", SqlDbType.VarChar,150).Value = vUsername;
command.Parameters.Add("#Membership_Year", SqlDbType.VarChar,10).Value = "2014";
SqlDataReader dr;
dr = command.ExecuteReader();
The error message:
Procedure or function
'sp_Member_Account_Select_By_User_And_Membership_Year' expects
parameter '#Username', which was not supplied.
I have tried different ways of adding parameters, including Addwithvalue, but with no success. What am I missing?
Since you obviously add the parameter to the command, there are two more things that can cause this exception:
Set the command type to CommandType.StoredProcedure:
command.ComandType = CommandType.StoredProcedure;
Set the user name parameter to DBNull.Value if vUsername is null:
command.Parameters.Add("#Username", SqlDbType.VarChar,150).Value = vUsername ?? DBNull.Value;
Could vUsername be null? When you need to pass a null to a stored procedure, you need to pass the constant DBNull.Value rather than null.
Something like
command.Parameters.Add("#Username", SqlDbType.VarChar,150).Value =
(vUsername == null) ? DbNull.Value : (object) vUsername;
How to fetch the return value from a stored procedure?
I noticed that the stored procedure returns an integer on its own. I need to fetch it in C#.
You can make use of Return parameter in C# to get that value. Like as below
SqlParameter retval = sqlcomm.Parameters.Add("#return_value", SqlDbType.VarChar);
retval.Direction = ParameterDirection.ReturnValue;
sqlcomm.ExecuteNonQuery();
string retunvalue = (string)sqlcomm.Parameters["#return_value"].Value;
Note your procedure must return a value to be able to fetch it:
create procedure [dbo].[usp_GetNewSeqVal]
#SeqName nvarchar(255)
as begin
declare #NewSeqVal int
select #NewSeqVal =1
---other statement
return #NewSeqVal
end
Check Following Code:
SqlParameter retval = sqlcomm.Parameters.Add("#b", SqlDbType.VarChar);
retval.Direction = ParameterDirection.ReturnValue;
sqlcomm.ExecuteNonQuery(); // MISSING
string retunvalue = (string)sqlcomm.Parameters["#b"].Value;
For further reference check link: Getting return value from stored procedure in C#
In your SP, you need to return KategoriId. By default SP returns the number of rows affected by the latest insert, update or delete statement.
And mke sure you use proper data type in C# and in database column KategoriId to make this work. I had problems in past when database column was Decimal and I tried to assign the return value to an int and it never worked.
You can use output parameter in store procedure or use ExecuteScalar instead of ExecuteNonQuery.
CREATE PROCEDURE [dbo].[Code]
#intEpmName NUMERIC,
#strFailedEMPID VARCHAR(1000) output
AS
DECLARE
#FailedCodes VARCHAR(1000)
BEGIN
----
my logic where i need return the value
SET #strFailedEMPID = #FailedCodes
-----
END
In the stored procedure above, I can send the value as "0" to #strFailedEMPID then to my procedure. However, when I return the value from my procedure, then to the same variable #strFailedEMPID I am sending the value as such:
lsqlParam = new SqlParameter("#strFailedEMPID ", SqlDbType.VarChar);
lsqlParam.Value = "0";
lsqlParam.Direction = ParameterDirection.ReturnValue;
lsqlCmd.Parameters.Add(lsqlParam);
Can anyone help with the correct syntax to get the return value from the procedure?
It's because you are defining the parameter in .NET as a ReturnValue which would actually equate to the scenario where you use RETURN within the stored procedure to return an integer (which you're not doing).
Instead, you need to define the #strFailedEMPID parameter as ParameterDirection.Output within your .NET code. If you want to pass a value in AND receive one out through the parameter, use ParameterDirection.InputOutput.
After executing the sproc, you then just:
string value = lsqlCmd.Parameters["#strFailedEMPID"].value;
So....
lsqlParam = new SqlParameter("#strFailedEMPID ", SqlDbType.VarChar);
lsqlParam.Value = "0";
lsqlParam.Direction = ParameterDirection.InputOutput;
lsqlCmd.Parameters.Add(lsqlParam);
lsqlCmd.ExecuteNonQuery();
string value = lsqlCmd.Parameters["#strFailedEMPID"].value;