Solved

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

Posted on 2007-12-06
9
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:noelroshan
ID: 20425027

rs.MoveNext()
Error Operation is not allowed when the object is closed.
0
 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

730 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