Results 1 to 5 of 5

Thread: Help with Loop and Insert into Database (Resolved)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Help with Loop and Insert into Database (Resolved)

    I am trying to insert a record into a database from each line in my text file. So far all I have accomplished is to insert 1 record (the same record) for each line in the file. So if I had 10 lines of different people I am getting some thing like this (see below) instead of the 10 different people.

    Fast Eddie
    Fast Eddie
    Fast Eddie
    Fast Eddie
    Fast Eddie
    Fast Eddie
    Fast Eddie
    Fast Eddie
    Fast Eddie
    Fast Eddie

    The code I have is
    Code:
    '***Subs
        Private Sub ReadFile()
    
            Dim oRead As StreamReader = _
                File.OpenText("C:\member.txt")
    
            Do While oRead.Peek >= 0
                Dim myLine As String = (oRead.ReadLine)
                Dim sAgent As String = myLine.Substring(0, 12)
                Dim sOffice As String = myLine.Substring(12, 12)
                Dim sName As String = myLine.Substring(24, 34)
                Dim sLName As String = myLine.Substring(58, 17)
                Dim sPhone As String = myLine.Substring(195, 12)
                Dim sExt As String = myLine.Substring(207, 4)
                Dim sFax As String = myLine.Substring(211, 12)
                Dim sEmail As String = myLine.Substring(207, 80)
                Dim sWeb As String = myLine.Substring(303, 80)
                Dim sDesig As String = myLine.Substring(386, 30)
                Dim sType As String = myLine.Substring(386, 30)
                Dim sLid As String = myLine.Substring(0, 12)
    
                InsertMember(sAgent, sOffice, sName, sLName, _
                    sPhone, sExt, sFax, sEmail, sWeb, sDesig, _
                    sType, sLid)
            Loop
            oRead.Close()
        End Sub
    Last edited by FastEddie; Apr 26th, 2008 at 01:21 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Help with Look and Insert into Database

    That code looks OK at a glance. It would be interesting to see your InsertMember method because if all you're inserting is "Fast Eddie" then you must not be using all the parameter values you're passing. Also, have you debugged to make sure that you're passing the correct data to InsertMember each time. If you haven't then you should do that immediately, because debugging is the first thing you should do. If the right data is being passed to InsertMember but the wrong data ends up in the database then the issue is obviously with InsertMember.

    Also, it's very inefficient to insert one record at a time like that. Imaging if you had thousands of records and you were connecting, inserting and disconnecting every time. What you should be doing is populating a DataTable with all the data, then inserting it all in one go. Follow the Data Access link in my signature for an example.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Help with Loop and Insert into Database

    jmcilhinney the datatable makes perfect sence and I have modified my code to do that. However I am still having a problem with the loop because this is only giving me the last record in the text file

    vb Code:
    1. ' Loop Problem
    2.     Private Sub ReadFile()
    3.  
    4.         Dim oRead As StreamReader = _
    5.             File.OpenText("C:\member.txt")
    6.  
    7.         Do While oRead.Peek >= 0
    8.  
    9.             Dim myLine As String = (oRead.ReadLine)
    10.             Dim sAgent As String = myLine.Substring(0, 12)
    11.             Dim sOffice As String = myLine.Substring(12, 12)
    12.             Dim sName As String = myLine.Substring(24, 34)
    13.             Dim sLName As String = myLine.Substring(58, 17)
    14.             Dim sPhone As String = myLine.Substring(195, 12)
    15.             Dim sExt As String = myLine.Substring(207, 4)
    16.             Dim sFax As String = myLine.Substring(211, 12)
    17.             Dim sEmail As String = myLine.Substring(207, 80)
    18.             Dim sWeb As String = myLine.Substring(303, 80)
    19.             Dim sDesig As String = myLine.Substring(386, 30)
    20.             Dim sType As String = myLine.Substring(386, 30)
    21.             Dim sLid As String = myLine.Substring(0, 12)
    22.  
    23.  
    24.             Dim myDT As New DataTable
    25.             myDT.Columns.Add("name", GetType(String))
    26.             myDT.Columns.Add("lname", GetType(String))
    27.             myDT.Columns.Add("officeid", GetType(String))
    28.             myDT.Columns.Add("phone", GetType(String))
    29.             myDT.Columns.Add("fax", GetType(String))
    30.             myDT.Columns.Add("email", GetType(String))
    31.             myDT.Columns.Add("web", GetType(String))
    32.             myDT.Columns.Add("designation", GetType(String))
    33.             myDT.Columns.Add("type", GetType(String))
    34.             myDT.Columns.Add("agentID", GetType(String))
    35.             myDT.Columns.Add("lid", GetType(String))
    36.  
    37.             Dim myRow As DataRow = myDT.NewRow
    38.             myRow("name") = sName
    39.             myRow("lname") = sLName
    40.             myRow("officeid") = sOffice
    41.             myRow("phone") = sPhone
    42.             myRow("fax") = sFax
    43.             myRow("email") = sEmail
    44.             myRow("web") = sWeb
    45.             myRow("designation") = sDesig
    46.             myRow("type") = sType
    47.             myRow("agentid") = sAgent
    48.             myRow("lid") = sLid
    49.             myDT.Rows.Add(myRow)
    50.  
    51.             DataGridView1.DataSource = myDT
    52.         Loop
    53.  
    54.         oRead.Close()
    55.  
    56.     End Sub

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Help with Loop and Insert into Database

    1) Don't set the datasource until the end.... rather than each time through the loop
    2) Put a breakpoint on line #7 above.... and step through it... see what it's doing.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Help with Loop and Insert into Database

    Techgnome moving the datasource and rearanging where the columns and rows are set took care of the problem. Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width