Stored procedure or function expects parameter which was not supplied - c#

I am trying to insert data into a SQL Server database by calling a stored procedure, but I am getting the error
*Procedure or function 'Insertion' expects parameter '#Emp_no', which was not supplied*
My stored procedure is called Insertion. I have checked it thoroughly and no parameters is missing also I have checked it by using a label. The label shows the value but I don't know why I am getting the error.
My code is
try
{
SqlCommand cmd = new SqlCommand();
cmd.Parameters.Clear();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Insertion";
cmd.Connection = con;
if (rdb_Male.Checked)
{
int #Emp_no = Convert.ToInt32(txtbx_Empno.Text);
string #Emp_name = txtbx_Emp_Name.Text;
double #phone = Convert.ToDouble(txtbx_Phone.Text);
string #Email = txtbx_Email.Text;
string #Password = txtbx_Pwd.Text;
string #Gender = rdb_Male.Text;
DateTime #Dob = Convert.ToDateTime(dob);
string #Address = txtbx_Address.Text;
string #Designation = txtbx_Designation.Text;
string #Qualification = txtbx_Qual.Text;
double #Experience = Convert.ToDouble(txtbx_Exp.Text);
double #Salary = Convert.ToDouble(txtbx_Sal.Text);
DateTime #Doj = Convert.ToDateTime(doj);
}
else if (rdb_Female.Checked)
{
int #Emp_no = Convert.ToInt32(txtbx_Empno.Text);
string #Emp_name = txtbx_Emp_Name.Text;
double #phone = Convert.ToDouble(txtbx_Phone.Text);
string #Email = txtbx_Email.Text;
string #Password = txtbx_Pwd.Text;
string #Gender = rdb_Female.Text;
DateTime #Dob = Convert.ToDateTime(dob);
string #Address = txtbx_Address.Text;
string #Designation = txtbx_Designation.Text;
string #Qualification = txtbx_Qual.Text;
double #Experience = Convert.ToDouble(txtbx_Exp.Text);
double #Salary = Convert.ToDouble(txtbx_Sal.Text);
DateTime #Doj = Convert.ToDateTime(doj);
}
if (con.State==ConnectionState.Closed)
con.Open();
LABEL.Text = txtbx_Empno.Text;
cmd.ExecuteNonQuery();
lbl_Errormsg.Visible = true;
lbl_Errormsg.Text = "Record Inserted Successfully";
con.Close();
}
and the stored procedure is
ALTER PROCEDURE dbo.Insertion
(
#Emp_no int,
#Emp_name varchar(30),
#phone numeric(10,0),
#Email varchar(30),
#Password varchar(10),
#Gender varchar(6),
#Dob date,
#Address varchar(100),
#Designation varchar(20),
#Qualification varchar(20),
#Experience numeric(4,2),
#Salary numeric(10,2),
#Doj date
)
AS
Begin
Insert into Register (Emp_no, Emp_name, phone, Email, Password, Gender, Dob, Address, Designation, Qualification, Experience, Salary, Doj)
Values(#Emp_no, #Emp_name, #phone, #Email, #Password, #Gender, #Dob, #Address, #Designation, #Qualification, #Experience, #Salary, #Doj)
End
Please help me. Thanks in advance.

Just a headsup, it might save someone a lot of time soul searching. If you have followed the recommendation here, like using AddWithValue in order to pass a paramter on, and you have everything verified and yet you are still getting the error message "Not supplied", check whether you have set the CommandType property of the command object to CommandType.StoredProcedure.
Not setting this property incurs the same message, believe me! Hope it helps someone.

You need to use SqlCommand.Parameters.AddWithValue:
cmd.Parameters.AddWithValue("#ParameterName", value);
or SqlCommand.Parameters.Add for other data types:
cmd.Parameters.Add("#ParameterName", SqlDbType.Int, 5);
cmd.Parameters["#ParameterName"].Value = value;
SqlCommand.Parameters.AddWithValue replaces the ambiguous overload of Add that took a string and object parameter. See MSDN for more info.

For others : I just faced the same error because one of my parameters was null. We need to check for it such as :
command.Parameters.AddWithValue("#phone", (object)phone?? DBNull.Value);

You need to use this:
cmd.Parameters.AddWithValue("#Emp_no", #Emp_no);
cmd.Parameters.AddWithValue("#Emp_name", #Emp_name);
cmd.Parameters.AddWithValue("#phone", #phone);
cmd.Parameters.AddWithValue("#Email", #Email);
cmd.Parameters.AddWithValue("#Password", #Password);
cmd.Parameters.AddWithValue("#Gender", #Gender);
cmd.Parameters.AddWithValue("#Dob", #Dob);
cmd.Parameters.AddWithValue("#Address", #Address);
cmd.Parameters.AddWithValue("#Designation", #Designation);
cmd.Parameters.AddWithValue("#Experience", #Experience);
cmd.Parameters.AddWithValue("#Salary", #Salary);
cmd.Parameters.AddWithValue("#Doj", #Doj);
Otherwise, it will throw that exception for each of the parameters.

Your Insertion stored procedure is expecting #Emp_no (along with about 15 other parameters). You cannot call the stored procedure without passing the parameters.
Take a look at this site for reference:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2
Everywhere you're defining variables, use Parameters.AddWithValue instead:
cmd.Parameters.AddWithValue("#Emp_no ", Convert.ToInt32(txtbx_Empno.Text));

This is how it can be done
using (var cmd = new SqlCommand("STORED_PROCEDURE_NAME", SqlConnection))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#PARAM_NAME", PARAM_VALUE);
}
Notice that AddWithValue, and CommandType.StoredProcedure both are essential.

"There's only one Add method that's obsoleted, the method that accepts a string for the parameter name and an object for the value. As you noted, you should call AddWithValue instead for this scenario."
http://social.msdn.microsoft.com/Forums/en-US/15bb16a4-0cf1-4289-b677-3b9d98f09298/parametersaddwithvalue-output-parameter-in-adonet-2?forum=adodotnetdataproviders
Not all Parameter.Add methods are depreciated. How are you suppose to make an OUTPUT parameter? You have to use Parameter.Add for this.

Same error message still these days scoping multiple problems - my issue was passing a null value to the parameter. The answer is to null check like so:
parameter.Value = (object)yourParamVal ?? DBNULL.Value;

Avoid parameters that have no value with the parameter set being DBNull.value

Related

Why i can not handle the store procedure return value is c#?

I'm new in c#,and want to write simple application work with sql server store procedure,in the sql server write this store procedure:
USE [mammutRecruitment]
ALTER PROCEDURE [dbo].[FirstStep]
#Name nvarchar(max),#Family nvarchar(max),#FatherName nvarchar(max),#BirthCertificate bigint,#PlaceOfBirth nvarchar(max),#BirthDate datetime,
#NationalCode bigint,#Religion nvarchar(max),#faith nvarchar(max),#Nationality nvarchar(max),#BloodGroup nvarchar(max)
AS
BEGIN
DECLARE #MYID bigint
insert into [dbo].[UserMainSpecifications] values(#Name,#Family,#FatherName,#BirthCertificate,#PlaceOfBirth,1,#BirthDate,#NationalCode,
#Religion,#faith,#Nationality,#BloodGroup,12,'123','123',1,2015-1-1,'12','123','1234',1)
select #MYID=[UserID] from [mammutRecruitment].[dbo].[UserMainSpecifications]
where [NationalCode]=#NationalCode
select #MYID as myID
END
and in c# write this code for call that:
using (SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=mammutRecruitment;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand("FirstStep", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#Name", SqlDbType.NVarChar).Value = m.Name;
cmd.Parameters.Add("#Family", SqlDbType.NVarChar).Value = m.Family;
cmd.Parameters.Add("#FatherName", SqlDbType.NVarChar).Value = m.FatherName;
cmd.Parameters.Add("#BirthCertificate", SqlDbType.BigInt).Value =Convert.ToInt64(m.BirthCertificate);
cmd.Parameters.Add("#PlaceOfBirth", SqlDbType.NVarChar).Value = m.PlaceOfBirth;
cmd.Parameters.Add("#BirthDate", SqlDbType.DateTime).Value =Convert.ToDateTime(dt.ToString());
cmd.Parameters.Add("#NationalCode", SqlDbType.BigInt).Value =Convert.ToInt64(m.NationalCode);
cmd.Parameters.Add("#Religion", SqlDbType.NVarChar).Value = m.Religion;
cmd.Parameters.Add("#faith", SqlDbType.NVarChar).Value = m.faith;
cmd.Parameters.Add("#Nationality", SqlDbType.NVarChar).Value = m.Nationality;
cmd.Parameters.Add("#BloodGroup", SqlDbType.NVarChar).Value = m.BloodGroup;
SqlParameter retval = cmd.Parameters.Add("#myID", SqlDbType.BigInt);
retval.Direction = ParameterDirection.ReturnValue;
con.Open();
cmd.ExecuteNonQuery();
var retunvalue = cmd.Parameters["#myID"].Value;
but in the this line i get zero value always:
var retunvalue = cmd.Parameters["#myID"].Value;
What happen?How can i solve that problem?thanks.
This line:
cmd.ExecuteNonQuery();
Is executing your query and not returning a value.
You could start by looking into using this instead:
cmd.ExecuteReader();
Or if you want the value of the first field of the first row, you could use this:
var returnValue = cmd.ExecuteScalar();
Which will give you an object that you can then convert or cast into the appropriate type for your method.
SqlCommand.ExecuteNonQuery Method ()
Executes a Transact-SQL statement against the connection and returns
the number of rows affected.
SqlCommand.ExecuteScalar Method ()
Executes the query, and returns the first column of the first row in
the result set returned by the query. Additional columns or rows are
ignored.
I believe you want the second method
You need to include #myID as an output parameter in your stored procedure definition:
ALTER PROCEDURE [dbo].[FirstStep]
#Name nvarchar(max),#Family nvarchar(max),#FatherName nvarchar(max),#BirthCertificate bigint,#PlaceOfBirth nvarchar(max),#BirthDate datetime,
#NationalCode bigint,#Religion nvarchar(max),#faith nvarchar(max),#Nationality nvarchar(max),#BloodGroup nvarchar(max)
, #myID bigint output
AS
And then remove the line
DECLARE #MYID bigint
You also need to add the parameter to cmd in your c# code:
cmd.Parameters.Add(retval);
you need define #MYID as output parameter.
USE [mammutRecruitment]
ALTER PROCEDURE [dbo].[FirstStep]
#MYID bigint output,#Name nvarchar(max),#Family nvarchar(max),#FatherName nvarchar(max),#BirthCertificate bigint,#PlaceOfBirth nvarchar(max),#BirthDate datetime,
#NationalCode bigint,#Religion nvarchar(max),#faith nvarchar(max),#Nationality nvarchar(max),#BloodGroup nvarchar(max)
AS
BEGIN
insert into [dbo].[UserMainSpecifications] values(#Name,#Family,#FatherName,#BirthCertificate,#PlaceOfBirth,1,#BirthDate,#NationalCode,
#Religion,#faith,#Nationality,#BloodGroup,12,'123','123',1,2015-1-1,'12','123','1234',1)
select #MYID = SCOPE_IDENTITY()
END
and add your command
SqlParameter pOut = new SqlParameter("#MYID", SqlDbType.BigInt);
pOut.Direction = ParameterDirection.Output;
cmd.Parameters.Add(pOut);
You can read the value now
cmd.ExecuteNonQuery();
long newId = (long)pOut.Value;
Actually, you need change that statement:
retval.Direction = ParameterDirection.ReturnValue;
to
retval.Direction = ParameterDirection.Output
because ExecuteNonQuery return:
Executes a Transact-SQL statement against the connection and returns the number of rows affected.
Although the ExecuteNonQuery returns no rows, any output parameters or return values mapped to parameters are populated with data.
End include your parameter to procedure definition:
#myID BIGINT OUTPUT
USE [mammutRecruitment]
ALTER PROCEDURE [dbo].[Firststep] #Name NVARCHAR(max),
#Family NVARCHAR(max),
#FatherName NVARCHAR(max),
#BirthCertificate BIGINT,
#PlaceOfBirth NVARCHAR(max),
#BirthDate DATETIME,
#NationalCode BIGINT,
#Religion NVARCHAR(max),
#faith NVARCHAR(max),
#Nationality NVARCHAR(max),
#BloodGroup NVARCHAR(max),
#myID BIGINT output
AS
BEGIN
INSERT INTO [dbo].[usermainspecifications]
VALUES (#Name,
#Family,
#FatherName,
#BirthCertificate,
#PlaceOfBirth,
1,
#BirthDate,
#NationalCode,
#Religion,
#faith,
#Nationality,
#BloodGroup,
12,
'123',
'123',
1,
2015 - 1 - 1,
'12',
'123',
'1234',
1)
SELECT #myID = [userid]
FROM [mammutRecruitment].[dbo].[usermainspecifications]
WHERE [nationalcode] = #NationalCode
END

C# stored procedure error returning value [duplicate]

This question already has answers here:
Return Value from a Stored Procedure
(2 answers)
Closed 8 years ago.
I never worked with a stored procedure that returned a value in C#. I tried looking on the internet and I haven't found anything that helps. It probably because I really don't understand how it works.
STORED PROCEDURE - dbo.sp_Admin_AddNewUser
USE [NGE]
GO
/****** Object: StoredProcedure [dbo].[sp_Admin_AddNewUser] Script Date: 05/19/2014 15:07:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_Admin_AddNewUser]
(
#firstname varchar(50),
#lastname varchar(50),
#middlename varchar(50),
#phone_office varchar(50),
#retval varchar(50) OUTPUT
)
AS
INSERT INTO EDUSER
(firstname,
lastname,
middlename,
phone_office,
synceratorInactiveFlag)
VALUES(
rtrim(ltrim(#firstname)),
rtrim(ltrim(#lastname)),
rtrim(ltrim(#middlename)),
rtrim(ltrim(#phone_office)),
'A')
IF ##error = 0
SELECT #retval = rtrim(ltrim(SCOPE_IDENTITY()))
--Add the user to the group
IF EXISTS (SELECT 1 FROM EDGROUP WHERE [group_id] = #GroupID)
BEGIN
INSERT INTO [NGE].[dbo].[EDGROUPPRACTICE]
([group_id]
,[user_id])
VALUES
(#GroupID
,#retval)
END
Code to call the store procedure:
SqlCommand cmd = new SqlCommand("dbo.sp_Admin_AddNewUser", conn);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
cmd.Parameters.Add(new SqlParameter("#firstname","Test"));
cmd.Parameters.Add(new SqlParameter("#lastname", "User"));
cmd.Parameters.Add(new SqlParameter("#middlename", "M"));
cmd.Parameters.Add(new SqlParameter("#phone_office", "(555) 555-5555"));
cmd.Parameters.Add("#retval", "");
cmd.Parameters["#retval"].Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
tempUserID = (string)cmd.Parameters["#retval"].Value; //GET ERROR HERE
I get an error:
Procedure or function expects parameter '#retval' which was not supplied
Try to add the parameters this way:
SqlParameter firstName = cmd.Parameters.Add("#firstname", SqlDbType.VarChar, 50);
nikParam.Direction = ParameterDirection.Input;
//Rest of the params
//Then the output param
SqlParameter retValOutput = cmd.Parameters.Add("#retval", SqlDbType.VarChar, 50);
nikParam.Direction = ParameterDirection.Output;
I hope this helps ;)
It is not return value, it is just output parameter. Use
cmd.Parameters["#retval"].Direction = ParameterDirection.Output;
instead of
cmd.Parameters["#retval"].Direction = ParameterDirection.ReturnValue;
To have return value you must do
RETURN #retval
in your SP.

Procedure or function expects parameter ' which is not supplied

hello friends i face one issue for load the data to grid view.
the page load event call the one method like loaddata() inside i write the code this
using (SqlConnection Sqlcon = new SqlConnection(strCon))
{
using (SqlCommand cmd = new SqlCommand())
{
Sqlcon.Open();
cmd.Connection = Sqlcon;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SP_Marketing";
//cmd.Parameters.Add(new SqlParameter("#Sno", (object) ?? null.Value));
cmd.Parameters.Add(new SqlParameter("#pvchAction", SqlDbType.VarChar,50));
cmd.Parameters["#pvchAction"].Value = "select";
cmd.Parameters.Add("#pIntErrDescOut", SqlDbType.Int).Direction = ParameterDirection.Output;
SqlAda = new SqlDataAdapter(cmd);
ds = new DataSet();
SqlAda.Fill(ds);
GridViewSample.DataSource = ds;
GridViewSample.DataBind();
}
}
the find the store procedure records in sqlada caught error like"Procedure or function expects parameter #dateemailed which is not supplied"
ALTER PROCEDURE SP_Marketing
(
#Sno int =0,
#DateEmailed datetime,
#DateResponded datetime,
#EmailRep varchar(100)=null,
#Type varchar(100)=null,
#Country varchar(100)=null,
#State varchar(100)=null,
#NameoftheCompany varchar(100)=null,
#website varchar(100)=null,
#FirstName varchar(100)=null,
#LastName varchar(100)=null,
#Title varchar(100)=null,
#Email varchar(100)=null,
#Telephone varchar(100)=null,
#Capabilities varchar(100)=null,
#Focus varchar(100)=null,
#pvchCreatedBy varchar(100)=null,
#pvchAction varchar(50)=null,
#pIntErrDescOut int output
)
AS
BEGIN
if(#pvchAction='select')
begin
SELECT sno,Dateemailed,dateresponded,emailrep,[type],country,[state], , nameofthecompany,website,Firstname,Lastname,Title,email,telephone,
capabilities, Focus FROM Emailmarketing WHERE active=1
end
else if(#pvchAction='insert')
begin
INSERT INTO EmailMarketing(DateEmailed,DateResponded,EmailRep,[Type],
Country,[State],NameoftheCompany,website,FirstName,LastName,Title,Email,Telephone,Capabilities,Focus,Createdby,CreatedDt,Active)VALUES(#DateEmailed,#DateResponded,#EmailRep,#Type,
#Country,#State,#NameoftheCompany,#website,#FirstName,#LastName,#Title,#Email,#Telephone,#Capabilities,#Focus,#pvchCreatedBy,GETDATE(),1);
end
else if(#pvchAction='update')
begin
UPDATE EmailMarketing SET DateEmailed=#DateEmailed,DateResponded=#DateResponded,EmailRep=#EmailRep,[Type]=#Type,Country=#Country,[State]=#State,NameoftheCompany=#NameoftheCompany,website=#website,FirstName=#FirstName,LastName=#LastName,Title=#Title,Email=#Email,Telephone=#Telephone,Capabilities=#Capabilities,Focus=#Focus,Updatedby=#pvchCreatedBy,UpdatedDt=GETDATE()
WHERE Sno=#Sno;
end
else if(#pvchAction='delete')
begin
UPDATE EmailMarketing SET Active=#pvchAction WHERE Sno=#Sno;
end
IF (##ERROR <> 0)
BEGIN
SET #pIntErrDescOut = 1
END
ELSE
BEGIN
SET #pIntErrDescOut = 0
END
END
The following parameters don't have default values in your stored proc:
#DateEmailed datetime,
#DateResponded datetime,
So you always need to provide those values in the code:
cmd.Parameters.Add(new SqlParameter("#DateEmailed", SqlDbType.DateTime));
cmd.Parameters["#DateEmailed"].Value = DateTime.Now; // Provide your value here
cmd.Parameters.Add(new SqlParameter("#DateResponded", SqlDbType.DateTime));
cmd.Parameters["#DateResponded"].Value = DateTime.Now; // Provide your value here
If you are calling a stored procedure, in addition to the NULL issue someone mentioned above, there is a type mismatch issue. For instance I was getting the "Expects parameter that is not supplied" error, when I was supplying the parameter.
After pulling my hair out for a while I found that the procedure had an input parameter of nvarchar(50) and I was passing a string that was longer than that. Apparently any kind of mismatch is reported the same as not providing (which in a way I suppose it is).
So you may want to double check data types and sizes on both ends.

returning int value from stored procedure and check it in asp.net code to validate login form

What is the true sequence to make this code run as I tried many time but I don't get a valid result
// the code of SQL stored procedure
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[login_proc] #username Varchar =50, #password varchar=50
as
Declare #user_name varchar , #pass_word varchar, #result varchar
Set #user_name = #username
Set #pass_word = #password
if EXISTS (select username , password from data where username= #user_name)
set #result= 1
else
set #result=0
return #result
and asp.net code is
SqlConnection conn = new SqlConnection ("Data Source=ANAGUIB-LAPNEW\\SQLEXPRESS;Initial Catalog=account;Integrated Security=True");
SqlCommand cmd = new SqlCommand("login_proc",conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramReturnValue = new SqlParameter();
paramReturnValue.ParameterName = "#result";
paramReturnValue.SqlDbType = SqlDbType.Int;
cmd.Parameters.Add(paramReturnValue);
cmd.Parameters["#result"].Direction = ParameterDirection.Output;
conn.Open();
cmd.Parameters.AddWithValue("#username", TextBox1.Text);
cmd.Parameters.AddWithValue("#password", TextBox2.Text);
cmd.ExecuteScalar();
string retunvalue = (string)cmd.Parameters["#result"].Value;
if (retunvalue == "1")
{
Response.Redirect("hello.aspx");
}
else
{
Response.Write("error");
}
conn.Close();
You're executing .ExecuteScalar() so you're expecting back a result set with a single row, single column from the stored procedure - but you're not selecting anything at the end of your stored proc!
You need to change your last line in the stored proc from
return #result
to
SELECT #result
and then it should work.
Add another parameter for the return value
ALTER PROC [dbo].[login_proc]
#username Varchar = 50,
#password Varchar = 50,
#result int OUTPUT
Examples can be viewd here.
Did you try this one;
Use the
return #result
and in c#
int resultID = Convert.ToInt32(cmd.ExecuteScalar());
Also remove next line
cmd.Parameters[""].value;
I'm unable to login any suggestions, both in case of stored procedure and codebehind can anyone provide solution to this,,,plz,,,

Stored Procedure Default Value

I'm a newbie when it comes to SQL. When creating a stored procedure with parameters as such:
#executed bit,
#failure bit,
#success bit,
#testID int,
#time float = 0,
#name varchar(200) = '',
#description varchar(200) = '',
#executionDateTime nvarchar(max) = '',
#message nvarchar(max) = ''
This is the correct form for default values in T-SQL? I have tried to use NULL instead of ''.
When I attempted to execute this procedure through C# I get an error referring to the fact that description is expected but not provided. When calling it like this:
cmd.Parameters["#description"].Value = result.Description;
result.Description is null. Should this not default to NULL (well '' in my case right now) in SQL?
Here's the calling command:
cmd.CommandText = "EXEC [dbo].insert_test_result #executed,
#failure, #success, #testID, #time, #name,
#description, #executionDateTime, #message;";
...
cmd.Parameters.Add("#description", SqlDbType.VarChar);
cmd.Parameters.Add("#executionDateTime", SqlDbType.VarChar);
cmd.Parameters.Add("#message", SqlDbType.VarChar);
cmd.Parameters["#name"].Value = result.Name;
cmd.Parameters["#description"].Value = result.Description;
...
try
{
connection.Open();
cmd.ExecuteNonQuery();
}
...
finally
{
connection.Close();
}
A better approach would be to change the CommandText to just the name of the SP, and the CommandType to StoredProcedure - then the parameters will work much more cleanly:
cmd.CommandText = "insert_test_result";
cmd.CommandType = CommandType.StoredProcedure;
This also allows simpler passing by name, rather than position.
In general, ADO.NET wants DBNull.Value, not null. I just use a handy method that loops over my args and replaces any nulls with DBNull.Value - as simple as (wrapped):
foreach (IDataParameter param in command.Parameters)
{
if (param.Value == null) param.Value = DBNull.Value;
}
However! Specifying a value with null is different to letting it assume the default value. If you want it to use the default, don't include the parameter in the command.
If you aren't using named parameters, MSSQL takes the parameters in the order received (by index). I think there's an option for this on the cmd object.
so your SQL should be more like
EXEC [dbo].insert_test_result
#executed = #executed,
#failure = #failure,
#success = #success,
#testID = #testID,
#time = #time,
#name = #name,
#description = #description,
#executionDateTime = #executionDateTime,
#message = #message;
cmd.CommandText = "insert_test_result";
cmd.Parameters.Add(new SQLParameter("#description", result.Description));
cmd.Parameters.Add(new SQLParameter("#message", result.Message));
try
{
connection.Open();
cmd.ExecuteNonQuery();
}
finally
{
connection.Close();
}

Categories