Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

Im trying to insert values in a database but i get the following error:"No value given for one or more required parameters"

//Below is my code

C#
protected void btnAddCourse_Click(object sender, EventArgs e)
   {
       //try
       //{
           string connString = ConfigurationManager.ConnectionStrings["courses"].ConnectionString;
           // set up and open connection
           dbConn = new OleDbConnection(connString);
           dbConn.Open();
           string updateSql = "UPDATE Courses " + "SET coursePrice = @price, StartDate=@date, Duration=@duration, Venue=@venue " + "WHERE Venue = @courseNo";
           dbCmd = new OleDbCommand(updateSql, dbConn);

           dbCmd.Parameters.Add("@price", OleDbType.VarChar, 20, "coursePrice");
           dbCmd.Parameters.Add("@date", OleDbType.VarChar, 20, " StartDate");
           dbCmd.Parameters.Add("@duration", OleDbType.VarChar, 10, "Duration");
           dbCmd.Parameters.Add("@venue", OleDbType.VarChar, 20, " Venue");

           dbCmd.Parameters["@price"].Value = txtPrice.Text;
           dbCmd.Parameters["@date"].Value = txtDate.Text;

           dbCmd.Parameters["@duration"].Value = txtDuration.Text;
           dbCmd.Parameters["@venue"].Value = txtVenue.Text;

           dbCmd.ExecuteNonQuery();
           MessageBox.Show("Added");

       //}
       //catch(OleDbException ex)
       //{
       //    MessageBox.Show(ex.Message);
       //}

   }
Posted

1 solution

string updateSql = "UPDATE Courses " + "SET coursePrice = @price, StartDate=@date, Duration=@duration, Venue=@venue " + "WHERE Venue = @courseNo";
Note the final parameter: "@courseNo".
Where do you set it? You set all the others!

BTW: An UPDATE command does not add any records - it modifies an existing one. So you are either using the wrong SQL command and want INSERT instead, or your method name is wrong, as well as your message box!
 
Share this answer
 
Comments
Anele Ngqandu 16-Oct-10 11:33am    
Thanks it worX
raju melveetilpurayil 16-Oct-10 19:36pm    
Anele.Ngqandu if the answer help you, don't forget rate 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