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