简体   繁体   中英

Error: Procedure or function expects parameter which was not supplied

Currently, my program in Visual Studio dynamically adds my data from my Repeater into my database.

Now I need to add the ID, my EventId and FormId, which I collected manually outside of the Repeater.

I need to add this in:

 sqlCmd.Parameters.Add("@EventId", SqlDbType.Int).Value = eventId;
 sqlCmd.Parameters.Add("@FormId", SqlDbType.Int).Value = formId;

However with how my code is setup, it gives an error when I add this code that says:

Additional information: Procedure or function 'spInsFormRegistrant' expects parameter '@EventId', which was not supplied.

Working code (with error code commented out):

 protected void BtnSubmit_Click(object sender, EventArgs e)
 {
            using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["Events2"].ConnectionString))
            {
                sqlConn.Open();

                using (SqlCommand sqlCmd = new SqlCommand())
                {

                   //sqlCmd.Parameters.Add("@EventId", SqlDbType.Int).Value = eventId;
                   //sqlCmd.Parameters.Add("@FormId", SqlDbType.Int).Value = formId;
                    foreach (RepeaterItem rpItem in RepeaterForm.Items)
                    {
                        Label lblDisplayName = rpItem.FindControl("lblDisplayName") as Label;
                        Label lblColumnName = rpItem.FindControl("lblColumnName") as Label;
                        TextBox txtColumnValue = rpItem.FindControl("txtColumnValue") as TextBox;

                        if (txtColumnValue != null)
                        {
                            sqlCmd.Connection = sqlConn;
                            sqlCmd.CommandType = CommandType.StoredProcedure;
                            sqlCmd.CommandText = "spInsFormRegistrant";
                            sqlCmd.Parameters.Clear();

                            sqlCmd.Parameters.Add("@ColumnName", SqlDbType.NVarChar).Value = lblColumnName.Text;
                            sqlCmd.Parameters.Add("@ColumnValue", SqlDbType.NVarChar).Value = txtColumnValue.Text;

                        sqlCmd.ExecuteNonQuery();
                        }
                    }
                }
            }
            PnlNone.Visible = false;
            PnlExist.Visible = false;
            PnlSuccess.Visible = true;
            PnlFail.Visible = false;
        }

So I just need to know where to add in @EventId and @FormId for this to function correctly. Each time I try it, it does not work. I must be missing something small for this to not produce an error. Or maybe it is an issue with my stored procedure...?

Stored Procedure

ALTER PROCEDURE [dbo].[spInsFormRegistrant]
    -- Add the parameters for the stored procedure here
     @EventId int,
     @FormId int,
     @ColumnName varchar(100),
    @ColumnValue varchar(100)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
     declare @Query nvarchar(4000)
    declare @ParmDefinition nvarchar(500);

    set @Query = 'INSERT into Registrant(DateCreated,EventId,FormId,'+ (@ColumnName) +') values (CURRENT_TIMESTAMP, @EventId, @FormId, @ColumnValue)'
    set @ParmDefinition = N'@ColumnValue varchar(100)'
    exec sp_executesql @Query, @ParmDefinition, @ColumnValue = @ColumnValue

END

You are not adding the parameter for eventID and for FormID, but you should try to use a different approach. Do not create everytime the parameters. You could create them just one time before entering the foreach loop and then, inside the loop change only their value

// This part never changes so, set it up just one time before the loop
sqlCmd.Connection = sqlConn;
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.CommandText = "spInsFormRegistrant";
sqlCmd.Parameters.Add("@EventId", SqlDbType.Int).Value = eventId;
sqlCmd.Parameters.Add("@FormId", SqlDbType.Int).Value = formId;

// These twos change inside the loop, so we don't need the value here
sqlCmd.Parameters.Add("@ColumnName", SqlDbType.NVarChar);
sqlCmd.Parameters.Add("@ColumnValue", SqlDbType.NVarChar);


foreach (RepeaterItem rpItem in RepeaterForm.Items)
{
    Label lblDisplayName = rpItem.FindControl("lblDisplayName") as Label;
    Label lblColumnName = rpItem.FindControl("lblColumnName") as Label;
    TextBox txtColumnValue = rpItem.FindControl("txtColumnValue") as TextBox;

    if (txtColumnValue != null)
    {
        sqlCmd.Parameters["@ColumnName"].Value = lblColumnName.Text;
        sqlCmd.Parameters["@ColumnValue"].Value = txtColumnValue.Text;
        sqlCmd.ExecuteNonQuery();
    }
}

Of course you don't need the call to Parameters.Clear

Then there is a problem in the way in which you pass the paramenters to the sp_executesql call inside the stored procedure. That system storedprocedure requires that you set the datatype for every parameter used in the query and an initialization list of these parameters.

You should write

...
-- Insert statements for procedure here
declare @Query nvarchar(4000)
declare @ParmDefinition nvarchar(500);

set @Query = 'INSERT into Registrant(DateCreated,EventId,FormId,'+ 
             (@ColumnName) +') values (CURRENT_TIMESTAMP, @EventId, @FormId, @ColumnValue)'
set @ParmDefinition = N'@ColumnValue varchar(100), @EventID int, @FormID int'
exec sp_executesql @Query, @ParmDefinition, 
                   @ColumnValue = @ColumnValue,
                   @EventID = @EventID,
                   @FormID = @FormID

You are clearing the parameters:

sqlCmd.Parameters.Clear();

You need to add them over and over:

foreach (RepeaterItem rpItem in RepeaterForm.Items)
{
    Label lblDisplayName = rpItem.FindControl("lblDisplayName") as Label;
    Label lblColumnName = rpItem.FindControl("lblColumnName") as Label;
    TextBox txtColumnValue = rpItem.FindControl("txtColumnValue") as TextBox;

    if (txtColumnValue != null)
    {
        sqlCmd.Connection = sqlConn;
        sqlCmd.CommandType = CommandType.StoredProcedure;
        sqlCmd.CommandText = "spInsFormRegistrant";
        sqlCmd.Parameters.Clear();  //that's fine, keep it

        //just put them in here
        sqlCmd.Parameters.Add("@EventId", SqlDbType.Int).Value = eventId;
        sqlCmd.Parameters.Add("@FormId", SqlDbType.Int).Value = formId;

        sqlCmd.Parameters.Add("@ColumnName", SqlDbType.NVarChar).Value = lblColumnName.Text;
        sqlCmd.Parameters.Add("@ColumnValue", SqlDbType.NVarChar).Value = txtColumnValue.Text;

    sqlCmd.ExecuteNonQuery();
    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM