error can not transform a DBNULL object

1

I'm doing an application where I fill gridview with a query SQL , but when wanting to register more information in gridview I get the following error:

  

You can not transform a DBNULL object into other types.

My code is as follows:

if (DialogResult.OK == miBskReten.ShowDialog()) 
{ 
    codRet = miBskReten.sCodigo; 
    detRet = miBskReten.sDetalle; 
    idRtn = miBskReten.iIdRtn; 
    PrcRet = miBskReten.dPorcen; 
    this.ultraGrid1.ActiveRow.Cells["Codigo"].Value = codRet; 
    this.ultraGrid1.ActiveRow.Cells["Detalle"].Value = detRet; 
    this.ultraGrid1.ActiveRow.Cells["idRetencionIVARenta"].Value =Convert.ToString(idRtn); 
    this.ultraGrid1.ActiveRow.Cells["Porcentaje"].Value =Convert.ToString(PrcRet); 
    this.ultraGrid1.ActiveRow.Cells["Valor"].Value =Convert.ToString(PrcRet*dBase); 
}

In this part of recording the information in the grid I can not tell the grid to register a new data.

    
asked by Edison 25.07.2017 в 08:06
source

1 answer

0

As far as I can see in the code you show, codRet , or detRet , or idRtn or PrcRet is DBNull and when you try to do the Convert you get the exception.

To solve it you only need to check that they are not DBNull before trying to make any conversion.

For this you only need to do something like this:

if (DialogResult.OK == miBskReten.ShowDialog())
{
    codRet = miBskReten.sCodigo;
    detRet = miBskReten.sDetalle;
    idRtn = miBskReten.iIdRtn;
    PrcRet = miBskReten.dPorcen;
    this.ultraGrid1.ActiveRow.Cells["Codigo"].Value = codRet;
    this.ultraGrid1.ActiveRow.Cells["Detalle"].Value = detRet;
    this.ultraGrid1.ActiveRow.Cells["idRetencionIVARenta"].Value = Convert.ToString(idRtn is DBNull ? 0 : idRtn);
    this.ultraGrid1.ActiveRow.Cells["Porcentaje"].Value = Convert.ToString(PrcRet is DBNull ? 0 : PrcRet);
    this.ultraGrid1.ActiveRow.Cells["Valor"].Value = Convert.ToString((PrcRet is DBNull ? 0 : PrcRet) * (dBase is DBNull ? 0 : PrcRet));
}
    
answered by 26.07.2017 в 09:29