0

I have an error in my code. It gives me an OleDbException (No value given for one or more required parameter), hope someone can show the the way.

here is part of my code:

public void buttonEditar_Click(object sender, EventArgs e)
{
   OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Gera\Documents\Visual Studio 2012\Projects\RichyAutoCenter\RichyAutoCenter\bin\Debug\Camionetas.accdb;");

   string query = "UPDATE [Camionetas] SET VIN = ?, Marca = ?, Modelo = ?, Año = ?, Rentada = ?, FechaRenta = ?, FechaEntrega = ?, DiasAtrasados = ?, FechaAceite = ?, FechaCambio = ?, Obervaciones = ?, PersonaRentada = ? WHERE ID = ?";

   int ID = int.Parse(numero);
   string Vin = textBoxVin.Text;
   string Marca = textBoxMarca.Text;
   string Modelo = textBoxModelo.Text;
   string Ano = textBoxAno.Text;
   Boolean Rentada = checkBoxRentada.Checked;
   string FechaRenta = dateTimePickerFechaRenta.Value.ToShortDateString();
   string FechaEntrega = dateTimePickerFechaEntrega.Value.ToShortDateString();
   int DiasAtrasados = int.Parse(textBoxDiasAtrasados.Text);
   string FechaAceite = dateTimePickerFechaAceite.Value.ToShortDateString();
   string FechaCambio = dateTimePickerFechaCambio.Value.ToShortDateString();
   string Observaciones = textBoxObservaciones.Text;
   string PersonaRentada = comboBoxPersonaRenta.Text;

   OleDbCommand command = new OleDbCommand(query, connection);
   command.CommandType = CommandType.Text;

   command.Parameters.AddWithValue("?",Vin);
   command.Parameters.AddWithValue("?",Marca);
   command.Parameters.AddWithValue("?",Modelo);
   command.Parameters.AddWithValue("?",Ano);
   command.Parameters.AddWithValue("?",Rentada);
   command.Parameters.AddWithValue("?",FechaRenta);
   command.Parameters.AddWithValue("?",FechaEntrega);
   command.Parameters.AddWithValue("?",DiasAtrasados);
   command.Parameters.AddWithValue("?",FechaAceite);
   command.Parameters.AddWithValue("?",FechaCambio);
   command.Parameters.AddWithValue("?",Observaciones);
   command.Parameters.AddWithValue("?",PersonaRentada);
   command.Parameters.AddWithValue("?",ID);

   connection.Open();
   command.ExecuteNonQuery(); // No value given for one or more required parameters error
   connection.Close();
}
Brian
  • 5,069
  • 7
  • 37
  • 47

1 Answers1

0

replace all yours command.Parameters.AddWithValue("?",ID); with general: command.Parameters.Add(ID);

or use different names instead of ?, check out examples at http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx

Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57