Link to home
Start Free TrialLog in
Avatar of Simon Cripps
Simon CrippsFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

Avatar of rajeeshmca
rajeeshmca
Flag of India image

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)
                   {
                   }
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;

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.

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.
Avatar of Simon Cripps

ASKER

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