[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 498
  • Last Modified:

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

0
Simon Cripps
Asked:
Simon Cripps
1 Solution
 
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now