1
**below is Textfile**
1010 IBFZEBMSKU28NOV16APTKS42490814
1015 APT55KUAPTKSS28NOV1628NOV1615.05.0016.05.00IRAPTMB254974DKUKU2297/72292700631550063155003                          KUWAIT AIRWAYS
101F 01ALOTAIBI/FAHADMR
1500 CC            01CCVI4439  AIBM6
1502 CC            01CCM  SFP    VI4439950000067487
1502 CC            01CEM  SFP    0418
1502 CC            01CDO  SFP    000
1502 CC            01PVO  SCC    KWD12
1510 LKR9800
1800 LKR9800
5030 31OCT16SYSVNKU0816LEID-0EEE99
7105 MKUWAIT AIRWAYS
1010 BWNALJMSKU28NOV16APTKS42490814

each line refers to a seperate table in sql. I need to import each line to the tables. I have trouble in importing lines "1502" to the one table. below is my code. i need help in the loop statement.

 IFReader.SetFieldWidths(5, 14, 2, 6, 6, 0)
     Dim fields6 As String() = IFReader.ReadFields()
        For Each f6 In fields6
           If Not (IsNothing(fields6(0) = "1502")) Then
              cmd6.Parameters("@RecordType").Value = fields6(0)
                        cmd6.Parameters("@Primary_Form_Payment").Value = fields6(1)
              cmd6.Parameters("@FOP_SubItem_No").Value = fields6(2)
              cmd6.Parameters("@FOP_SubItem_Code").Value = fields6(3)
              cmd6.Parameters("@FOP_SubItem_Code1").Value = fields6(4)
              cmd6.Parameters("@UnknownText").Value = fields6(5)
              cmd6.ExecuteNonQuery()
            End If
            Next
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I think you need `If fields6(0).Trim() = "1502" ...` instead of `If Not (IsNothing(fields6(0) = "1502")) ...`. – Andrew Morton Feb 08 '17 at 11:40
  • What trouble are you having? Have a look at [ask] - you'll see that ideally you need to add a description of expected behaviour and actual behaviour - cheers. – David Wilson Feb 08 '17 at 13:36

1 Answers1

0

I found the solution for my question. I used streamreader instead of textfieldparser. So now i get all the values for lines 1502 from the text file

 Dim fs As New FileStream(txtPath, FileMode.Open, FileAccess.Read)
            Dim sr As New StreamReader(fs)
            sr.BaseStream.Seek(0, SeekOrigin.Begin)

            While sr.Peek() > -1
                str = sr.ReadLine
                Dim splitOut As String() = str.Split(" ")
  If splitOut(0) = "1502" Then
                        ppay = ""
                        fopno1 = ""
                        fopcode = ""
                        fopcode1 = ""
                        untxt = ""

                        ppay = str.Substring(5, 14)
                        fopno1 = str.Substring(19, 2)
                        fopcode = str.Substring(21, 6)
                        fopcode1 = str.Substring(27, 6)
                        untxt = str.Substring(33, str.Length - 33)

                        Dim sql6 As String = "INSERT INTO MCO_1502 VALUES(@Primary_Form_Payment,@FOP_SubItem_No,@FOP_SubItem_Code,@FOP_SubItem_Code1,@UnknownText)"
                        Dim cmd6 As New SqlCommand(sql6, con)


                        cmd6.Parameters.AddWithValue("@Primary_Form_Payment", ppay)
                        cmd6.Parameters.AddWithValue("@FOP_SubItem_No", fopno1)
                        cmd6.Parameters.AddWithValue("@FOP_SubItem_Code", fopcode)
                        cmd6.Parameters.AddWithValue("@FOP_SubItem_Code1", fopcode1)
                        cmd6.Parameters.AddWithValue("@UnknownText", untxt)
                        cmd6.ExecuteNonQuery()
End If
End While