Solved

Need to split column full of data  and insert to new table?

Posted on 2007-12-06
9
208 Views
Last Modified: 2013-11-26
I try this code but not working pls help me out.

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration


Public Class Form1
    Dim InsrSql As String
    Dim RPID As String
    Dim tempStr As String
    Dim Bname() As String
    Dim words() As String
    Dim i As Integer
    Dim pos As String
    Dim s As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Sample data in Authors
        's = "Xiao, J & Zhang, Y & Fuller, A & Xiaohua, J"

        Dim conn As New ADODB.Connection
        Dim rs As ADODB.Recordset
        Dim SqlStr As String
        conn.Open(" Provider=SQLOLEDB; server=PQDEV1; Initial Catalog=ndev; User ID=*****;Password=****;")
        SqlStr = "SELECT  USQ_RPID ,Authors FROM Author "
        rs = conn.Execute(SqlStr, , )

        While Not rs.EOF

            RPID = rs("USQ_RPID")
            s = rs("Authors")
            words = Split(s, "&")

            For i = 0 To UBound(words)
                pos = CStr(i + 1)
                tempStr = words(i)
                Bname = Split(tempStr, ",")

                If Len(tempStr) > 0 Then

                    'MsgBox("Fname -" + Bname(0) + " L int -" + Bname(1) + " Pos -" + CStr(1 + i))

                    InsrSql = "INSERT INTO SpAuthors(USQ_RPID, F_Name, L_NameI, pos) VALUES ('" & RPID & "', '" & Bname(0) & "', '" & Bname(1) & "', '" & pos & "'"
                    ' rs = conn.Execute(InsrSql, , )

                End If
            Next
            rs.MoveNext()
        End While
        conn.Close()
    End Sub

   
   
End Class
0
Comment
Question by:noelroshan
  • 5
  • 4
9 Comments
 
LVL 25

Expert Comment

by:imitchie
ID: 20424769
missing end )

                    InsrSql = "INSERT INTO SpAuthors(USQ_RPID, F_Name, L_NameI, pos) VALUES ('" & RPID & "', '" & Bname(0) & "', '" & Bname(1) & "', '" & pos & "')"
0
 

Author Comment

by:noelroshan
ID: 20424897
s = rs("Authors").Value is saying error Conversion from type 'DBNull' to type 'String' is not valid
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20424906
s = Convert.ToString(rs("Authors").Value)

However, instead of NULL in the db field, you will end up with '' (empty string)
0
 

Author Comment

by:noelroshan
ID: 20425027

rs.MoveNext()
Error Operation is not allowed when the object is closed.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 25

Expert Comment

by:imitchie
ID: 20425042
change

rs = conn.Execute(InsrSql, , )

to

conn.Execute(InsrSql, , )

otherwise you're tramping through the previous rs= asignment
0
 

Author Comment

by:noelroshan
ID: 20425090
what is that mean "Line 1: Incorrect syntax near 'Brien'.""
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20425127
InsrSql = "INSERT INTO SpAuthors(USQ_RPID, F_Name, L_NameI, pos) VALUES ('" & RPID & "', '" & Bname(0).Replace("'","''") & "', '" & Bname(1).Replace("'","''") & "', '" & pos & "'"
0
 

Author Comment

by:noelroshan
ID: 20425273
InsrSql = "INSERT INTO SpAuthors(USQ_RPID, F_Name, L_NameI, pos) VALUES ('" & RPID & "', '" & Bname(0).Replace("'","''") & "', '" & Bname(1).Replace("'","''") & "', '" & pos & "'"

Error :Index was outside the bounds of the array.
0
 
LVL 25

Accepted Solution

by:
imitchie earned 500 total points
ID: 20425357
That happens when you have a name with no comma, which causes Split to return only 1 value for Bname...
make this minor change

                tempStr = words(i)
                if InStr(tempStr, ",") < 1 then tempStr = tempStr + ","
                Bname = Split(tempStr, ",")
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
upgrade sql 2005 32bit to sql 2008 32 or 64bit on a server 2008 r2 box 6 54
Query 14 54
CROSS APPLY 4 45
How can I use SQL Stored Procedure to return FedEx Zone 17 41
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now