Principales respuestas
Como arreglar el mensagem de error Conversion from type 'Byte()' to type 'String' is not valid

Pregunta
-
Olá a todos !
Estou en un desarollo en tres Capas.
Quiero grabar um fichero convertido a binario pero me sale el error de arriba.
Creo que el error es en la declaración de la Capa Entidad.No lo sé como hacer las declaraciones correctas para grabar los datos en binario.
Gracias por ayudarme.
Mira la etapas abajo.
1 - DESCRIPCIÓN DE LA TABLA
CREATE TABLE [dbo].[CHAMADO_HOMOLOGADO]( [ID] [int] IDENTITY(1,1) NOT NULL, [CODIGO_FORNECEDOR] [int] NOT NULL, [CODIGO_CHAMADO] [int] NOT NULL, [CHAMADO_BINARIO] [varbinary](max) NOT NULL, [NOME_ARQUIVO] [varchar](100) NOT NULL, [DATA_HOMOLOGACAO] [date] NOT NULL,
.....
2 - CAPA ENTIDAD (parcial)
Public Class CEChamadoHomologado
Private _codigo_Fornecedor As String
Private _codigo_Chamado As String
Private _chamado_Binario As String -- esto es el campo que vá recibir los datos en binario
Public Property chamado_Binario As String
Get
Return _chamado_Binario
End Get
Set(value As String)
_chamado_Binario = value
End Set
End Property....
3 - FUNCTION (devuelve ruta + nombre del fichero)
Public Function GetPathFile() MsgError = "" nerror = 0 Dim OFD = OpenFileDialog1 OFD.Multiselect = False OFD.Title = "Selecione um arquivo .PDF" OFD.Filter = "All Files | *.pdf" OFD.ShowDialog() ' txtNomeArquivo.Text = OFD.f For Each F As String In OFD.FileNames txtNomeArquivo.Text = System.IO.Path.GetFileName(F) Next Return OFD.FileName End Function
4 - FUNCTION (recibe la ruta del fichero e devuelve el fichero convetido en binario)
Public Function FiletoBytes(PathFile) Dim fsreader As New FileStream(PathFile, FileMode.Open, FileAccess.Read) Dim breader As New BinaryReader(fsreader) Dim filecreate(fsreader.Length) As Byte breader.Read(filecreate, 0, Convert.ToInt32(fsreader.Length)) fsreader.Close() Return filecreate End Function
5 - CAPAPRESENTACION (parcial)
SUB ANADIR LOS DATOSSub IncluirArquivo() Dim objeto As New CEChamadoHomologado objeto.codigo_Fornecedor = getIdFornecedor() objeto.codigo_Chamado = getcodigoChamado() objeto.chamado_Binario = FiletoBytes(PathFile) -- el erro ocurre en la conversión del fichero objeto.data_Homologacao = getdataHomologacao() ..... capaNegocios.novoArquivo(objeto) End Sub
6 - CAPADATOS
Public Sub novoArquivo(ByVal objArq As CEChamadoHomologado) cn = objCon.conectar() Try cn.Open() da = New SqlDataAdapter("PROCvbi_CADASTRA_CHAMADO_HOMOLOGADO", cn) da.SelectCommand.CommandType = CommandType.StoredProcedure With da.SelectCommand.Parameters .Add("@CODIGO_FORNECEDOR", SqlDbType.Char).Value = objArq.codigo_Fornecedor .Add("@CODIGO_CHAMADO", SqlDbType.Char).Value = objArq.codigo_Chamado .Add("@CHAMADO_BINARIO", SqlDbType.VarBinary).Value = objArq.chamado_Binario -- fichero convertido .Add("@NOME_ARQUIVO", SqlDbType.Char).Value = objArq.nome_Arquivo .Add("@DATA_HOMOLOGACAO", SqlDbType.Char).Value = objArq.data_Homologacao End With da.SelectCommand.ExecuteNonQuery() MsgBox("Arquivo incluido...", MsgBoxStyle.Information) Catch ex As Exception MessageBox.Show("Error ao incluir Aquivo " + ex.Message, "Mensagem", MessageBoxButtons.OK, MessageBoxIcon.Error) Finally da.Dispose() cn.Dispose() End Try End Sub
7 - IMAGEN del error
JoseBonfim
- Editado JoseBonfim martes, 22 de noviembre de 2016 0:45
Respuestas
-
Hola JoseBonfim,
Pero estás declarando tu variable chamado_Binario como tipo String, tendrías que ser de tipo Byte().
Private _chamado_Binario As Byte() Public Property chamado_Binario As Byte() Get Return _chamado_Binario End Get Set(value As Byte()) _chamado_Binario = value End Set End Property
Pienso que tu función podría simplificarse a :
Public Function FiletoBytes(ByVal PathFile As String) As Byte() Return File.ReadAllBytes(PathFile) End Function
Procura agregar el Option Strict On antes de tus Imports, para tener un mayor control de las conversiones.
Saludos.
JC NaupaCrispín
Lima - Perú
La magia no existe, la programación SI- Marcado como respuesta JoseBonfim martes, 22 de noviembre de 2016 22:36
Todas las respuestas
-
Hola JoseBonfim,
Pero estás declarando tu variable chamado_Binario como tipo String, tendrías que ser de tipo Byte().
Private _chamado_Binario As Byte() Public Property chamado_Binario As Byte() Get Return _chamado_Binario End Get Set(value As Byte()) _chamado_Binario = value End Set End Property
Pienso que tu función podría simplificarse a :
Public Function FiletoBytes(ByVal PathFile As String) As Byte() Return File.ReadAllBytes(PathFile) End Function
Procura agregar el Option Strict On antes de tus Imports, para tener un mayor control de las conversiones.
Saludos.
JC NaupaCrispín
Lima - Perú
La magia no existe, la programación SI- Marcado como respuesta JoseBonfim martes, 22 de noviembre de 2016 22:36
-
Joel,
Muchas gracias por contestar mi duda.
Hizo como me haz dito.
Saludos,
JoseBonfim (desde Brazil)
- Editado JoseBonfim martes, 22 de noviembre de 2016 22:40