Option Strict On Imports Extensiones Imports System.Data Imports System.IO Imports GemBox.Spreadsheet Partial Class GPS_DispositivosControles Inherits ClsPaginas #Region "Propiedades" Public Property CveControl As Integer Get Return ViewState("CveControl").ToString.ToEntero End Get Set(value As Integer) ViewState("CveControl") = value End Set End Property Public Property EsEditar As Integer Get Return ViewState("EsEditar").ToString.ToEntero End Get Set(value As Integer) ViewState("EsEditar") = value End Set End Property #End Region Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then CveControl = -1 EsEditar = 0 CargaDatos(-1) CargaDdls() End If End Sub Private Sub CargaDdls() DdlEquipo.DataSource = GlobalDTDispositivos(-1) DdlEquipo.DataBind() DdlEquipo.Items.Insert(0, New ListItem("Seleccione", "-1")) Dim GPS As New ClsGPS If GPS.UnidadMedidaGet() Then DdlUnidadMedida.DataSource = GPS.DtResultados Else DdlUnidadMedida.DataSource = Nothing End If GPS.Dispose() DdlUnidadMedida.DataBind() DdlUnidadMedida.Items.Insert(0, New ListItem("Seleccione", "-1")) End Sub Private Sub CargaDatos(CveCliente As Integer) Dim GPS As New ClsGPS If GPS.ControlesGet(CveCliente) Then GvDatos.DataSource = GPS.DtResultados Else GvDatos.DataSource = Nothing End If GPS.Dispose() GvDatos.DataBind() UpDatos.Update() End Sub Protected Sub imgE_Click(sender As Object, e As EventArgs) Dim Boton = CType(sender, LinkButton) CveControl = Boton.CommandArgument.ToEntero Dim GvRow As GridViewRow = DirectCast(Boton.NamingContainer, GridViewRow) EsEditar = 1 DdlUnidadMedida.SelectedValueExist(GvRow.DataKey("CveUnidadMedida")) DdlEquipo.SelectedValueExist(GvRow.DataKey("CveDispositivo")) TxtAlto.Text = GvRow.DataKey("Alto") TxtAncho.Text = GvRow.DataKey("Ancho") TxtCalibración.Text = "[]" TxtCapacidad.Text = GvRow.DataKey("Capacidad") TxtControl.Text = GvRow.DataKey("Control") TxtLargo.Text = GvRow.DataKey("Largo") TxtLongitud.Text = GvRow.DataKey("Longitud") TxtSensor.Text = GvRow.DataKey("Sensor") DdlNumero.SelectedValueExist(GvRow.DataKey("CveMetrica")) BtnAgregarCatalogo.Text = "Actualizar" UpBodyCompuesto.Update() UpFooter.Update() RegistraScript("$('#divAgregar').modal()") End Sub Protected Sub BtnAgregarCatalogo_Click(sender As Object, e As EventArgs) Handles BtnAgregarCatalogo.Click Dim GPS As New ClsGPS GPS.IniciaTransacSQL() Dim huboerror As Boolean = False If EsEditar = 0 Then If TxtCalibración.getfiles().Archivos.Count > 0 Then Dim Archivo As Archivo = TxtCalibración.getfiles().Archivos(0) If Archivo.Extencion <> "xls" AndAlso Archivo.Extencion <> "xlsx" Then GPS.RollBack() GPS.Dispose() Alert("El archivo de calibración debe de ser un archivo excel.") Exit Sub End If End If If GPS.ControlAdd(TxtControl.Text, 1, TxtCapacidad.Text.ToDouble, TxtLargo.Text.ToDouble, TxtAncho.Text.ToDouble, TxtAlto.Text.ToDouble, TxtLongitud.Text.ToDouble, TxtSensor.Text, DdlEquipo.SelectedValue.ToEntero, DdlNumero.SelectedValue.ToEntero) Then CveControl = GPS.CveControl Else huboerror = True End If If TxtCalibración.getfiles().Archivos.Count > 0 AndAlso CveControl > 0 AndAlso Not huboerror Then Try Dim Archivo As Archivo = TxtCalibración.getfiles().Archivos(0) Dim Path As String = $"{AppDomain.CurrentDomain.BaseDirectory}TablasCalibracion" Dim dirSonido As String = String.Format("{0}\{1}.{2}", Path, FormatearNombreArchivo(Archivo.filenameO), Archivo.Extencion) If File.Exists(dirSonido) Then My.Computer.FileSystem.DeleteFile(dirSonido) End If My.Computer.FileSystem.MoveFile(Archivo.FileUrl, dirSonido) SpreadsheetInfo.SetLicense(SerialGemBoxSpreadsheet()) Dim EF As ExcelFile = ExcelFile.Load(dirSonido) Dim Hoja As ExcelWorksheet = EF.Worksheets(0) Dim Opciones As New CreateDataTableOptions() Opciones.ColumnHeaders = True Opciones.StartRow = 0 Opciones.NumberOfColumns = 3 Opciones.NumberOfRows = Hoja.Rows.Count Opciones.Resolution = ColumnTypeResolution.AutoPreferStringInvariantCulture Dim TempDT As DataTable = Hoja.CreateDataTable(Opciones) For Each row As DataRow In TempDT.Rows If Not GPS.CalibracionAdd(row("Nivel").ToString.ToDouble, row("Valor").ToString.ToDouble, CveControl) Then huboerror = True Exit For End If Next Catch ex As Exception huboerror = True End Try End If If Not huboerror Then GPS.Commit() Alert("El Control se agregó correctamente") Else GPS.RollBack() Alert("Ocurrió un error al realizar la calibración", TipoMensaje.MsgError) End If ElseIf EsEditar = 1 Then If TxtCalibración.getfiles().Archivos.Count > 0 Then Dim Archivo As Archivo = TxtCalibración.getfiles().Archivos(0) If Archivo.Extencion <> "xls" AndAlso Archivo.Extencion <> "xlsx" Then GPS.RollBack() GPS.Dispose() Alert("El archivo de calibración debe de ser un archivo excel.") Exit Sub End If End If If Not GPS.ControlUpdate(TxtControl.Text, 1, TxtCapacidad.Text.ToDouble, TxtLargo.Text.ToDouble, TxtAncho.Text.ToDouble, TxtAlto.Text.ToDouble, TxtLongitud.Text.ToDouble, TxtSensor.Text, DdlEquipo.SelectedValue.ToEntero, CveControl, DdlNumero.SelectedValue.ToEntero) Then huboerror = True End If If TxtCalibración.getfiles().Archivos.Count > 0 AndAlso Not huboerror Then Try huboerror = Not GPS.CalibracionDelete(CveControl) If Not huboerror Then Dim Archivo As Archivo = TxtCalibración.getfiles().Archivos(0) Dim Path As String = $"{AppDomain.CurrentDomain.BaseDirectory}TablasCalibracion" Dim dirSonido As String = String.Format("{0}\{1}.{2}", Path, FormatearNombreArchivo(Archivo.filenameO), Archivo.Extencion) If File.Exists(dirSonido) Then My.Computer.FileSystem.DeleteFile(dirSonido) End If My.Computer.FileSystem.MoveFile(Archivo.FileUrl, dirSonido) SpreadsheetInfo.SetLicense(SerialGemBoxSpreadsheet()) Dim EF As ExcelFile = ExcelFile.Load(dirSonido) Dim Hoja As ExcelWorksheet = EF.Worksheets(0) Dim Opciones As New CreateDataTableOptions() Opciones.ColumnHeaders = True Opciones.StartRow = 0 Opciones.NumberOfColumns = 3 Opciones.NumberOfRows = Hoja.Rows.Count Opciones.Resolution = ColumnTypeResolution.AutoPreferStringInvariantCulture Dim TempDT As DataTable = Hoja.CreateDataTable(Opciones) For Each row As DataRow In TempDT.Rows If Not GPS.CalibracionAdd(row("Nivel").ToString.ToDouble, row("Valor").ToString.ToDouble, CveControl) Then huboerror = True Exit For End If Next End If Catch ex As Exception huboerror = True End Try End If If Not huboerror Then GPS.Commit() Alert("El Control se actualizó correctamente") Else GPS.RollBack() Alert("Ocurrió un error al realizar la calibración", TipoMensaje.MsgError) End If End If TxtAlto.Text = "" TxtAncho.Text = "" TxtCalibración.Text = "[]" TxtCapacidad.Text = "" TxtControl.Text = "" TxtLargo.Text = "" TxtLongitud.Text = "" TxtSensor.Text = "" DdlEquipo.SelectedValueExist("-1") DdlUnidadMedida.SelectedValueExist("-1") GPS.Dispose() CargaDatos(-1) RegistraScript("$('#divAgregar').modal('hide')") UpBodyCompuesto.Update() UpFooter.Update() End Sub Protected Sub GvDatos_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GvDatos.RowDeleting Dim GPS As New ClsGPS Dim CveControl As Integer = GvDatos.Rows(e.RowIndex).DataKey("CveControl").ToEntero If GPS.ControlesDelete(CveControl) Then Alert("El Control se eliminó correctamente", TipoMensaje.MsgInformacion) End If GPS.Dispose() CargaDatos(-1) UpDatos.Update() End Sub Protected Sub GvDatos_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GvDatos.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then e.Row.VerticalAlign = VerticalAlign.Middle e.Row.FCLinkButton("ImgE").CommandArgument = e.Row.DataKey("CveControl").ToString e.Row.FCLinkButton("ImgC").CommandArgument = e.Row.DataKey("CveControl").ToString End If End Sub Protected Sub btnCancelar_Click(sender As Object, e As EventArgs) Handles BtnOpenDiv.Click EsEditar = 0 BtnAgregarCatalogo.Text = "Agregar" TxtControl.Text = "" TxtCapacidad.Text = "" TxtLargo.Text = "" TxtAncho.Text = "" TxtAlto.Text = "" TxtLongitud.Text = "" TxtSensor.Text = "" TxtCalibración.Text = "[]" DdlEquipo.SelectedValueExist("-1") DdlUnidadMedida.SelectedValueExist("-1") UpBodyCompuesto.Update() UpFooter.Update() RegistraScript("$('#divAgregar').modal()") End Sub Public Function FormatearNombreArchivo(nombreArchivoOriginal As String) As String Dim nombreArchivoFormateado As String nombreArchivoFormateado = nombreArchivoOriginal.Replace(" ", "_") nombreArchivoFormateado = nombreArchivoFormateado.Replace("'", "") nombreArchivoFormateado = nombreArchivoFormateado.Replace(Chr(34), "") nombreArchivoFormateado = nombreArchivoFormateado.Replace("$", "_") nombreArchivoFormateado = nombreArchivoFormateado.Replace("#", "_") nombreArchivoFormateado = nombreArchivoFormateado.Replace("%", "_") nombreArchivoFormateado = nombreArchivoFormateado.Replace("!", "_") nombreArchivoFormateado = nombreArchivoFormateado.Replace("&", "_") nombreArchivoFormateado = nombreArchivoFormateado.Replace("?", "_") nombreArchivoFormateado = nombreArchivoFormateado.Replace("¿", "_") nombreArchivoFormateado = nombreArchivoFormateado.Replace("¡", "_") nombreArchivoFormateado = nombreArchivoFormateado.Replace("*", "_") nombreArchivoFormateado = nombreArchivoFormateado.Replace("~", "_") nombreArchivoFormateado = nombreArchivoFormateado.Replace("^", "_") Return nombreArchivoFormateado End Function Protected Sub imgC_Click(sender As Object, e As EventArgs) Dim Boton = CType(sender, LinkButton) CveControl = Boton.CommandArgument.ToEntero Dim GvRow As GridViewRow = DirectCast(Boton.NamingContainer, GridViewRow) myModalLabel2.InnerText = $"Tabla de calibración de {GvRow.DataKey("Control")}" Dim GPS As New ClsGPS If GPS.CalibracionGet(0, CveControl) Then GvCalibracion.DataSource = GPS.DtResultados End If GPS.Dispose() GvCalibracion.DataBind() EsEditar = 1 UPCalibracion.Update() RegistraScript("$('#dvConsulta').modal()") End Sub End Class