Question

I am coming up with the error, "A parameter is missing. [ Parameter ordinal = 1 ]", in Web Matrix when I try to run the following SQL query:

if(IsPost)
{
var db = Database.Open("TheatreBooking");
var update = 
"INSERT INTO Customer ([Customer_First_Name],[Customer_Surname],[Customer_Add_Ln_1],
           [Customer_Add_Ln_2],[Customer_Add_Ln_3],[Customer_Add_Ln_4],[Customer_Postcode],
           [Customer_Tel],[Customer_Email]) VALUES
          (@customer_first_name,@customer_surname,@customer_add_ln_1,
           @customer_add_ln_2,@customer_add_ln_3,@customer_add_ln_4,
          @customer_postcode,@customer_telephone,@customer_email)";
db.Execute(update);
 }

It seems to be on the db.execute(update) line, however, I am unsure what my additional parameter should be.

Customer is my table, and it includes the capitalised headings (e.g. Customer_First_Name), and then the values I would like to insert are the non-capitalised (e.g. customer_first_name) which have been entering into a form:

<form action="" method ="post" >
        <p>First Name</p>
            <input type="text" name ="customer_first_name"> </input> </br>
            <p>Surname</p>
            <input type="text" name ="customer_surname"> </input> </br>
             <p>Address</p>
            <input type="text" name ="customer_add_ln_1"> </input> </br>
            <input type="text" name ="customer_add_ln_2"> </input> </br>
            <input type="text" name ="customer_add_ln_3"> </input> </br>
            <input type="text" name ="customer_add_ln_4"> </input> </br>
            <p>Postcode</p>
            <input type="text" name ="customer_postcode"> </input> </br>
            <p>Telephone</p>
            <input type="text" name ="customer_telephone"> </input> </br>
            <p>Email</p>
            <input type="text" name ="customer_email"> </input> </br>
            <input type="Submit" name ="Confirm"> </input> 
        </form>

The columns are as follows:

enter image description here Thanks.

Was it helpful?

Solution

You can use something like this:

  using (SqlConnection connection = new SqlConnection("TheatreBooking"))// I guess "TheatreBooking" is your connectionString 
  {
     connection.Open(); 
      using (SqlCommand command = connection.CreateCommand()) 
      { 
         command.CommandText = "INSERT INTO Customer ([Customer_First_Name],[Customer_Surname],
                            [Customer_Add_Ln_1],[Customer_Add_Ln_2],[Customer_Add_Ln_3],
                            [Customer_Add_Ln_4],[Customer_Postcode], [Customer_Tel],[Customer_Email]) VALUES
                            (@customer_first_name,@customer_surname,@customer_add_ln_1,
                             @customer_add_ln_2,@customer_add_ln_3,@customer_add_ln_4,
                             @customer_postcode,@customer_telephone,@customer_email)"; 

        command.Parameters.AddWithValue("@customer_first_name",customerfirstname));  
       command.Parameters.AddWithValue("@customer_surname",customersurname));
        ....// you do the same for the rest (6)as the above line
        command.ExecuteNonQuery(); 
      }
   }

If you are missing a library: add using System.Data.SqlClient;

I hope it will help you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top