I have a parameterized query and it was working fine, but when I delete de DB and create it again, with the same values and everything, it throws an exception that says it cannot insert value NULL with the value sexo, but all the values are assigned, here it’s the code:
try{
var cmdPersona_Log = new SqlCommand();
cmdPersona_Log.Parameters.Clear();
cmdPersona_Log.Connection = mySqlConnection;
cmdPersona_Log.CommandType = CommandType.Text;
cmdPersona_Log.CommandText = @"INSERT INTO [Tomin].[TominRH].[Persona_Log] "
+ "([Id_Action],[Id_User],[Id_Date],[Id_Entidad],[Nombre],[Paterno],[Materno],[Sexo],[Id_Nacionalidad])"
+ " Values (1, 'Admin', @fecha, @id_entidad, @nombre, @paterno, @materno, @sexo, 52)";
cmdPersona_Log.Parameters.AddWithValue("@fecha", DateTime.Now);
cmdPersona_Log.Parameters.AddWithValue("@id_entidad", dbRow["CUENTA"].ToString().Trim());
cmdPersona_Log.Parameters.AddWithValue("@nombre", nombre ?? string.Empty);
cmdPersona_Log.Parameters.AddWithValue("@paterno", paterno ?? string.Empty);
cmdPersona_Log.Parameters.AddWithValue("@materno", materno ?? string.Empty);
cmdPersona_Log.Parameters.AddWithValue("@sexo", 1);
cmdPersona_Log.ExecuteNonQuery();
}
catch(Exception e)
{
MessageBox.Show(dbRow["CUENTA"] + " Persona_log " + e.ToString());
}
I’ve checked the DB and it doesn’t seem to be the problem, any sugestion??
You may be running into a case where
AddWithValueisn’t inferring your parameter type ofbitproperly. Usetrue/falseinstead of1/0: