I have created a stored procedure for inserting values in a table. This is the stored procedure:
CREATE PROCEDURE [dbo].[insertproc]
@CollectionPersonName varchar(50),
@Address varchar(1500),
@ContactNo varchar(50),
@CreatedBy int,
@CreatedOn datetime
AS
BEGIN
SET NOCOUNT ON;
-- Insert statements for procedure here
insert into CollectionPersonDetails(CollectionPersonName, Address, ContactNo, CreatedBy, CreatedOn)
values(@CollectionPersonName, @Address, @ContactNo, @CreatedBy, @CreatedOn);
END
Below is the function that I have written for passing parameters and executing the query in the stored procedure:
public static int SaveDataProcedure(tblCollectionPersonObj oSaveObj, int iUserId)
{
string query = "insertproc";
OdbcConnection conn = new OdbcConnection(Connect.ConnectionString());
OdbcCommand cmd = new OdbcCommand("insertproc",conn);
cmd.CommandText = query;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CollectionPersonName", oSaveObj.SCollectionPersonName);
cmd.Parameters.AddWithValue("@Address", oSaveObj.SAddress);
cmd.Parameters.AddWithValue("@ContactNo", oSaveObj.SPhoneNo);
cmd.Parameters.AddWithValue("@CreatedBy", iUserId);
cmd.Parameters.AddWithValue("@CreatedOn", CommonData.GetDateForDatabase(DateTime.Now));
cmd.Connection.Open();
int i= cmd.ExecuteNonQuery();
cmd.Connection.Close();
return i;
}
I am getting an error which says stored procedure expects the parameter @collectionPersonName
which was not supplied. I have tried using parameters.add
function as well, but the same error was shown.
I can see the values coming when I debug through the code but when the execution comes to ExecuteNonQuery()
that's the point where I get the error.The mistake might be very silly but I haven't been able to find it.
Kindly help.
AddWithValue
method will not add parameter, if value is null.
Check if oSaveObj.SCollectionPersonName
is null and use DBNull.Value
in this case
cmd.Parameters.AddWithValue("@CollectionPersonName", oSaveObj.SCollectionPersonNamу ?? DBNull.Value);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.