Estoy haciendo la carga de un archivo excel a un BD oracle, este es mi codigo, el problema que tengo es que cuando se ejecuta en un servidor IIS no se cargan todos los registros del archivo en la BD, al parecer (no estoy seguro)cuando entra en el catch es cuando se detiene y ya no carga mas registros, como puedo de otra manera obtener los errores de formateo sin usar el try-catch, cabe señalar que cuando ejecuto el proyecto de manera local en mi pc este si se ejecuta bien y carga todos los registros
private void CargaArchivos()
{
string Provider = "Microsoft.ACE.OLEDB.12.0";
try
{
if (fuExcel.PostedFile.FileName.Length > 0)
{
String ConnExcel = "";
string HojaExcel = "";
string pathOriginal = Server.MapPath("Files/" + fuExcel.PostedFile.FileName);
if (File.Exists(pathOriginal))
File.Delete(pathOriginal);
fuExcel.SaveAs(pathOriginal);
string nombreArchivo = fuExcel.PostedFile.FileName.ToString();
if (fuExcel.PostedFile.FileName.Contains("xlsx"))
ConnExcel = "Provider=" + Provider + "; Data Source = " + pathOriginal + "; Extended Properties = \"Excel 12.0;HDR=YES;READONLY=FALSE\";";
else
ConnExcel = "Provider=" + Provider + "; Data Source = " + pathOriginal + "; Extended Properties = \"Excel 8.0;HDR=YES;READONLY=FALSE\";";
OleDbConnection ConnLocal = new OleDbConnection(ConnExcel);
ConnLocal.Open();
OleDbCommand cmdLocal = new OleDbCommand("", ConnLocal);
System.Data.DataTable dtExcelSheetName = ConnLocal.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
foreach (DataRow drtabla in dtExcelSheetName.Rows)
if (drtabla["Table_Name"].ToString().ToUpper().Contains("LAYOUT") || drtabla["Table_Name"].ToString().ToUpper().Contains("SHEET1") || drtabla["Table_Name"].ToString().ToUpper().Contains("TABLE1"))
{
HojaExcel = drtabla["Table_Name"].ToString();
break;
}
if (HojaExcel == "")
HojaExcel = "Hoja1$";
OleDbDataAdapter dta = new OleDbDataAdapter("Select * from [" + HojaExcel + "]", ConnLocal);
DataTable dtExcel = new System.Data.DataTable();
dta.Fill(dtExcel);
txtTotal.Text = dtExcel.Rows.Count.ToString();
int filas = 1;
int c = 0;
System.Data.DataTable dtSalida = new System.Data.DataTable();
dtSalida.Columns.Add("AA", typeof(DateTime));
dtSalida.Columns.Add("BB", typeof(string));
dtSalida.Columns.Add("CC", typeof(int));
dtSalida.Columns.Add("DD", typeof(int));
dtSalida.Columns.Add("EE", typeof(string));
dtSalida.Columns.Add("FF", typeof(DateTime));
dtSalida.Columns.Add("GG", typeof(int));
dtSalida.Columns.Add("HH", typeof(string));
dtSalida.Columns.Add("II", typeof(string)).AllowDBNull = true;
dtSalida.Columns.Add("JJ", typeof(float)).AllowDBNull = true;
dtSalida.Columns.Add("KK", typeof(string)).AllowDBNull = true;
dtSalida.Columns.Add("LL", typeof(int)).AllowDBNull = true;
dtSalida.Columns.Add("MM", typeof(int)).AllowDBNull = true;
dtSalida.Columns.Add("NN", typeof(float)).AllowDBNull = true;
dtSalida.Columns.Add("OO", typeof(string)).AllowDBNull = true;
dtSalida.Columns.Add("PP", typeof(int)).AllowDBNull = true;
dtSalida.Columns.Add("QQ", typeof(string)).AllowDBNull = true;
dtSalida.Columns.Add("RR", typeof(DateTime)).AllowDBNull = true;
dtSalida.Columns.Add("SS", typeof(int));
dtSalida.Columns.Add("TT", typeof(float)).AllowDBNull = true;
dtSalida.Columns.Add("UU", typeof(float)).AllowDBNull = true;
dtSalida.Columns.Add("VV", typeof(string)).AllowDBNull = true;
float res_dou;
int i = 0;
foreach (DataRow dr in dtExcel.Rows)
{
try
{
DataRow row2 = dtSalida.NewRow();
if (float.TryParse(dr["UU"].ToString(), out res_dou) == false)
{
throw new Exception("Error al formatear");
}
row2["AA"] = Convert.ToDateTime(Convert.ToDateTime(dr["AA"].ToString().Replace("a", "").Replace("m", "").Replace("p", "").Replace(".", "")).ToString("dd/MM/yyyy"));
row2["BB"] = dr["HR_TRANSACCION"].ToString().Replace(":", "");
row2["CC"] = dr["CVE_CARRIL"].ToString();
row2["DD"] = dr["NUM_EVENTO"].ToString();
row2["EE"] = dr["CVE_PZCOBRO"].ToString();
row2["FF"] = Convert.ToDateTime(Convert.ToDateTime(dr["FCH_BASE"].ToString().Replace("a", "").Replace("m", "").Replace("p", "").Replace(".", "")).ToString("dd/MM/yyyy"));
row2["GG"] = dr["CVE_TRAM_CAM"].ToString();
row2["HH"] = dr["CVE_TVEHICULO"].ToString();
row2["II"] = dr["CVE_CLSVEHEJES_DIC"].ToString();
row2["JJ"] = DBNull.Value;
row2["KK"] = dr["NUM_TARJETA"].ToString();
row2["LL"] = DBNull.Value;
row2["MM"] = DBNull.Value;
row2["NN"] = DBNull.Value;
row2["OO"] = DBNull.Value;
row2["PP"] = Convert.ToInt32(dr["NUM_TRANSVAL_CAPUFE"]);
row2["QQ"] = DBNull.Value;
row2["RR"] = Convert.ToDateTime(dr["RR"].ToString().Replace("a", "").Replace("m", "").Replace("p", "").Replace(".", "")).ToString("dd/MM/yyyy HH:mm:ss");
row2["SS"] = dr["SEQ_TRANSIT"].ToString();
row2["TT"] = DBNull.Value;
row2["UU"] = res_dou;
row2["VV"] = nombreArchivo;
dtSalida.Rows.Add(row2);
i++;
Session["Procesado"] = i;
txtProcesado.Text = Session["Procesado"].ToString();
}
catch (Exception ex)
{
lstMensaje.Items.Add("Error Fila: " + i + 1 + " -> " + ex.Message);
}
}
OracleConnection con = new OracleConnection();
con.ConnectionString = "XXX";
try
{
con.Open();
using (OracleBulkCopy bulkcopy = new OracleBulkCopy(con))
{
bulkcopy.DestinationTableName = "tconsolida";
bulkcopy.WriteToServer(dtSalida);
}
con.Close();
con.Dispose();
}
catch (Exception ex)
{
lstMensaje.Items.Add("Error al hacer bulk-> " + ex.Message);
}
}
}
catch (Exception ex)
{
lstMensaje.Items.Add("Error General: " + ex.Message);
}
}
catch
?, ¿hay alguna manera en la que pueda comprobar qué diferencias hay entre el servidor IIS y su proyecto local?. Cuando he tenido este problema es (o por temas de datos del Excel) o porque faltaba algún campo en la tabla a guardar los datos...Contains("xlsx")
si despues el provider es el mismo para ambos casos ?Session["Procesado"] = i;
y porque asignastxtProcesado.Text
si sabes que no veras reflejado un avance mientras estes en el servidortry...catch
para obtener los problemas que se generen, pero ya que asignas estos a un lista de mensajes, porque no visualizas el problema ?