jueves, 28 de febrero de 2019

eliminar el nodo raiz

'Dim input = XDocument.Parse(xmlString)
'Dim output = input.Root.Descendants

fuente

viernes, 22 de febrero de 2019

error web service access is denied cuando se envia un documento

System.Security.Cryptography.CryptographicException: Access is denied.

   at System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr)
   at System.Security.Cryptography.Utils._CreateCSP(CspParameters param, Boolean randomKeyContainer, SafeProvHandle& hProv)
   at System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer)
   at System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSAParameters parameters)
   at WebService.ted_signature.getRSA(String t)
   at WebService.ted_signature.generar_firmar_ted(String contenido_a_firmar, String rsask)
   at WebService.ted_xml.crear_ted(String nodo_caf, String rsask, String rut_emisor, String dte, String folio, String fecha_emision, String rut_receptor, String razon_social_receptor, String monto_total, String item1_detalle, String ts)
   at WebService.Service1.firmar_xml(Byte[] f, Char ambiente)


Solución: dar permisos al usuario Everyone/Todos en la carpeta C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys

error web service al firmar un documento

System.Security.Cryptography.CryptographicException: The system cannot find the file specified.

   at System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer)
   at System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSAParameters parameters)
   at WebService.ted_signature.getRSA(String t)
   at WebService.ted_signature.generar_firmar_ted(String contenido_a_firmar, String rsask)
   at WebService.ted_xml.crear_ted(String nodo_caf, String rsask, String rut_emisor, String dte, String folio, String fecha_emision, String rut_receptor, String razon_social_receptor, String monto_total, String item1_detalle, String ts)
   at WebService.Service1.firmar_xml(Byte[] f, Char ambiente)

solución: agregar en el código

 Dim csp As CspParameters = New CspParameters()
        'csp.Flags = CspProviderFlags.UseExistingKey
        csp.Flags = CspProviderFlags.UseMachineKeyStore
        Dim RSA As System.Security.Cryptography.RSACryptoServiceProvider = New System.Security.Cryptography.RSACryptoServiceProvider(1024, csp)


agregar private key al certificado

fuente

abrir mmc

desde el command prompt cmd
escribir> mmc

jueves, 21 de febrero de 2019

extraer archivo binario sql

Dim con As New SqlConnection(Form1.conex)
        Dim query As String = "select certificado_bin from certificados where id=@id"
        con.Open()
        Using cmd As New SqlCommand(query, con)
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = id

            Using sql_query_result = cmd.ExecuteReader
                If sql_query_result IsNot Nothing Then
                    sql_query_result.Read()
                    Dim blob = New Byte((sql_query_result.GetBytes(0, 0, Nothing, 0, Integer.MaxValue)) - 1) {}
                    sql_query_result.GetBytes(0, 0, blob, 0, blob.Length)
                End If
            End Using
        End Using
        con.Close()

fuente

miércoles, 20 de febrero de 2019

martes, 19 de febrero de 2019

datagridview no seleccionar fila al iniciar


dataGridView1.ClearSelection()

fuente

cargar un combobox con datos sql

Private Sub folios_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim con As New SqlConnection(Form1.conex)
        Dim query As String = "select rut_empresa from empresas"
        con.Open()
        Using cmd As New SqlCommand(query, con)
            Dim rs As SqlDataReader = cmd.ExecuteReader
            While rs.Read
                cbRutEmpresas.Items.Add(rs("rut_empresa").ToString)
            End While
            rs.Close()

        End Using
        con.Close()
    End Sub


Private Sub folios_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim con As New SqlConnection(Form1.conex)
        Dim query As String = "select rut_empresa from empresas"
        con.Open()
        Using cmd As New SqlCommand(query, con)

            Dim adapter As New SqlDataAdapter(cmd)
            Dim table As New DataTable()
            adapter.Fill(table)
            cbRutEmpresas.DataSource = table
            cbRutEmpresas.DisplayMember = "id" ' columna id de la tabla empresas
            cbRutEmpresas.ValueMember = "rut_empresa" ' columna rut_empresa de la tabla rut_empresa
        End Using
        con.Close()
    End Sub


fuente1
fuente2
fuente3




cargar datos datagridview desde sql


 Public Sub get_datos_empresas()
        dgv1.Columns.Clear()
        Dim con As New SqlConnection(Form1.conex)
        Dim da As New SqlDataAdapter("select * from empresas", con)
        Dim ds As New DataSet
        da.Fill(ds)
        Me.dgv1.DataSource = ds.Tables(0)
    End Sub


fuente

viernes, 15 de febrero de 2019

Cambiar encoding

Imports System.IO

Public Class Form1

    Private Sub btnChangeEncoding_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChangeEncoding.Click

        'http://net-informations.com/q/faq/memory.html
        'http://www.csharp411.com/c-convert-string-to-stream-and-stream-to-string/

        Dim xml_encoding As String = "<?xml version=""1.0"" encoding=""iso-8859-1""?>"

        Dim mem As MemoryStream = New MemoryStream
        Dim osetdtexmlout As New StreamWriter(mem, System.Text.Encoding.Default)
        Dim osetdtexmlin As New StreamReader(New MemoryStream(System.Text.Encoding.Default.GetBytes(xml_encoding)), System.Text.Encoding.Default)

        For i As Integer = 1 To 43
            osetdtexmlin.Read()
        Next

        osetdtexmlout.WriteLine("<?xml version=""1.0"" encoding=""ISO-8859-1""?>")
        osetdtexmlout.Write(osetdtexmlin.ReadToEnd)
        osetdtexmlout.Flush()
        mem.Position = 0
        Dim sr As New StreamReader(mem)
        Dim str As String = sr.ReadToEnd
        osetdtexmlout.Close()
        osetdtexmlin.Close()
        Dim p1 As String = ""
    End Sub
End Class
' https://www.pcreview.co.uk/threads/streamreader-to-read-from-string.2245002/
' http://www.csharp411.com/c-convert-string-to-stream-and-stream-to-string/
' https://stackoverflow.com/questions/235156/redirect-net-streamwriter-output-to-a-string-variable

jueves, 14 de febrero de 2019

cambiar encoding xml

 'doc.LoadXml(xml)
 'Dim decl = doc.ChildNodes.OfType(Of XmlDeclaration)().FirstOrDefault
  'decl.Encoding = "ISO-8859-1"
  'doc.Save("DTE_FIRMA_OK.xml")

eliminar xmlns archivo xml

Dim str As String = System.Text.Encoding.UTF8.GetString(f)
            Dim doc As New XmlDocument
            Using tr As XmlTextReader = New XmlTextReader(New StringReader(str))
                tr.Namespaces = False
                doc.Load(tr)
            End Using

martes, 12 de febrero de 2019

Indentar xml en memoria

 data_xml = "<Documento>" & data_xml & "</Documento>"
            Dim memory_stream As New MemoryStream
            Dim writer As New XmlTextWriter(memory_stream, System.Text.Encoding.UTF8)
            doc.LoadXml(data_xml)
            writer.Formatting = Formatting.Indented
            doc.WriteContentTo(writer)
            writer.Flush()
            memory_stream.Flush()

            memory_stream.Position = 0
            Dim stream_reader As New StreamReader(memory_stream)
            Dim xml_indent As String = stream_reader.ReadToEnd


Dim xml_indent As String = indentar_xml("<Documento>" & data_xml & "</Documento>")
Public Function indentar_xml(ByVal xml_string As String) As String
        Dim memory_stream As New MemoryStream
        Dim writer As New XmlTextWriter(memory_stream, System.Text.Encoding.UTF8)
        Dim doc_xml As New XmlDocument
        doc_xml.LoadXml(xml_string)
        writer.Formatting = Formatting.Indented
        doc_xml.WriteContentTo(writer)
        writer.Flush()
        memory_stream.Flush()
        memory_stream.Position = 0
        Dim stream_reader As New StreamReader(memory_stream)
        Dim xml_indent As String = stream_reader.ReadToEnd
        Return xml_indent
    End Function



fuente

Limpiar array

array= New Byte(array.Length - 1) {}
Array.Clear(array, 0, array.Length)

viernes, 8 de febrero de 2019

crear tag unico xml

Private Sub btnCrearTagUnico_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCrearTagUnico.Click
        Dim doc As New XmlDocument
        doc.Load("tagunico.xml")
        Dim x As XmlNode = doc.SelectSingleNode("/configuration/system.web")
        Dim child As XmlElement = doc.CreateElement("httpRuntime")
        child.SetAttribute("maxRequestLengt", 0)
        x.AppendChild(child)
        doc.Save("tagunico.xml")
        Dim p1 As String = ""
    End Sub


<?xml version="1.0"?>
<configuration>
  <system.web>
    <httpModules>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </httpModules>
  </system.web>
</configuration>

<?xml version="1.0"?>
<configuration>
  <system.web>
    <httpModules>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </httpModules>
<httpRuntime maxRequestLengt="0" />
  </system.web>
</configuration>



fuente

web service tamaño archivo

máximo tamaño archivo subida

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="xxx" />
  </system.web>
</configuration>
fuente