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;
Related
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;
How to set Default Value as Value of SqlCommand SqlParameter?
SqlCommand is the class contained in System.Data.SqlClient
My Schema does not accept DBNull.Value as value
SqlCommand command = new SqlCommand();
command.Parameters.Add(new SqlParameter("column_1", SqlDbType.VarChar, 1) { Value = DBNull.Value });
As lboshuizen points out, if your "schema" isn't taking null, or the parameter of the stored procedure or query cannot be null, trying to give it a null value will likely fail. In this case, you need to see why you are trying to set it to null if you know it doesn't accept it. I would surmise an empty string as opposed to null would work, or something else that is sensible.
The SqlParameterCollection property Parameters has an AddWithValue method:
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue(v=vs.110).aspx
You can simply insert null and that will be handled for you:
command.Parameters.AddWithValue("#parameter", null);
If you cannot provide a null, you can provide something like an empty string:
command.Parameters.AddWithvalue("#parameter", "");
You also very rarely need to specify the data type using this mechanism.
If the schema doesn't accept null it indicates that the column is mandatory and requires a sensible value.
Using a default value for a column is a property of the schema.
So either alter the schema to provide a default.
ALTER TABLE {TABLENAME}
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL}
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}
Choose a valid (not null) default value in your application code and use it as a value in the parameter.
const string default = "unknown";
...
SqlCommand command = new SqlCommand();
command.Parameters.Add(new SqlParameter("column_1", SqlDbType.VarChar, 1)
{ Value = default });
Note: Changing the schema to accept NULL is considered cheating :-)
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.
I am currently trying to complete a transaction for a web based app, however;
Procedure or function 'completeTransaction' expects parameter '#dateTime', which was not supplied.
Here is copy of the function.
public static void completeTransaction(string storeCode, string employeeId, DateTime Date, string itemListNoId)
{
using (SqlConnection conn = new SqlConnection("Data Source = ; Initial Catalog =Business ; Integrated Security = true;"))
{
using (SqlCommand command = new SqlCommand("dbo.completeTransaction", conn))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("#storeCode", SqlDbType.Int).Value = storeCode;
command.Parameters.Add("#employeeId", SqlDbType.Int).Value = employeeId;
**command.Parameters.Add("#Date", SqlDbType.DateTime).Value = Date;**
command.Parameters.Add("#itemListNoId", SqlDbType.Int).Value = itemListNoId;
conn.Open();
command.ExecuteNonQuery();
conn.Close();
}
}
}
My sql table contains the following tables and types (storeCode, INT, employee, INT, Date, DATETIME, itemListNoId, INT)
You do not pass a parameter called #dateTime. It seems like this line
command.Parameters.Add("#Date", SqlDbType.DateTime).Value = Date;
Should be
command.Parameters.Add("#dateTime", SqlDbType.DateTime).Value = Date;
But without the SP source code it is hard to be sure. Keep in mind that SQL Server is complaining about the NAME of the parameter not about its type.
expects parameter '#dateTime'
You passed a parameter named #Date.
the name of the parameter is wrong:
command.Parameters.Add("#dateTime", SqlDbType.DateTime).Value = Date;
If you are getting this and you have passed in the correctly named parameter, check that the CommandType is set to Stored procedure
cmd.CommandType = CommandType.StoredProcedure;
I was seeing these same symptoms spent an embarrassingly long time tracking down how each parameter was getting to the stored proc.
As previous answers have correctly mentioned, most probable reasons for this error are either forgetting to add parameter(s) to the SqlCommand or forgetting to set command's type to CommandType.StoredProcedure
In case you have already set above correctly and still pulling your hair, then this might be the reason.
If you set parameters value to null (e.g. mySQLParam1.Value = valuePassedToMe and if valuePassedToMe is null) then you will get the same error (i.e. Procedure or function '...' expects parameter '...', which was not supplied).
This can be solved by assigning DBNull.Value when the value needs to be null
i.e.
mySQLParam1.Value = valuePassedToMe ?? (object)DBNull.Value;
When you assign null to a parameter ADO.Net converts it to default. Below is an example from SQL Server Profiler.
exec dbo.MyStoredProcedure #mySQLParam1=default,#mySQLParam2=default,#mySQLParam3=default,...
When you assign DBNull.Value the generated SQL becomes:
exec dbo.MyStoredProcedure #mySQLParam1=NULL,#mySQLParam2=NULL,#mySQLParam3=NULL,...
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;