0

Hi Im trying to save a null value for a datetime (fechaNac) but i cant do it .is it possible to do it using my generic code in asp?

public int add(Cliente dato)
        {
            string cmdText = "insert into clientes values ( @nombre, @apellido,@telefono,@celular,@oldtelefono, @oldcelular, @dni , @fechaNac)";
            Dictionary<string, Object> parametros = new Dictionary<string, Object>();
            parametros.Add("@nombre", dato.Nombre);
            parametros.Add("@apellido", dato.Apellido);
            parametros.Add("@telefono", dato.Telefono);
            parametros.Add("@celular", dato.Celular);
            parametros.Add("@oldtelefono", dato.OldTelefono);
            parametros.Add("@oldcelular", dato.OldCelular);
            parametros.Add("@dni", dato.Dni);
            parametros.Add("@fechaNac", dato.FechaNac);
            try
            {
                return this.setData(cmdText, parametros);
            }
            catch (Exception ex)
            {
                throw new Exception("No se pudo insertar el usuario en la base de datos", ex);
            }
        }


        public int setData(string cmdText, Dictionary<string, Object> listaParametros)
    {
        int res;
        using (SqlCommand cmd = new SqlCommand(cmdText, cn)) {
            foreach (KeyValuePair<string, Object> parametro in listaParametros) {
                cmd.Parameters.AddWithValue(parametro.Key, parametro.Value);
            }
            Conectar();
            try {
                res = cmd.ExecuteNonQuery();
            } catch (Exception ex) {
                throw new Exception("No se pudo insertar el dato", ex);
            } finally {
                Desconectar();
            }
        }
        return res;
    }

thanks in advance for your help! Yanina

ryekayo
  • 2,341
  • 3
  • 23
  • 51
user3767613
  • 123
  • 1
  • 1
  • 11

1 Answers1

0

Yes, but you need to coalesce it to DBNull.Value first.

cmd.Parameters.AddWithValue(parametro.Key, parametro.Value ?? DBNull.Value);
Jon Tirjan
  • 3,556
  • 2
  • 16
  • 24
  • thanks for the answer , it doesnt work, this is the sql exception i got with that code , "The column name or the specified values ​​do not correspond to the definition of the table." – user3767613 Apr 09 '15 at 02:45
  • Sounds like there are other, unrelated problems with the script. Try to resolve these in SSMS first, then come back to the app. Also, it's good practice to specify the columns you're inserting into - `insert into clientes (columnName1, columnName2, etc) values...` – Jon Tirjan Apr 09 '15 at 02:50
  • I debug the application , the only parameter that assigned null is fechaNac . I cant find what i am doing wrong – user3767613 Apr 09 '15 at 02:52
  • If fechaNac isnt null then it works fine , in SSMS works fine too – user3767613 Apr 09 '15 at 03:00
  • Please specify the columns explicitly in your `INSERT` statement. It might also help to add the table schema to the question. – Jon Tirjan Apr 09 '15 at 03:09
  • I think Jon is on the right track. It is likely just that you need to cast the value correctly. See examples [here](http://stackoverflow.com/a/13451148/685760) and [here](http://stackoverflow.com/a/863380/685760) – Mr Moose Apr 09 '15 at 03:20
  • i specify the columns in the insert statement and it works ... thanks so much – user3767613 Apr 09 '15 at 03:29