MS-SQL Stored Procedure doesn't work with empty params - c#

I have a form with inputs 'name' and 'phone'.
When they have values, everything works: a record is inserted into the database using a stored procedure, spFoo:
String sp = "spFoo 'test name','test phone'";
OdbcCommand command = new OdbcCommand(sp, connection);
command.CommandType = System.Data.CommandType.StoredProcedure;
connection.Open();
Response.Write(command.ExecuteNonQuery().ToString());
Works on application
Works on Mgmt Studio
But when they don't have values I get -1 as a response in the application:
String sp = "spFoo '',''";
Does not work in application (-1)
Works on Mgmt Studio
I want the user to be able to create a record without any input.
Why does this work in management studio, but not on the application?
Update: I added defaults to the params in the stored procedure, it didn't work; I gave empty strings "NULL" as values in the code, still no luck. Is this a server setting or something that won't allow empty variables?

You'll need to remove the SET NOCOUNT ON; from your stored procedure.
From the documentation:
Stops the message that shows the count of the number of rows affected by a Transact-SQL statement or stored procedure from being returned as part of the result set.
As others have pointed out you should parameterise your query too but the -1 is caused by NOCOUNT being ON.
Edit
I realise it's not what you're asking but to use a parameterised query with ODBC you need to use ?'s as ordinal place holders as per the documentation here. For example:
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
string sp = "{call spFoo (?, ?)}";
using (OdbcCommand command = new OdbcCommand(sp, connection))
{
command.CommandType = System.Data.CommandType.StoredProcedure;
connection.Open();
//the order here is important, the names are not!
command.Parameters.Add("#name", OdbcType.VarChar).Value = "test name";
command.Parameters.Add("#phone", OdbcType.VarChar).Value = "test phone";
Console.WriteLine(command.ExecuteNonQuery().ToString());
}
}

When you are calling a stored procedure from code, you should use the Parameters property on the command. Try this:
String sp = "spFoo";
command.Parameters.Add("#name", "test name");
command.Parameters.Add("#phone", "test phone");

As JimmyV said, you should use the command.Parameters.Add method to setup your parameters, passing in null whenever a parameter value is not specified. To address your comment about the error 'procedure or function 'spFoo' expects parameter '#name', which was not supplied', you'll also need to modify your stored procedure to use default values when a param is not supplied (e.g. null):
CREATE PROCEDURE MyStoredProcedure
#foo int = null
AS
BEGIN
...
END
Sorry for not adding this a comment on the above post. Not enough reputation!

You shouldn't be calling a stored procedure the way that you currently are. You should be using parameters. Your code is susceptible to SQL injection.
Never string concat user inputted values.
What you should have, is a stored procedure setup similarly:
CREATE PROCEDURE spFoo
#name varchar(50) = 'Jim', -- default
#phone varchar(50) = null -- optional
AS
BEGIN
SET NOCOUNT ON;
-- INSERT STATEMENT
END
GO
And then supply the parameters in the code:
string name = this.nameTextBox.Text;
string phone = this.phoneTextBox.Text;
if (string.IsNullOrWhiteSpace(name))
name = null;
if (string.IsNullOrWhiteSpace(phone))
phone = null;
SqlConnection connection = new SqlConnection(#"<connection string>");
using (SqlCommand command = connection.CreateCommand())
{
command.CommandType = CommandType.StoredProcedure;
// leave this as the stored procedure name only
command.CommandText = "spFoo";
// if name is null, then Jim gets passed (see stored procedure definition)
// if phone is null, then null gets passed (see stored procedure definition)
command.Parameters.AddWithValue("#name", name);
command.Parameters.AddWithValue("#phone", phone);
try
{
connection.Open();
int result = command.ExecuteNonQuery();
Console.WriteLine(result);
}
finally
{
if (connection.State != ConnectionState.Closed)
connection.Close();
}
}
I'm not sure why you used the Odbc namespace objects since it sounds like you are using MS-SQL. You should be using objects from the System.Data.SqlClient namespace.
The answer to your actual question would most likely involve executing a script (not a stored procedure) similar to:
DECLARE #RC int
DECLARE #name varchar(50)
DECLARE #phone varchar(50)
-- TODO: Set parameter values here.
EXECUTE #RC = spFoo
#name,
#phone
GO
Which is not recommended.

Related

Return newly created stored procedure C#

I've tried two different thoughts in resolving this issue without success. I've look at the following links here, here and here but I end up with
Object reference not set to an instance of an object.
or
Procedure or function InsertHero has too many arguments specified.
When I execute the stored procedure in SSMS, my IDs also appear off in that they are incrementing from 1110 instead of the original number set at 100 (this might be a different issue).
It seems like I'm 99% of the way there and I'm missing a small piece. What am I missing?
C#:
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand("InsertHero", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("#Name", hero.Name);
command.Parameters.AddWithValue("#Universe", hero.Universe);
// command.Parameters.Add("#Created_Id", SqlDbType.Int).Direction = ParameterDirection.Output;
int id = (int)command.ExecuteScalar();
}
}
Stored procedure:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[InsertHero]
#Name nvarchar(50),
#Universe nvarchar(15),
#Created_Id int OUTPUT
AS
INSERT INTO Hero
VALUES (#Name, #Universe);
SET #Created_Id = SCOPE_IDENTITY()
GO
Stored procedure (different way):
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[InsertHero]
#Name nvarchar(50),
#Universe nvarchar(15) OUTPUT
AS
INSERT INTO Hero
OUTPUT inserted.Id
VALUES (#Name, #Universe);
GO
EDIT:
Leaving this here in case someone stumbles upon it. Turned out to be a problem with the connection string so be sure to double and maybe triple check
There is no need for the #CreatedID parameter in the first example. Just change your stored procedure to
ALTER procedure [dbo].[InsertHero] #Name nvarchar(50), #Universe nvarchar(15)
as
Insert into Hero values (#Name, #Universe);
SELECT SCOPE_IDENTITY()
When you call ExecuteScalar the result is expected as the return from a SELECT statement not from an output parameter. If you want to use an output paramenter then you need to add it to the SqlCommand parameters collection and read it back its value from the same collection after executing the command.
Also do not use AddWithValue, this method while handy has numerous drawbacks documented in Can we Stop using AddWithValue Already and also in How data access code affects Database performances
Instead use Add specifying type and size of the two parameters
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand("InsertHero", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("#Name", SqlDbType.NVarChar, 50).Value = hero.Name;
command.Parameters.Add("#Universe", SqlDbType.NVarChar, 15).Value = hero.Universe;
int id = (int)command.ExecuteScalar();
}
}
The second method should work as well if you change back the #Universe parameter to the same definition of the first example (No output clause).
ALTER procedure [dbo].[InsertHero] #Name nvarchar(50), #Universe nvarchar(15)

SQL Server stored procedure doesn't return a result in VS C# but does in SQL Server

I have written a stored procedure that returns XML. When I run the stored procedure in SQL Server management studio, it returns the xml as it should but, when I run it in my C# code, no xml is returned. My code looks like this:
using (SqlConnection conn = new SqlConnection(connStr)) {
using (SqlCommand cmnd = new SqlCommand("dbo.spMyStoredProcXML", conn)) {
cmnd.CommandType = CommandType.StoredProcedure;
cmnd.Parameters.Add("#Param1", SqlDbType.VarChar, 50);
cmnd.Parameters["#Param1"].Value = "Some value";
//more parameters...
conn.Open();
XmlReader xrdr = cmnd.ExecuteXmlReader();
//do stuff...
}
}
I have checked the permissions for the user that I created, and they look like this:
At the server level, in the Login Properties dialog, under User Mapping, I have mapped the user to the correct database, and "checked" the public, db_datareader, db_datawriter, and then even added them to db_owner roles.
At the database level, I have also gone into the "Securables" section of the Database User dialog and explicitly given the user Grant and With Grant permissions to Execute the stored procedure in question. I have also explicitly selected "Grant" permissions for the user to Delete, Insert, Select, and Update all the tables in the database.
Still, when the "XmlReader xrdr = cmnd.ExecuteXmlReader();" line runs, the xrdr variable is empty. No error is reported. Can someone give suggestions as to what else I need to examine?
Thanks in advance for any help you can provide.
Here is a simplified version of the stored procedure (which works perfectly in SQL Server Management Studio):
CREATE PROCEDURE [dbo].[spGetXML]
(#param1 varchar(50), #param2 varchar(1))
AS
BEGIN
SET NOCOUNT ON;
with xmlnamespaces ('http://schemas.xmlsoap.org/soap/encoding/' as SOAP_ENC)
select
d.Col1,
d.Col2,
from MyTable d
where d.Col1 = #param1 and
d.Col2 = #param2
FOR XML PATH('ObjectName'), root('DOCUMENT'), type
END
I suppose your StoredProcedure has already the necessary fields.
So, try this:
using (SqlConnection conn = new SqlConnection(connStr)) {
conn.Open();
using (SqlCommand cmnd = new SqlCommand("dbo.spMyStoredProcXML", conn)) {
cmnd.CommandType = CommandType.StoredProcedure;
cmnd.Parameters.AddWithValue("#Param1", "Some value";)
//more parameters...
XmlReader xrdr = cmnd.ExecuteXmlReader();
//do stuff...
}
}
Maybe try this:
In your procedure make sure your parameter is defined as an output param:
CREATE PROCEDURE [dbo].[MyStoredProcedure]
#Param1 xml OUTPUT
SET #Param1 = (SELECT XML from TABLE)
Then in your C# code:
cm.Parameters.Add("#Param1", SqlDbType.Xml).Direction = ParameterDirection.Output;
Param1 = Convert.ToString(cm.Parameters["#Param1"].Value);
Obviously, adjust your datatypes to whatever is fitting. Additionally, if you're doing a bunch of work in this procedure I would personally use ExecuteNonQuery as opposed to using an XMLReader

One function for multiple stored procedures C#

I have a function that executes stored procedures. The thing is, I wish to use this one function for multiple stored procedures that take different arguments.
Of course, if I do so, I will get an error saying that
Procedure or function has too many arguments specified
What I want to do is to create the function such that when it executes a stored procedure, that stored procedure only takes the parameters that it needs and ignore the others. Is this possible?
Here is what I have so far:
try
{
using (SqlConnection con = new SqlConnection(consr))
{
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.CommandText = stp;
cmd.Parameters.Add(new SqlParameter("#proc1", cmb1.SelectedItem.ToString()));
cmd.Parameters.Add(new SqlParameter("#proc2", cmb2.SelectedItem.ToString()));
cmd.Parameters.Add(new SqlParameter("#proc3", cmb3.SelectedItem.ToString()));
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
con.Open();
reader = cmd.ExecuteReader();
con.Close();
}
}
catch (SqlException exp)
{
throw new InvalidOperationException(exp.Message);
}
Here are two of the procedures:
ALTER PROCEDURE [dbo].[test1]
#proc1 varchar(20)
AS
Begin
select * from tab where name=#proc1
END
and
ALTER PROCEDURE [dbo].[test1]
#proc2 varchar(20)
AS
Begin
select * from tab where name=#proc2
END
I want to use the same function to execute both
You can set the params you don't want to use to null, then check for which values are available and respond accordingly
ALTER PROCEDURE [dbo].[test1]
#proc1 varchar(20) = null
#proc2 varchar(20) = null
#proc3 varchar(20) = null
AS
Begin
IF #proc1 IS NOT NULL BEGIN
select * from tab where name=#proc1
END
END
Make your function take two arguments, the name of the SP and a 2d array of SP argument names and their corresponding values. Then set up your sql command by iterating over the 2d array and adding the parameters in the loop.
If you need to return several result sets, see here: How do I return multiple result sets with SqlCommand? and modify the function and args so that they take an array of SP names and an array of 2d arrays, containing the SP arg names and values for each SP.
I think you should use this structure :
CREATE PROCEDURE MyProcName
#Parameter1 INT = 1,
#Parameter2 VARCHAR (100) = 'StringValue',
#Parameter3 VARCHAR (100) = NULL
AS
/* check for the NULL / default value (indicating nothing was passed */
if (#Parameter3 IS NULL)
BEGIN
/* whatever code you desire for a missing parameter*/
INSERT INTO ........
END
/* and use it in the query as so*/
SELECT *
FROM Table
WHERE Column = #Parameter
Can you please share the stored procedure code? I think you need to use if else or case statement in your function and call appropriate stored procedure with required parameters inside your function.

Visual C# stored procedure asking for parameter that isn't needed

Stored procedure:
ALTER PROCEDURE VendorsRowcount
#RowCount int OUTPUT
AS
SET NOCOUNT ON
SELECT *
FROM dbo.Vendors
SET #RowCount = ##ROWCOUNT
RETURN #RowCount
C#:
using (var conn = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=Pricer;Persist Security Info=True;User ID=xxx;Password=xxx"))
using (var command = new SqlCommand("VendorsRowcount", conn)
{
CommandType = CommandType.StoredProcedure
})
{
conn.Open();
command.ExecuteNonQuery();
conn.Close();
}
I am getting the error:
Additional information: Procedure or function 'VendorsRowcount' expects parameter '#RowCount', which was not supplied.
I am just learning C# after setting out to learn VB and realizing that there are a lot more resources on the internet for C#.
This is probably a stupid question, but I have searched and maybe the terms I use are not the correct ones, because I can not find an answer.
To the best of my knowledge, I don't need to send a parameter because #RowCount is output.
Why do I get this error?
If you declare a parameter in the stored procedure, it has no relevance the fact that is declared as OUTPUT. You need to pass it from your C# code. The alternative is to declare the parameter as optional as shown in another answer. However you have now a problem. How do you read back in your C# code that parameter's value?
First option, pass the parameter at the stored procedure and read it back
conn.Open();
SqlParameter prm = command.Parameters.Add(new SqlParameter("#RowCount", SqlDbType.Int));
prm.Direction = ParameterDirection.Output;
command.ExecuteNonQuery();
Console.WriteLine(prm.Value.ToString());
conn.Close();
Second option, set the parameter as optional, call the SqlCommandBuilder.DeriveParameters method to fill the Parameter collection on the C# side and read it back. (Please read the remarks section in the link provided about the efficiency of this solution)
-- in the stored procedure
#RowCount int = 0 OUTPUT
conn.Open();
SqlCommandBuilder.DeriveParameters(command);
command.ExecuteNonQuery();
Console.WriteLine(command.Parameters["#RowCount"].Value.ToString());
conn.Close();
However I am puzzled by the fact that you run a potentially costly SELECT * command but you don't seems to be interested in the records returned.
In this context, a StoredProcedure seems to be excessive and adds a maintenance issue while you could get the row count simply writing:
conn.Open();
command.CommandText = "SELECT COUNT(*) FROM Vendors";
int rowCount = Convert.ToInt32(command.ExecuteScalar());
Console.WriteLine(rowCount.ToString());
conn.Close();
You need to pass in that parameter. Here's a good example of how to do this:
Get output parameter value in ADO.NET
If the parameter should be optional, you must provide a default value in your Stored Procedure.
For example:
#RowCount int OUTPUT = 0

SQL Server stored procedure that returns a boolean if table exists, c# implementation

I have created a stored procedure that takes a single argument, the name of a table, and returns 1 if it exists in the database, 0 if it does not. In SQL Server Management Studio testing my stored procedure works exactly as I'd like it to, however I'm having trouble getting that value for use in my C# program.
My options seem to be ExecuteScalar(), ExecuteNonQuery() or ExecuteReader(), none of which seem appropriate for the task, nor can I get them to even retrieve my stored procedure's result.
I have tried assigning my parameter with both cmd.Parameters.AddWithValue and cmd.Parameters.Add again to no avail.
Assuming you have a stored procedure like this which selects either a 0 (table does not exist) or 1 (table does exist)
CREATE PROCEDURE dbo.DoesTableExist (#TableName NVARCHAR(100))
AS
BEGIN
IF EXISTS (SELECT * FROM sys.tables WHERE Name = #TableName)
SELECT 1
ELSE
SELECT 0
END
then you can write this C# code to get the value - use .ExecuteScalar() since you're expecting only a single row, single column:
// set up connection and command
using (SqlConnection conn = new SqlConnection("your-connection-string-here"))
using (SqlCommand cmd = new SqlCommand("dbo.DoesTableExist", conn))
{
// define command to be stored procedure
cmd.CommandType = CommandType.StoredProcedure;
// add parameter
cmd.Parameters.Add("#TableName", SqlDbType.NVarChar, 100).Value = "your-table-name-here";
// open connection, execute command, close connection
conn.Open();
int result = (int)cmd.ExecuteScalar();
conn.Close();
}
Now result will contain either a 0 if the table doesn't exist - or 1, if it does exist.
Use this:
var returnParameter = cmd.Parameters.Add("#ReturnVal", SqlDbType.Int);
returnParameter.Direction = ParameterDirection.ReturnValue;
Your stored procedure should return 0 or 1.

Categories