How To Get Set Value From Stored procedure in C# - c#

I have a stored procedure that client provided me
Like:
ALTER Proc [dbo].[XYZ]
#Parameter varchar(100),
#Parameter1 nvarchar(4000) out
SET #APIString = "Test Test"
I have no rights to change this procedure.
When I execute procedure through C# I get a blank string from procedure
How to get the #Parameter1 value in my project?
C# Code:
SqlCommand cmd = new SqlCommand("dbo.XYZ", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#Parameter", Parameter);
cmd.Parameters.Add("#Parameter1", SqlDbType.VarChar,4000);
cmd.Parameters["#Parameter1"].Direction = ParameterDirection.Output;
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
String = reader["#Parameter1"] != null ? reader["#Parameter1"].ToString() : "";
}
conn.Close();

#Parameter1 is an output parameter. You can get its value the same way you set the values for input parameters, e.g.
var cmd = new SqlCommand("dbo.XYZ", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#Parameter", inputValue);
// add the output parameter
cmd.Parameters.Add("#Parameter1", SqlDbType.NVarChar).Direction =
ParameterDirection.Output;
cmd.ExecuteNonQuery();
string parameter1 = (string)cmd.Parameters["#Parameter1"].Value;
You should also use ExecuteNonQuery unless the store procedure returns values with a select statement.

Related

A SqlParameter with parameter name '#name' is not contained by this SqlParameterCollection function

I am trying to return a value from the code below but I am getting an error that says:
A SqlParameter with parameter name '#vRESULT' is not contained by this SqlParameterCollection
c# Code:
public int userLogin()
{
string connStr = ConfigurationManager.ConnectionStrings["conn"].ToString();
string cmdStr = #"fucn_LOg";
using (SqlConnection conn = new SqlConnection(connStr))
using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
{
try
{
conn.Open();
cmd.CommandText = cmdStr;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
cmd.Parameters[":vResult"].Direction = ParameterDirection.Output;
cmd.Parameters.Add(new SqlParameter("param1", SqlDbType.VarChar)).Value = TB_1.Text;
cmd.Parameters.Add(new SqlParameter("param2", SqlDbType.VarChar)).Value = TB_2.Text;
cmd.ExecuteScalar();
return Int32.Parse(cmd.Parameters[":vResult"].Value.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return -1;
}
}
}
the sql server function code below with returning parameter DECLARE #vResult int
CREATE FUNCTION USER_LOGIN(#USER_NAME VARCHAR(60),
#PWD VARCHAR(60))
RETURNS INT
AS BEGIN
DECLARE #vResult int
SELECT #vRESULT=COUNT(*)
FROM OPER
WHERE UPPER(UNAM)=UPPER(#USER_NAME)
AND PSW=#PWD
IF #vResult=1
SET #vResult=1
ELSE
SET #vResult= -1
RETURN #vResult
END
Just Get result from Stroed Procedure like this:
var result = cmd.ExecuteScalar();
return Int32.Parse(result.ToString());
This gets first and Only result from Stored Procedure.
Also recommend simplify your code Like this:
public int userLogin() {
string connStr = ConfigurationManager.ConnectionStrings["conn"].ToString();
using (SqlConnection conn = new SqlConnection(connStr))
using (SqlCommand cmd = new SqlCommand("fucn_LOg", conn)) {
try {
cmd.Connection.Open();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#param1", TB_1.Text);
cmd.Parameters.AddWithValue("#param2", TB_2.Text);
var result = cmd.ExecuteScalar();
return Int32.Parse(result.ToString());
}
catch (Exception ex) {
MessageBox.Show(ex.ToString());
return -1;
}
finally {
if (cmd.Connection.State != ConnectionState.Closed) cmd.Connection.Close();
}
}
}
And your Stored procedure should looks Like this:
CREATE PROCEDURE fucn_LOg
(
#param1 nvarchar(max),
#param2 nvarchar(max)
)
AS
BEGIN
SET NOCOUNT ON;
if (exists(select * from tbUsers where flLogin = #param1 and flPassword = #param2))
begin
return 1;
end
else
begin
return 0;
end
END
GO
OR
CREATE PROCEDURE fucn_LOg
(
#param1 nvarchar(max),
#param2 nvarchar(max)
)
AS
BEGIN
SET NOCOUNT ON;
select COUNT(*) from tbUsers where flLogin = #param1 and flPassword = #param2
END
GO
Several problems.
First, you don't need the cmd.Parameters.Clear();, as you just establish a new cmd.
Second, use # for SQL Server parameters.
Third, a parameter named vResult is not set, so cmd.Parameters[":vResult"].Direction is invalid. You need to assign its type and value. Make sure your stored procedure has this parameter set with correct SQL data type.
Lastly, I guess you return the vResult in your stored procedure like select #vResult; so make it a new vResult = function(vResult). But no, it is not how SQL Server work. It won't change your input parameter even though you return your #vResult. While, ExecuteScaler does. So, simply get your result back by var result = cmd.ExecuteScalar();.
You are getting data from a stored procedure, not getting back the parameter you sent. That's the supposed correct way.
conn.Open();
cmd.CommandText = cmdStr;
cmd.CommandType = CommandType.StoredProcedure;
//Base on sql you provided, it is no need for this part.
/*
SqlParameter vResult = new SqlParameter();
vResult.ParameterName = "#vResult";
vResult.Direction = ParameterDirection.Output;
vResult.SqlDbType = System.Data.SqlDbType.???;
vResult.Value = ???;
cmd.Parameters.Add(vResult);
*/
cmd.Parameters.Add("#param1", SqlDbType.VarChar).Value = TB_1.Text;
cmd.Parameters.Add("#param2", SqlDbType.VarChar).Value = TB_2.Text;
var result = cmd.ExecuteScalar();
return Int32.Parse(result.ToString());
This is hard to debug without the SP, but a couple of things jump out.
First, you need to use the '#' character as a prefix for your parameter names, not a colon.
Second, you should define your output parameter like this:
SqlParameter outputParam = new SqlParameter("#vResult", SqlDbType.Int);
outputParam.Direction = ParameterDirection.Output;
cmd.Parameters.Add(outputParam);

How to get return value from stored procedure

I have the following stored procedure:
ALTER PROCEDURE spLogin
#CAD_WEB_LOGIN_USERNAME varchar(60),
#CAD_WEB_LOGIN_PASSWORD varchar(60),
#Result int output
AS
BEGIN
SELECT
CAD_NUMBER
FROM
CMACADDR
WHERE
CAD_WEB_LOGIN_USERNAME = #CAD_WEB_LOGIN_USERNAME
AND CAD_WEB_LOGIN_PASSWORD = #CAD_WEB_LOGIN_PASSWORD
END
In C#, I want to execute this query and get the return value.
This is my code:
int flag = 0;
con.Open();
SqlCommand cmd3 = new SqlCommand("spLogin", con);
cmd3.Connection = con;
cmd3.CommandType = CommandType.StoredProcedure;
cmd3.Parameters.Add("#CAD_WEB_LOGIN_USERNAME", SqlDbType.VarChar).Value = txtUserName.Text;
cmd3.Parameters.Add("#CAD_WEB_LOGIN_PASSWORD", SqlDbType.VarChar).Value = txtPassword.Text;
SqlParameter parm = new SqlParameter("#Return", SqlDbType.Int);
parm.Direction = ParameterDirection.ReturnValue;
cmd3.Parameters.Add(parm);
flag = cmd3.ExecuteNonQuery();
con.Close();
int id = Convert.ToInt32(parm.Value);
I get an error:
Procedure or function 'spLogin' expects parameter '#Result', which was not supplied.
What's the logic error with this code?
Thanks
Change the ParameterDirection to Output , and change the parameter name to #Result .
SqlParameter parm = new SqlParameter("#Result", SqlDbType.Int);
parm.Direction = ParameterDirection.Output;
cmd3.Parameters.Add(parm);
As error suggest 'spLogin' expects parameter '#Result'
Change
SqlParameter parm = new SqlParameter("#Return", SqlDbType.Int);
to
SqlParameter parm = new SqlParameter("#Result", SqlDbType.Int);
EDIT
Also updated your procedure, return some value. Currently you are not returning anything. Also you don't need to add an extra parameter in SP.
ALTER PROCEDURE Splogin #CAD_WEB_LOGIN_USERNAME VARCHAR(60),
#CAD_WEB_LOGIN_PASSWORD VARCHAR(60)
AS
BEGIN
Declare #MyResult as INT
SELECT #MyResult = cad_number
FROM cmacaddr
WHERE cad_web_login_username = #CAD_WEB_LOGIN_USERNAME
AND cad_web_login_password = #CAD_WEB_LOGIN_PASSWORD
RETURN #MyResult -- return value
END

Stored procedure returns null as output parameter

I have stored procedure, which works great in MS SQL management studio.
When I try to use it in VS rows returns fine, but value of output parameters is NULL.
SqlCommand cmd = new SqlCommand("proc_name", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("#p_SomeVal", SqlDbType.Int));
cmd.Parameters["#p_SomeVal"].Direction = ParameterDirection.Output;
rdr = cmd.ExecuteReader();
//...process rows...
if (cmd.Parameters["#p_SomeVal"].Value != null)
SomeVal = (int)cmd.Parameters["#p_SomeVal"].Value;
cmd.ExecuteNonQuery(); Has the same result.
USE [db_name]
GO
DECLARE #return_value int,
#p_SomeValue int
EXEC #return_value = [dbo].[Proc_name]
#p_InputVal = N'aa',
#p_SomeValue = #p_SomeValue OUTPUT
SELECT #p_SomeValue as N'p_SomeValue'
SELECT 'Return Value' = #return_value
GO
SqlCommand cmd = new SqlCommand("proc_name", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("#p_SomeVal", SqlDbType.Int));
cmd.Parameters["#p_SomeVal"].Direction = ParameterDirection.Output;
rdr = cmd.ExecuteReader();
//...process rows...
rdr.Close();
if (cmd.Parameters["#p_SomeVal"].Value != null)
SomeVal = (int)cmd.Parameters["#p_SomeVal"].Value;
After procesing rows I added rdr.Close(); and worked fine.
Salaam,
You can check if output is null and convert like this.
returnedSQLParameter.Value != DBNull.Value? (int)returnedSQLParameter.Value : 0;
Because it is returning DBNull.value when output sent NULL from stored procedure.

ExecutenonQuery not working

I have stored proc as below:
ALTER PROC pr_Update_Users_Nomination
(
#UserID AS VARCHAR(100),
#Nominated AS BIT
)
AS
UPDATE User
SET isNominated = #Nominated
WHERE
EMPID = #UserID;
I want to call this procedure from c# code: Below is the code I am trying:
void OpenConnection()
{
string Nominated = "False";
//Connection String
string sConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ConString1"].ConnectionString;
SqlConnection mySqlCon = new SqlConnection(sConnString);
SqlCommand mySqlCom = mySqlCon.CreateCommand();
//Call the stored proc and provide in parameters
mySqlCom.CommandText = "EXECUTE pr_Update #UserID #Nominated";
mySqlCom.Parameters.Add("#UserID", SqlDbType.VarChar, 20).Value = UserID;
mySqlCom.Parameters.Add("#Nominated", SqlDbType.Bit).Value = Nominated;
mySqlCon.Open();
mySqlCom.ExecuteNonQuery();
mySqlCon.Close();
}
I get an error saying
Incorrect Syntax near #Nominated
first, when executing a procedure with parameter(s), separate the parameters with a comma
EXECUTE pr_Update #UserID, #Nominated
second, modify your code into this,
string sConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ConString1"].ConnectionString;
using(SqlConnection mySqlCon = new SqlConnection(sConnString))
{
using(SqlCommand mySqlCom = new SqlCommand())
{
mySqlCom.Connection = mySqlCon;
mySqlCom.CommandText = "pr_Update";
mySqlCom.CommandType = CommandType.StoredProcedure;
mySqlCom.Parameters.Add("#UserID", SqlDbType.VarChar, 20).Value = UserID;
mySqlCom.Parameters.Add("#Nominated", SqlDbType.Bit).Value = Nominated;
try
{
mySqlCon.Open();
mySqlCom.ExecuteNonQuery();
}
catch(SqlException ex)
{
// do something with the exception
// don't hide it
}
}
}
You are missing a comma (,) between the parameters.
It should be
mySqlCom.CommandText = "EXECUTE pr_Update #UserID, #Nominated";
mySqlCom.Parameters.Add("#UserID", SqlDbType.VarChar, 20).Value = UserID;
mySqlCom.Parameters.Add("#Nominated", SqlDbType.Bit).Value = Nominated;
Alternatively, since all you are doing is calling a stored proc, you could do:
mySqlCom.CommandType = CommandType.StoredProcedure ;
mySqlCom.CommandText = "pr_Update"; //no need to specify parameter names
mySqlCom.Parameters.Add("#UserID", SqlDbType.VarChar, 20).Value = UserID;
mySqlCom.Parameters.Add("#Nominated", SqlDbType.Bit).Value = Nominated;
Give only name of stored procedure, as you are adding parameter in statements after this. Also set CommandType.
mySqlCom.CommandText = "pr_Update";
mySqlCom.CommandType = CommandType.StoredProcedure;
You are invoking wrong SQL. You should set the command text of command to pr_Update only:
mySqlCom.CommandText = "pr_Update";
And set type command type to stored procedure:
mySqlCom.CommandType = CommandType.StoredProcedure;
See MSDN page for more.

Get value from stored procedure

i have the following stored procedure in sql server and i am trying to take the permission value with C#.
CREATE PROCEDURE [dbo].[GetPermission]
#userName varchar(50),
#permission int output
AS
BEGIN
select #permission = PERMISSION from USERS where UserName = #userName
END;
My C# code is the following:
SqlCommand cmd = new SqlCommand(
"sp_getPermission", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(
new SqlParameter("#UserName", textBox1.Text));
cmd.Parameters.Add(
new SqlParameter("#permission", "none"));
SqlDataReader rdr = null;
rdr = cmd.ExecuteReader();
MessageBox.Show( rdr["Permission"].ToString() );
But i get the following error on the last line of C# code:
Invalid attempt to read when no data is present.
Any suggestions?
Let's simply this:
first, remove the output parameter #permission, then, change your procedure like this:
CREATE PROCEDURE [dbo].[GetPermission]
#userName varchar(50)
AS
BEGIN
select PERMISSION from USERS where UserName = #userName
END;
and for read the permission, use the ExecuteScalar method:
SqlCommand cmd = new SqlCommand(
"sp_getPermission", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(
new SqlParameter("#UserName", textBox1.Text));
var permission = (System.Int32)cmd.ExecuteScalar();
You need to set the ParameterDirection of the permission parameter on the SqlCommand to ParameterDirection.Output
Also, as you are not resulting a resultset, no need to use ExecuteReader. Just do:
cmd.ExecuteNonQuery();
You need to Read your SqlDataReader:
rdr = cmd.ExecuteReader();
if (rdr.HasRows()){
rdr.Read()
MessageBox.Show( rdr["Permission"].ToString() );
rdr.Close()
}
However, I don't believe you need a SqlDataReader for this situation. The following should work:
cmd.Parameters.Add(
new SqlParameter("#permission", "none"));
cmd.Parameters["#permission"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
MessageBox.Show(cmd.Parameters["#permission"].Value);

Categories

Resources