Question

VB.Net 2005

Asked by: 2004hummer

Hello,

Here is my code:

Imports System
Imports System.IO
Imports System.Diagnostics
Imports APS.Infrastructure
Imports System.Data.SqlClient
Imports cltComps

Public Class Presort
    Dim dsResult As Integer
    Private Function AndNotLineIndexOf(ByVal line As String, ByVal SearchFor As String)
        Dim TotalPieces As Integer
        Dim NumPieces As Integer
        Dim Rate As Integer
       


        If line.IndexOf(SearchFor) > 0 Then 'And Not line.IndexOf("POSTAGE STATEMENT") > 0 Then
           


            TotalPieces = CType(line.Substring(94, 10), Integer)
            Rate = line.Substring(94, 10)
            NumPieces = line.Substring(94, 10)
        End If
        Return dsResult
    End Function
    Public Function ReadFile(ByVal Path As String)
        Dim dsResult As Integer
        Dim fs As FileStream = New FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.Read)
        Dim sr As StreamReader = New StreamReader(fs)
        Dim line As String
        Dim TotalPieces, NumPieces, Rate As Integer

        Try
            Do While sr.Peek() >= 0

                line = sr.ReadLine()
                TotalPieces = AndNotLineIndexOf(line, "A3 3-Digit")
                AndNotLineIndexOf(line, "A2 5-Digit")
                AndNotLineIndexOf(line, "A4 AADC")

            Loop
        Catch ex As Exception

        Finally
            If Not sr Is Nothing Then
                sr.Close()
            End If
            If Not fs Is Nothing Then
                fs.Close()
            End If

        End Try
        Return dsResult
    End Function




Here is where I am stuck and need help…

I am trying to parse data from a text file.

I am wanting the values from line 106….

In the file the field/ row called A1…which is column 3-4
I want the value to pull for this field for this which is column 6-12 and it will be inserted into my database table field called A1Desc


Next, for the same line field/ row called A1 I wann the value to pull for columns 46-50 and it will be inserted into my database table field called A1Rate



Here is my stored procedure to give an idea of where I am going with this:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go



ALTER PROCEDURE [dbo].[UpdatePostageAnalysis]
      (
            @ProcessID                              char(5),
            @A1Desc                              char(20),
            @A1Rate                              decimal(19,4),
            @A1Pieces                              int,
            @A1Total                              decimal(19,4),
            @A2Desc                              char(20),
            @A2Rate                              decimal(19,4),
            @A2Pieces                              int,
            @A2Total                              decimal(19,4),
            @A3Desc                              char(20),
            @A3Rate                              decimal(19,4),
            @A3Pieces                              int,
            @A3Total                              decimal(19,4),
            @A4Desc                              char(20),
            @A4Rate                              decimal(19,4),
            @A4Pieces                              int,
            @A4Total                              decimal(19,4),
            @A5Desc                              char(20),
            @A5Rate                              decimal(19,4),
            @A5Pieces                              int,
            @A5Total                              decimal(19,4),
            @PartAPieces                        int,
            @PartATotal                        decimal(19,4),
            @PartAFullTotal                  decimal(19,4),
            --@sC1Desc                              char(20),
            --@dC1Rate                              decimal(19,4),
            --@iC1Pieces                              int,
            --@dC1Total                              decimal(19,4),
            --@sC2Desc                              char(20),
            --@dC2Rate                              decimal(19,4),
            --@iC2Pieces                              int,
            --@dC2Total                              decimal(19,4),
            ---@sC3Desc                              char(20),
            --@dC3Rate                              decimal(19,4),
            --@iC3Pieces                              int,
            ---@dC3Total                              decimal(19,4),
            --@sC4Desc                              char(20),
            --@dC4Rate                              decimal(19,4),
            ---@iC4Pieces                              int,
            ---@dC4Total                              decimal(19,4),
            ---@iPartCPieces                        int,
            ---@dPartCTotal                        decimal(19,4),
            --@dPartCFullTotal                  decimal(19,4),
            --@dTotalMailingAmount            decimal(19,4),
            ---@dTotalFullMailingAmount      decimal(19,4),
            @TotalPieces                        int
            --@iBillingID                              int
      )
AS
      UPDATE PostageAnalysis
            SET
             A1Desc=@A1Desc, A1Rate=@A1Rate, A1Pieces=@A1Pieces, A1Total=@A1Total,
             A2Desc=@A2Desc, A2Rate=@A2Rate, A2Pieces=@A2Pieces, A2Total=@A2Total,
             A3Desc=@A3Desc, A3Rate=@A3Rate, A3Pieces=@A3Pieces, A3Total=@A3Total,
             A4Desc=@A4Desc, A4Rate=@A4Rate, A4Pieces=@A4Pieces, A4Total=@A4Total,
             A5Desc=@A5Desc, A5Rate=@A5Rate, A5Pieces=@A5Pieces, A5Total=@A5Total,
             PartAPieces=@PartAPieces, PartATotal=@PartATotal, PartAFullTotal=@PartAFullTotal,
             TotalPieces=@TotalPieces
            WHERE ProcessID = @ProcessID
RETURN




I need my VB code modified to get me started on what I am trying to do and I should be able to take it from there to parse the rest of the data.

Thanks for the help.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2007-05-02 at 07:23:07ID22547609
Tags

2005

,

file

Topic

Visual Studio

Participating Experts
1
Points
500
Comments
10

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. =A1 in B1, =A2 in C1, =A3 in D1, ......
    Hi Experts, I want to put =A1 in B1, =A2 in C1, =A3 in D1, ......, =A99 in .... Any idea? Thank you in advance.

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: FernandoSotoPosted on 2007-05-02 at 10:39:41ID: 19017770

Hi 2004hummer;

I will try and help with this although I am not a database programmer. So if you are looking for help in that area please let me know.

It would help if you can post a couple of line from the file that you need to parse.

You state that, "I am wanting the values from line 106…." , not sure if you mean that you need the data from line 106 of the file and on or just line 106?

In the function AndNotLineIndexOf you parse the line but do nothing with the result?

Can you explain a little more.

Thanks;

Fernando

 

by: 2004hummerPosted on 2007-05-02 at 10:59:01ID: 19017905

I have the SQL side...I am sharp in SQL but weak in VB.Net...

If you can help me with the VB side I can take it from there.

The code I pasted earlier is what I was trying to do but have no clue as to what to do....so if you want to disregard all or any of it that it is cool.. This is the output from the .360 report that you helped me with earlier.

It is row 107 not 106...sorry about that...

look at A1 below…..

I need the value 3Digit which is columns 6-13 (inserted into database table field called A1Desc)


I need the value 0.308 which is columns 46-50 (inserted into database table field called A1Rate)

I need the value 74241 which is columns 71-75(inserted into database table field called A1Pieces)

I need the value $  22866.2280 which is columns 96-105 (inserted into database table field called A1Total)


| A1 3-Digit                                 0.308     x              74241          =      $  22866.2280      

I need this all to go into a dataset or something so that I can call my stored procedure “UpdatePostageAnalysis” to set the results in the table


Thanks for the help…

 

by: FernandoSotoPosted on 2007-05-02 at 11:26:35ID: 19018104

A couple of quick questions.

This code will only need to read the 107 th line of the file and pull the data from that line?

Is this file a fixed length field format? Meaning that every line in the file has the same number of characters so that knowing that a line has 112 characters plus line termination characters times 106 lines will place the reader at the beginning of line 107. Or is each line in the file a different length?

What type of database are you connection to?

 

by: 2004hummerPosted on 2007-05-02 at 12:34:49ID: 19018628


I am connecting to an SQL 2005 database.

For demonstration purposes I used line 107 which is one of the lines but here is everything:

All of the lines that need read are 106-109

I used line 107 earlier because it contained data where as line 106 didn’t but it will at times:

Here are all of the lines:


| A2 5-Digit                                     x              
| A3 3-Digit                                 0.308     x              74241          =      $  22866.2280      
| A4 AADC                                 0.308     x              74241          =      $  22866.2280      
| A5 Mixed AADC                                 0.308     x              74241          =      $  22866.2280      



In this file there are no values for the A2 line but if there were it would be the following:

I need the value 5- Digit which is columns 6-13 (inserted into database table field called A2Desc)
I need the value 0.308 which is columns 46-50 (inserted into database table field called A2Rate)
I need the value 74241 which is columns 71-75(inserted into database table field called A2Pieces)
I need the value $  22866.2280 which is columns 96-105 (inserted into database table field called A2Total)



I need the value 3Digit which is columns 6-13 (inserted into database table field called A3Desc)
I need the value 0.308 which is columns 46-50 (inserted into database table field called A3Rate)
I need the value 74241 which is columns 71-75(inserted into database table field called A3Pieces)
I need the value $  22866.2280 which is columns 96-105 (inserted into database table field called A3Total)


I need the value AADC which is columns 6-9 (inserted into database table field called A4Desc)
I need the value 0.308 which is columns 46-50 (inserted into database table field called A4Rate)
I need the value 74241 which is columns 71-75(inserted into database table field called A4Pieces)
I need the value $  22866.2280 which is columns 96-105 (inserted into database table field called A4Total)

I need the value Mixed AADC which is columns 6-15 (inserted into database table field called A5Desc)
I need the value 0.308 which is columns 46-50 (inserted into database table field called A5Rate)
I need the value 74241 which is columns 71-75(inserted into database table field called A5Pieces)
I need the value $  22866.2280 which is columns 96-105 (inserted into database table field called A5Total)


All column locations are the same for each line except for the fact that the AADC and Mixed AADC (A4Desc & A5Desc) positions are slightly different. Everything else is the same.


 

by: FernandoSotoPosted on 2007-05-02 at 13:37:32ID: 19019137

The important question which needs answering is, are all lines in the file the same length?

From the sample you posted they are not. For example on line A3 the field 0.308 starts at column 46 where on line A4 it starts at column 43 and on line A5 it starts at column 49.

 

by: 2004hummerPosted on 2007-05-02 at 13:45:42ID: 19019201

yes all of the lines are the same length......

I have the file open in the text editor and they are all the same length....

I know the pasting of the sample doesn't show it but the column positons are correct that I have indicated

 

by: FernandoSotoPosted on 2007-05-02 at 21:59:31ID: 19021139

Hi 2004hummer;

Here is the sample code to parse the file and the results are placed into a data table.

Imports System.IO

    ' The number of characters on each line without the carriage return/line feed
    Private RecSize As Integer = 105
    ' The first line in the file to be read
    Private FirstRecToRead As Integer = 106
    ' The last line in the file to be read
    Private LastRecToRead As Integer = 109

    Private Function ReadFile(ByVal Filename As String) As Integer

        ' Check to see if file exist before attempting to process it.
        If Not File.Exists(Filename) Then Return -1
        ' Open the file for random access
        Dim fs As New FileStream(Filename, FileMode.Open, _
            FileAccess.Read, FileShare.Read)
        ' Check to see if there is enough data to read the lines we want. In not
        ' Close the file and return an error number
        If fs.Length < ((RecSize + 2) * LastRecToRead - 2) Then
            fs.Close()
            Return -2
        End If
        ' Set up buffer for reading from file
        Dim buffer(RecSize + 1) As Byte
        Dim BufferStr As String
        ' Set up a datatable with the results of the file parse
        Dim dt As DataTable = CreateDataTable()
        Dim dr As DataRow
        ' Position the file pointer to the beginning of the first record to read
        fs.Position = (FirstRecToRead - 1) * (RecSize + 2)
        For idx As Integer = FirstRecToRead To LastRecToRead
            fs.Read(buffer, 0, RecSize + 2)
            BufferStr = System.Text.Encoding.Default.GetString(buffer)
            dr = dt.NewRow
            dr("A2Desc") = BufferStr.Substring(5, 8)
            ' The next three values are allowing for no values in the field
            ' and will set it to DBNull if it is empty.
            If BufferStr.Substring(45, 5).Trim = "" Then
                dr("A2Rate") = DBNull.Value
            Else
                dr("A2Rate") = CDec(BufferStr.Substring(45, 5))
            End If
            If BufferStr.Substring(70, 5).Trim = "" Then
                dr("A2Pieces") = DBNull.Value
            Else
                dr("A2Pieces") = CInt(BufferStr.Substring(70, 5))
            End If
            If BufferStr.Substring(95, 10).Trim = "" Then
                dr("A2Total") = DBNull.Value
            Else
                dr("A2Total") = CDec(BufferStr.Substring(95, 10))
            End If
            dt.Rows.Add(dr)
        Next
        fs.Close()

        ' I had Placed a data grid view on my test form to verify the data it was
        ' pulling from the file this next statement will bind the datatable to the
        ' datagrid so that you can verify the information.
        DataGridView1.DataSource = dt

        ' Return 0 for no error.
        Return 0

    End Function

    ' This function just builds the data table to hold the results of the
    ' information pulled from the file.
    Private Function CreateDataTable() As DataTable

        Dim dt As New DataTable
        Dim dc As DataColumn

        dc = New DataColumn("A2Desc", GetType(String))
        dt.Columns.Add(dc)
        dc = New DataColumn("A2Rate", GetType(Decimal))
        dc.AllowDBNull = True
        dt.Columns.Add(dc)
        dc = New DataColumn("A2Pieces", GetType(Integer))
        dc.AllowDBNull = True
        dt.Columns.Add(dc)
        dc = New DataColumn("A2Total", GetType(Decimal))
        dc.AllowDBNull = True
        dt.Columns.Add(dc)

        Return dt

    End Function

Fernando

 

by: 2004hummerPosted on 2007-05-03 at 07:10:02ID: 19023357

Thanks so much!!

 

by: FernandoSotoPosted on 2007-05-03 at 13:35:58ID: 19026695

Not a problem, glad I was able to help. ;=)

 

by: 2004hummerPosted on 2007-05-03 at 13:38:45ID: 19026720

I have an easy question out there I just posted...500 points...

can you look at it real quick

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...