Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to run a query on a table but keep getting the error:

The parameterized query '(@TourName char(8000))UPDATE ToursTbl SET Availability = (Availa' expects the parameter '@TourName', which was not supplied.


I guessing its to do with how I've provided the @TourName parameter. Heres my code Any help??


for (int i = 0; i < bookedToursArray.Length; i++)
           {
               //command to update the number of available tours in the tours table
               SqlCommand cmd2 = new SqlCommand();
               cmd2.CommandText = "UPDATE ToursTbl SET Availability = (Availability-1) WHERE TourName = '@TourName'";
               cmd2.Parameters.Add("@TourName", SqlDbType.Char).Value = bookedToursArray[i];

               cmd2.Connection = conn;

               conn.Open();

               cmd2.ExecuteNonQuery();

               conn.Close();
           }


What I have tried:

The problem seems to be my syntax
Posted
Updated 20-May-17 9:43am
Comments
[no name] 20-May-17 15:37pm    
"The problem seems to be my syntax", you seem to be correct. Get rid of the single quotes and use AddWithValue instead.

1 solution

See the line
C#
cmd2.CommandText = "UPDATE ToursTbl SET Availability = (Availability-1) WHERE TourName = '@TourName'";
When using parameterized queries the single-quotes are added for you. That line should be
C#
cmd2.CommandText = "UPDATE ToursTbl SET Availability = (Availability-1) WHERE TourName = @TourName";
Your next problem is with
C#
cmd2.Parameters.Add("@TourName", SqlDbType.Char).Value = bookedToursArray[i];
Don't use Add - change it to
C#
cmd2.Parameters.AddWithValue("@TourName",bookedToursArray[i]);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900