This is a new issue of my previous question (C# Using Parameters in SqlHelper.DB). If I pass null as the parameters and set the command text to a parameterless stored procedure, it works fine.
SQLHelper.DB is executing the stored procedure but I get an error:
Unhandled Exception: System.Data.SqlClient.SqlException: Procedure or function 'sptest' expects parameter '@param1', which was not supplied.
This can be recreated by creating a new .NET 4.7.2 console app and installing Nuget package SQLHelper.DB then using the code below.
Console app code:
using Microsoft.Extensions.Configuration;
using SQLHelperDB;
using SQLHelperDB.HelperClasses;
using SQLHelperDB.HelperClasses.Interfaces;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
TestSql();
}
static void TestSql()
{
IParameter[] p = new IParameter[]
{
new Parameter<string>("@param1", "test1"),
new Parameter<string>("@param2", "test2")
};
IConfigurationRoot Configuration = new ConfigurationBuilder()
.AddInMemoryCollection()
.Build();
SQLHelper helper = new SQLHelper(Configuration, SqlClientFactory.Instance, "your connection string here");
helper.CreateBatch()
.AddQuery("dbo.sptest", CommandType.StoredProcedure, p)
.Execute();
}
}
}
Stored procedure:
CREATE PROCEDURE [dbo].[sptest]
(@param1 VARCHAR(10),
@param2 VARCHAR(10))
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO dbo.tbltest (field1, field2)
VALUES (@param1, @param2);
END;
Table:
CREATE TABLE [dbo].[tbltest]
(
[field1] [VARCHAR](10) NULL,
[field2] [VARCHAR](10) NULL
) ON [PRIMARY]