I am making an application that uploads an excel file to the server through a input fileupload
, when the file is uploaded, the fields of the excel file are inserted into a DB.
When there are fields that have already been loaded (duplicates) it does not insert any excel record (neither duplicates nor duplicates), but when there are duplicate records it sends a message in English.
Is it possible to translate ?, it also sends that id's are duplicated. How can I get them to put together a more personalized message?
Public Class Cargador
Inherits System.Web.UI.Page
Dim mifichero As HttpPostedFile
Dim nombreArchivoUsuario As String
Dim sSheetName As String
Protected Sub btnAdjuntar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdjuntar.Click
Try
If (subirArchivos.HasFile) Then
'Sube el archivo seleccionado a la carpeta adjuntos del servidor
subirArchivos.SaveAs(MapPath("./adjuntos/" + subirArchivos.FileName))
mifichero = subirArchivos.PostedFile
'Obtiene el nombre del archivo que se ha subido
nombreArchivoUsuario = mifichero.FileName
lbmostrardv.visible = True
lbadjuntardv.visible = False
lbadjuntar.Text = ""
lbmostrar.Text = nombreArchivoUsuario
lbmostrare.Text = ""
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim cn As SqlClient.SqlConnection = New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("reportes").ConnectionString)
Dim sql1 As SqlClient.SqlCommand
Dim dr As SqlClient.SqlDataReader
cn.Open()
sql1 = New SqlClient.SqlCommand("select nombreCargador from CHIP where nombreCargador='" & nombreArchivoUsuario & "'", cn)
dr = sql1.ExecuteReader()
''------------ Verifica que el archivo no se haya cargado ----------------------------
If dr.HasRows Then
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "<script type='text/javascript'>alert('El archivo " & nombreArchivoUsuario & " ya fue cargado')</script>")
Else
Dim cadconex As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=C:\Inetpub\wwwroot\adjuntos\" & nombreArchivoUsuario & "; Extended Properties=""Excel 8.0;HDR=Yes"""
Dim conn As OleDbConnection
conn = New OleDbConnection(cadconex)
conn.Open()
''OBTENEMOS NOMBRE DE LA PRIMER HOJA
Dim dtExcelSchema As DataTable
dtExcelSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
''SELECCIONAMOS TODO LO QUE CONTENGA LA PRIMER HOJA
Dim comand As OleDbCommand
comand = New OleDbCommand("select * from [Hoja1$]", conn)
Dim adapter As New OleDbDataAdapter
adapter.SelectCommand = comand
Dim ds As DataSet
ds = New DataSet
adapter.Fill(ds)
conn.Close()
''INSERTAMOS EN LA BASE DE DATOS LO OBTENIDO DEL EXCEL
Dim conectsql As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("reportes").ConnectionString)
conectsql.Open()
Dim importar As SqlBulkCopy
importar = New SqlBulkCopy(conectsql)
importar.DestinationTableName = "CHIP"
importar.WriteToServer(ds.Tables(0))
lbimportar.Text = "Se adjuntaron e insertaron correctamente los datos del archivo seleccionado"
conectsql.Close()
'------------ Bandera de control para que no se carguen archivos de nueva cuenta ----------------------------
Dim cn2 As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("reportes").ConnectionString)
Dim sql2 As SqlClient.SqlCommand
cn2.Open()
sql2 = New SqlClient.SqlCommand("update dbo.CHIP set nombreCargador='" + nombreArchivoUsuario + "' WHERE nombreCargador is null ", cn2)
sql2.ExecuteNonQuery()
cn2.Close()
End If
dr.Close()
cn.Close()
'Catch ex As Exception
'System.Web.HttpContext.Current.Response.Write("<div id='mensaje'>" & ex.Message & "</div>")
'End Try
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Else
lbmostrardv.visible = False
lbadjuntardv.visible = True
lbadjuntar.Text = "Error al cargar el archivo. Asegurese de que sea un archivo válido"
lbmostrar.Text = ""
lbmostrare.Text = ""
End If
Catch ex As Exception
System.Web.HttpContext.Current.Response.Write("<div id='mensaje'>" & ex.Message & "</div>")
lbmostrar.Text = ex.ToString()
End Try
End Sub
End Class