issue with Update to SQL using AddWithValue in C#

Hi,
newly converting over to C# from VB and having an issue with writing an update using an SQL stored procedure. I am getting the follwing issue:
System.Data.SqlClient.SqlException: Procedure or function 'SPAWCampaignUpdate' expects parameter '@CampaignID', which was not supplied.
I have stepperd trought the execution of the programme and all appears OK and all appears OK.
The command text for SQLCmd  is as expected "SPAWCampaignUpdate"
There are 7 parameters supplied and one of them is called @CampaignID with the value I expected supplied by campaign.id.
in the stored procedure the CampaignID is defined as int and in the mapped in the table to a field defined as an integer as well. I have tried with both .AddWithValue("CampaignID", and .AddWithValue("@CampaignID"
Any suggestions (cade attached below)

SqlCommand SQLCmd = new SqlCommand("SPAWCampaignUpdate", con);
               SQLCmd.Parameters.AddWithValue("CampaignID", campaign.id);
               SQLCmd.Parameters.AddWithValue("campaignname", campaign.name);
               SQLCmd.Parameters.AddWithValue("campaignstatus", campaign.status);
               SQLCmd.Parameters.AddWithValue("campaignservingStatus", campaign.servingStatus);
               SQLCmd.Parameters.AddWithValue("campaignstartDate", campaign.startDate);
               SQLCmd.Parameters.AddWithValue("campaignendDate", campaign.endDate);
               SQLCmd.Parameters.AddWithValue("campaignbudget", System.Convert.ToString(campaign.budget));
                   try
                    {
                        SQLCmd.Connection.Open();
                        SqlDataReader UpdateRecs;
                        UpdateRecs = SQLCmd.ExecuteReader();
                        SQLCmd.Connection.Close();
                        SQLCmd.Connection.Dispose();
                    }
                   catch (Exception ex)
                   {
                   }

The Stored Procedure is
ALTER PROCEDURE dbo.SPAWCampaignUpdate
	@CampaignID int,
	@campaignname nvarchar (100),
	@campaignstatus nvarchar (10),
	@campaignservingStatus nvarchar (10),
	@campaignstartDate date,
	@campaignendDate date,
	@campaignbudget int
	
AS UPDATE AWCampaign
	SET AWCname = @campaignname, AWCstatus = @campaignstatus, AWCServingStatus = @campaignservingStatus, AWCStartDate = @campaignstartDate, AWCEndDate = @campaignendDate, AWCBudget = @campaignbudget
WHERE (AWCCampaignID = @CampaignID)
	RETURN

Open in new window

Simon CrippsOwnerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rajeeshmcaCommented:
hi Crippsy,

You have missed out the @ symbol for the parameters....

SqlCommand SQLCmd = new SqlCommand("SPAWCampaignUpdate", con);
               SQLCmd.Parameters.AddWithValue("@CampaignID", campaign.id);
               SQLCmd.Parameters.AddWithValue("@campaignname", campaign.name);
               SQLCmd.Parameters.AddWithValue("@campaignstatus", campaign.status);
               SQLCmd.Parameters.AddWithValue("@campaignservingStatus", campaign.servingStatus);
               SQLCmd.Parameters.AddWithValue("@campaignstartDate", campaign.startDate);
               SQLCmd.Parameters.AddWithValue("@campaignendDate", campaign.endDate);
               SQLCmd.Parameters.AddWithValue("@campaignbudget", System.Convert.ToString(campaign.budget));
                   try
                    {
                        SQLCmd.Connection.Open();
                        SqlDataReader UpdateRecs;
                        UpdateRecs = SQLCmd.ExecuteReader();
                        SQLCmd.Connection.Close();
                        SQLCmd.Connection.Dispose();
                    }
                   catch (Exception ex)
                   {
                   }
0
Carl TawnSystems and Integration DeveloperCommented:
You need to set the CommandType property of your SqlCommand to CommandType.StoredProcedure.

You may also want to move you connection disposal code into a "finally" block, as you have it at the moment, the connection won't be closed if an exception is thrown.
SQLCmd.CommandType = System.Data.CommandType.StoredProcedure;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lazyberezovskyCommented:
Add @ before parameters names.
And its better to have strong typing and have it all in one line of code:
SqlCommand.Parameters.Add("@CampaignID", SqlDbType.Int).Value = campaign.id;

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

lazyberezovskyCommented:
carl_tawn, there is no error is CommandType set to Text. Only difference is that SQL server will need to parse query and build execution plan.

0
Carl TawnSystems and Integration DeveloperCommented:
Erm, you might want to try that again.

If you specify the command type as Text then you need to apply placeholders for the parameters in the command, since the parser will do a straight substitution requiring a call to "sp_executesql" so it would be:

SqlCommand SQLCmd = new SqlCommand("SPAWCampaignUpdate @CampaignID, @campaignname, campaignname, @campaignstatus, @campaignservingstatus, @campaignstartDate, @campaignenddate, campaignbudget", con);

If you specify the command type as Stored Procedure then you don't need to specify the parameters in the command text and the sql engine can directly execute the procedure.
0
Simon CrippsOwnerAuthor Commented:
Sport on, missed the setting as type stored procedure. Also thanks for the pointer on closing the data connectio, I have now resolved that as well.
Many thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.