Site Search:
Sign in | Join | Help

This Blog

Syndication

VB.NET

Notes, Tricks and Tips on VB.NET

September 2008 - Posts

  • Parse a text file row by row

    Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
           
            Try

                Dim reader As New TextFieldParser(Me.OpenFileDialog1.FileName)

                'header row fixed field lengths
                Dim stdFormat As Integer() = {2, 23, 9, 15, 30, 20, 15, 8, 8, 11}
                'detail row fixed field lengths
                Dim stdSecondFormat As Integer() = {1, 14, 17}

               'holds a row's values for PM10000EXT
                Dim pm100values(11) As String
                'holds a row's values for PM10100EXT
                Dim pm101values(3) As String

                Dim vchrnmbr As String
                Dim docdate As Date
                Dim pordnmbr As String
                Dim vendorID As String
                Dim rmitoaddressID As String
                Dim facility As String
                Dim glaccount As String
                Dim DocAmount As Double
                Dim Amount As Double
                Dim dtString As String

                reader.TextFieldType = FieldType.FixedWidth
                reader.SetFieldWidths(stdFormat)

                Dim currentrow As String()

                While Not reader.EndOfData
                    Dim intIndex As Integer = 0
                    Try
                        Dim RowType As String = reader.PeekChars(1)
                        If String.Compare(RowType, "D") = 0 Then
                            ' If this line describes a line that starts with D, the format of the row will be different.
                            reader.SetFieldWidths(stdSecondFormat)
                            currentrow = reader.ReadFields
                            Dim currentField As String

                            For Each currentField In currentrow
                                'MsgBox(currentField)
                                pm101values.SetValue(currentField, intIndex)
                                intIndex += 1
                            Next
                            'create insert procedure for pm10100EXT for these values, run here
                            'setting doc vchrnmbr to the header number
                            vchrnmbr = docVchrNmbr
                            glaccount = pm101values(1)
                            Amount = pm101values(2)

                            facPM10000EXT.PM10100EXT_INS(vchrnmbr, glaccount, Amount)

                            reader.SetFieldWidths(stdFormat)
                        Else

                            currentrow = reader.ReadFields()
                            Dim currentField As String
                            For Each currentField In currentrow
                                'MsgBox(currentField)
                                pm100values.SetValue(currentField, intIndex)
                                intIndex += 1
                            Next
                            'create insert procedure for pm10000EXT for these values, run here
                            vchrnmbr = pm100values(1)

                            'convert didnt like the date string format - work around
                            Dim iYear As Integer = System.Convert.ToInt32(pm100values(7).Substring(0, 4))
                            Dim iMonth As Integer = System.Convert.ToInt32(pm100values(7).Substring(4, 2))
                            Dim iDay As Integer = System.Convert.ToInt32(pm100values(7).Substring(6, 2))
                            docdate = New DateTime(iYear, iMonth, iDay)

                            pordnmbr = pm100values(2)
                            rmitoaddressID = vendorarray(1)
                            facility = pm100values(6)
                            DocAmount = pm100values(9)

                            facPM10000EXT.PM10000EXT_INS(vchrnmbr, docdate, pordnmbr, vendorID, rmitoaddressID, facility, DocAmount)
                            'have to set this variable for details insert
                            
                        End If
                    Catch ex As Exception
                        MsgBox("Line " & ex.Message & _
                                    "is not valid and will be skipped.")


                    End Try
                End While

                MsgBox("Import Complete")

            Catch Ex As Exception

            End Try


        End Sub

  • Code to call a web service

    When I develop a VB application that calls a web service, I usually am developing the web service in the same solution. That's convenient, because you can step through and debug all the code.

    But I'd also like to be able to deploy the web service and have my VB app call the live service. So...

    Here's VB code that will call a web service with a dynamic URL. First, the app.config entry that makes it possible:

    <appSettings>
        <add key="webservicezzz" value="<a href="http://localhost/GPConnect.asmx">http://localhost/GPConnect.asmx</a>" />
        <add key="webservice" value="<a href="http://localhost:1906/eConnect/GPConnect.asmx">http://localhost:1906/eConnect/GPConnect.asmx</a>" />
    </appSettings>
    <system.diagnostics>

    What I do is manipulate the 'zzz's to call the dev or live service. (the one with the '1906' is the dev one... but you knew that)

    Code for Visual Studio 2005:

    Dim webService As localhost.GPConnect = New localhost.GPConnect
    
    
    webService.Url = mstrURL
    
    
    Try
        webService.myMethod()
        MsgBox("done")
    Catch ex As Exception
        MsgBox(ex.Message)
    
    
    End Try

    It's just a little different in 2008:

    Private Sub btnTestEconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestEconnect.Click
        Dim strURL As String = System.Configuration.ConfigurationManager.AppSettings("webService")
        Dim url As New System.ServiceModel.EndpointAddress(strURL)
        
        Dim webService As New GPconnectLocal.GPConnectSoapClient
        webService.Endpoint.Address = url
        
        Try
            webService.Send("", "localhost", "")
            MsgBox("done")
        Catch ex As Exception
            MsgBox(ex.Message)
        
        End Try
    End Sub

    As always, I welcome and comments or feedback.

  • Datatable select syntax

    The following example uses a filter expression to return an array of DataRow objects.

    Private Sub GetRowsByFilter()
    
    
        Dim table As DataTable = DataSet1.Tables("Orders")
    
    
        ' Presuming the DataTable has a column named Date.
        Dim expression As String
        expression = "Date > #1/1/00#"
        Dim foundRows() As DataRow
    
    
        ' Use the Select method to find all rows matching the filter.
        foundRows = table.Select(expression)
    
    
        Dim i As Integer
        ' Print column 0 of each returned row.
        For i = 0 to foundRows.GetUpperBound(0)
           Console.WriteLine(foundRows(i)(0))
        Next i
    End Sub

More Posts Next page »