How to assign NULL value to fields in a table in code

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for PostgreSQL
Post Reply
JORGEMAL
Posts: 171
Joined: Thu 03 Jul 2008 23:55

How to assign NULL value to fields in a table in code

Post by JORGEMAL » Thu 21 Aug 2014 19:16

How can I assign NULL values to the following fields in the code below:
dfe_fraccion, dfe_unidad_comercial, dfe_unidad_tarifa and dfe_unidad_cove. Please note that 2 fields are string and 2 are integer.

Code: Select all

public void Inserta(ref String strMensaje, String strOrigen)
{
    //Definicion el Query
    StringBuilder strQuery = new StringBuilder();
    strQuery.Append("INSERT INTO detalle_facturas_exportacion (");
    strQuery.Append("dfe_id_factura, ");
    strQuery.Append("dfe_id_parte, ");
    strQuery.Append("dfe_cantidad, ");
    strQuery.Append("dfe_precio_unitario, ");
    strQuery.Append("dfe_fraccion, ");
    strQuery.Append("dfe_unidad_comercial, ");
    strQuery.Append("dfe_unidad_tarifa, ");
    strQuery.Append("dfe_factor_conversion, ");
    strQuery.Append("dfe_unidad_cove, ");
    strQuery.Append("dfe_factor_cove, ");
    strQuery.Append("dfe_numero_orden "); ;
    strQuery.Append(") VALUES (");
    strQuery.Append(":prmIdFactura, ");
    strQuery.Append(":prmIdParte, ");
    strQuery.Append(":prmCantidad, ");
    strQuery.Append(":prmPrecioUnitario, ");
    strQuery.Append(":prmFraccion, ");
    strQuery.Append(":prmUnidadComercial, ");
    strQuery.Append(":prmUnidadTarifa, ");
    strQuery.Append(":prmFactorConversion, ");
    strQuery.Append(":prmUnidadCove, ");
    strQuery.Append(":prmFactorCove, ");
    strQuery.Append(":prmNumeroOrden ) ");

    //Definicion de la conexion.
    String strConnAux = Herramientas.Herramientas.ObtenerStringConexion();
    String strConn = System.Configuration.ConfigurationManager.ConnectionStrings[strConnAux].ToString();
    PgSqlConnection pgConn = new PgSqlConnection(strConn);
    PgSqlCommand pgCmd = new PgSqlCommand(strQuery.ToString(), pgConn);
    pgCmd.Parameters.Add("prmIdFactura", PgSqlType.Int).Value = this.IdFactura;
    pgCmd.Parameters.Add("prmIdParte", PgSqlType.Int).Value = this.IdParte;
    pgCmd.Parameters.Add("prmCantidad", PgSqlType.Numeric).Value = this.Cantidad;
    pgCmd.Parameters.Add("prmPrecioUnitario", PgSqlType.Numeric).Value = this.PrecioUnitario;
    pgCmd.Parameters.Add("prmFraccion", PgSqlType.VarChar).Value = this.Fraccion;
    pgCmd.Parameters.Add("prmUnidadComercial", PgSqlType.Int).Value = this.UnidadComercial;
    pgCmd.Parameters.Add("prmUnidadTarifa", PgSqlType.Int).Value = this.UnidadTarifa;
    pgCmd.Parameters.Add("prmFactorConversion", PgSqlType.Numeric).Value = this.FactorConversion;
    pgCmd.Parameters.Add(":prmUnidadCove", PgSqlType.VarChar).Value = this.UnidadCove;
    pgCmd.Parameters.Add(":prmFactorCove", PgSqlType.Numeric).Value = this.FactorCove;
    pgCmd.Parameters.Add(":prmNumeroOrden", PgSqlType.Int).Value = this.NoOrden;


    pgConn.Open();
    pgCmd.ExecuteNonQuery();
    pgConn.Close();

Pinturiccio
Devart Team
Posts: 2420
Joined: Wed 02 Nov 2011 09:44

Re: How to assign NULL value to fields in a table in code

Post by Pinturiccio » Fri 22 Aug 2014 14:40

Use the following code to insert a NULL value into the columns you listed:

Code: Select all

pgCmd.Parameters.Add("prmFraccion", PgSqlType.VarChar).Value = DBNull.Value;
pgCmd.Parameters.Add("prmUnidadComercial", PgSqlType.Int).Value = DBNull.Value;
pgCmd.Parameters.Add("prmUnidadTarifa", PgSqlType.Int).Value = DBNull.Value;
pgCmd.Parameters.Add(":prmUnidadCove", PgSqlType.VarChar).Value = DBNull.Value;

Post Reply