2012-02-29 14 views
5

Hola, estoy tratando de escribir algunos valores en mi SQL a través del C# WinForm que estoy construyendo. Me aparece un error que no puedo resolver.excepción sql, falta la consulta parametrizada

Estoy llamando a esto:

SQL.insertIntoChildTable(Convert.ToInt32(child_IDTextBox.Text), 
         child_FirstNameTextBox.Text, 
         child_LastNameTextBox.Text, 
         Convert.ToInt32(parent1IDTextBox.Text), 
         Convert.ToInt32(parent2IDTextBox.Text), 
         birthdayDateTimePicker.Value.ToShortDateString(), 
         age.ToString(), 
         null, null, null, null, null, 
         child_disciplineTextBox.Text, child_NotesTextBox.Text); 

De esta:

public static void insertIntoChildTable(int childID, string firstName, string lastName, int parent1ID, int parent2ID, string birthdate, string age, string lastCheckedInTime, string lastCheckedInBy, string lastCheckOutTime, string lastCheckedOutBy, string ageGroup, string disciplineNotes, string otherNotes) 
{ 
    try 
    { 
     using (SqlConnection connection = new SqlConnection(Global.connectionString)) 
     using (SqlCommand command = connection.CreateCommand()) 
     { 
      command.CommandText = "INSERT INTO ProjectList (ChildID, FirstName, LastName, Parent1ID, Parent2ID, Birthdate, Age, LastCheckedInTime, LastCheckedInBy, LastCheckOutTime, LastCheckedOutBy, AgeGroup, DisciplineNotes, OtherNotes) VALUES (@childID, @firstName, @lastName, @parent1ID, @parent2ID, @birthdate, @age, @lastCheckedInTime, @lastCheckedInBy, @lastCheckOutTime, @lastCheckedOutBy, @ageGroup, @disciplineNotes, @otherNotes)"; 

      command.Parameters.AddWithValue("@childID", childID); 
      command.Parameters.AddWithValue("@firstName", firstName); 
      command.Parameters.AddWithValue("@lastName", lastName); 
      command.Parameters.AddWithValue("@parent1ID", parent1ID); 
      command.Parameters.AddWithValue("@parent2ID", parent2ID); 
      command.Parameters.AddWithValue("@birthdate", birthdate); 
      command.Parameters.AddWithValue("@age", age); 
      command.Parameters.AddWithValue("@lastCheckedInTime", lastCheckedInTime); 
      command.Parameters.AddWithValue("@lastCheckedInBy", lastCheckedInBy); 
      command.Parameters.AddWithValue("@lastCheckOutTime", lastCheckOutTime); 
      command.Parameters.AddWithValue("@lastCheckedOutBy", lastCheckedOutBy); 
      command.Parameters.AddWithValue("@ageGroup", ageGroup); 
      command.Parameters.AddWithValue("@disciplineNotes", disciplineNotes); 
      command.Parameters.AddWithValue("@otherNotes", otherNotes); 

      connection.Open(); 
      command.ExecuteNonQuery(); 
     } 
    } 
    catch (SqlException ex) 
    { 
     Console.WriteLine(ex.Message); 
    } 
} 

Y conseguir esto:

Una primera excepción del tipo 'System.Data.SqlClient.SqlException 'se produjo en System.Data.dll
La consulta parametrizada' (@childID int, @ firstName nvarchar (4), @lastName nvarchar (5), @ pare 'espera el parámetro ' @lastCheckedInTime ', que no se suministró.

¿Alguna idea?

+1

Creo que desea especificar el comando.CommandType = CommandType.Text para que sea explícito (no necesariamente la causa del problema, sino una buena práctica). – kaj

Respuesta

11

Supongo que es null. Los parámetros que son null no se agregan. Necesita ser DBNull.Value. Puede hacer esto agregando ?? DBNull.Value a cada parámetro. Estúpido, lo sé:

command.Parameters.AddWithValue("@lastCheckInTime", 
     lastCheckInTime ?? DBNull.Value); 

para cada parámetro; o bucle sobre ellos después, la fijación de cualquier null a DBNull.Value:

foreach(var param in command.Parameters) { 
    if(param.Value == null) param.Value = DBNull.Value; 
} 

como una cosa lado - que tienen todos como los string suena muy poco probable; debe ser DateTime o DateTime?, ¿no?

+0

yup 'null' funciona de forma diferente en SQL. Gracias. y estoy convirtiendo todo en texto porque solo intento poner esto en marcha. Lanzar los valores al tipo correcto vendrá, solo necesito más experiencia. gracias de nuevo. – ikathegreat

Cuestiones relacionadas