I am trying insert some entries in the database using a stored procedure which is called by a C# method as shown below:
public int addProfile(UserProfile NewUserPro)
{
DataSet ds = ExecuteStoredProcedure("AddNewPro", new SqlParameter[]{
new SqlParameter("@UserID", NewUserPro.UsrId),
new SqlParameter("@Type", NewUserPro.Type),
new SqlParameter("@Phone", NewUserPro.Phone),
new SqlParameter("@Name", NewUserPro.Name),
new SqlParameter("@FatherName", NewUserPro.FatherName),
new SqlParameter("@GroupID", NewUserPro.GroupID),
new SqlParameter("@IsPublic", 0)});
return int.Parse(ds.Tables[0].Rows[0][0].ToString());
}
ExecuteStoredProcedure()
is a C# method created by me. It executes specified stored procedure and returns the output as a DataSet
. This is the definition of that function:
public DataSet ExecuteStoredProcedure(string ProcedureName, SqlParameter[] Parameters)
{
try
{
using (SqlCommand com = new SqlCommand("[dbo]." + ProcedureName))
{
com.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter sp in Parameters)
com.Parameters.Add(sp);
return SelectDataSet(com);
}
}
catch (Exception e)
{
throw e;
}
}
As you can see in addProfile()
method I am already providing @IsPublic
parameter, but it's still throwing following exception:
Procedure or function 'AddNewPro' expects parameter '@IsPublic', which was not supplied.
@IsPublic
parameter is an int
parameter that saves value to [IsPublic]
field which is an int
field in a table with null
allowed.
UPDATE 1
I am including the input parameters of the stored procedure. I think that will be enough? If you want I can include complete stored procedure but it's a lengthy code and due to some confidentiality reasons I have to cut some lines.
ALTER PROCEDURE [dbo].[AddNewPro]
@UserID int,
@Name NVARCHAR(MAX),
@Type int,
@Phone VARCHAR(MAX),
@GroupID int,
@FatherName VARCHAR(MAX),
@IsPublic int
AS
UPDATE 2
Ok I found a little hint to the solution. When I put the breakpoint on the first line of ExecuteStoredProcedure
, I found that its getting the value of @IsPublic
as null. But I provided the value while adding the parameters. Why this is happening?