Error: Procedure or function expects parameter which was not supplied

前端 未结 2 817
你的背包
你的背包 2021-01-27 13:28

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 man

相关标签:
2条回答
  • 2021-01-27 13:55

    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
    
    0 讨论(0)
  • 2021-01-27 14:00

    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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题