jueves, 5 de mayo de 2016

C# - validar tipo de datos de un DataTable

*Por  ejemplo para carga de datos desde un EXCEL.

 private bool ValidaTipoDatoEntrada(DataTable dt,  ref string msjValidaEntrada)
        {
            //97 : CargaDeudaHonorarios 26 : CargaDeudaSeguroCliente

            var columnaError = string.Empty;
            try
            {
                foreach (DataRow row in dt.Rows)
                {
                    if (!string.IsNullOrEmpty(row[0].ToString()))
                    {
                        columnaError = @"Patente Rol - valor: " + row[0];
                        var patenteRol = row[0].ToString();
                    }

                    if (!string.IsNullOrEmpty(row[1].ToString()))
                    {
                        columnaError = @"Operación - valor: " + row[1];
                        var operacion = Convert.ToDecimal(row[1]);
                    }

                    if (!string.IsNullOrEmpty(row[2].ToString()))
                    {
                        columnaError = @"Código Material - valor: " + row[2];
                        var codMaterial = Convert.ToInt32(row[2]);
                    }

                    if (!string.IsNullOrEmpty(row[3].ToString()))
                    {
                        columnaError = @"Fecha - valor: " + row[3];
                        var fecha = Convert.ToDateTime(row[3]);
                    }

                    if (!string.IsNullOrEmpty(row[4].ToString()))
                    {
                        columnaError = @"Monto - valor: " + row[4];
                        var monto = Convert.ToSingle(row[4]);
                    }

                    if (!string.IsNullOrEmpty(row[5].ToString()))
                    {
                        columnaError = @"Tipo Documento - valor: " + row[5];
                        var tipoDocumento = Convert.ToInt32(row[5]);
                    }

                    if (!string.IsNullOrEmpty(row[6].ToString()))
                    {
                        columnaError = @"Numero Documento - valor: " + row[6];
                        var numDocum = Convert.ToDecimal(row[6]);
                    }

                    if (!string.IsNullOrEmpty(row[7].ToString()))
                    {
                        columnaError = @"Rut Beneficiario - valor: " + row[7];
                        var rutBeneficiario = Convert.ToInt32(row[7]);
                    }

                    if (!string.IsNullOrEmpty(row[8].ToString()))
                    {
                        columnaError = @"Nombre Beneficiario - valor: " + row[8];
                        var nombreBeneficiario = row[8].ToString();
                    }

                    if (!string.IsNullOrEmpty(row[9].ToString()))
                    {
                        columnaError = @"Es Docto Electronico - valor: " + row[9];
                        var esDoctoElectronico = row[9].ToString();
                    }

                    if (!string.IsNullOrEmpty(row[10].ToString()))
                    {
                        columnaError = @"Glosa - valor: " + row[10];
                        var glosa = row[10].ToString();
                    }

                    if (!string.IsNullOrEmpty(row[11].ToString()))
                    {
                        columnaError = @"Concepto - valor: " + row[11];
                        var concepto = Convert.ToInt32(row[11]);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                msjValidaEntrada = "El formato de entrada no es correcto para la columna: " + columnaError;
                return false;
            }
        }




/*Llamada del Método*/
  if (ValidaTipoDatoEntrada(dt, tipoDeuda, ref msjValidaEntrada))
  {

  }
  else
  {
      fupArchivo.JSProperties["cpError"] = "true";
      fupArchivo.JSProperties["cpMensaje"] = "alert('" + msjValidaEntrada + "')";
    }


No hay comentarios:

Publicar un comentario