Results 1 to 13 of 13

Thread: [RESOLVED] [2005] readXML populate different Tables

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Resolved [RESOLVED] [2005] readXML populate different Tables

    Ok. I Export two tables in XML using writeXML and i want to update another database with the same table schema with these XML tables.
    Below is the Update code and Exported XML i am trying to use.
    So far No Luck. where am i going wrong?
    Code:
    VB Code:
    1. Private Sub updateFromXML()
    2.         Dim dsXMLImport As New DataSet
    3.         Dim cmdAdapter As New OleDbCommand
    4.         myConn = New OleDbConnection(strAccessConn)
    5.  
    6.         Try
    7.             myConn.Open()
    8.             cmdAdapter.Connection = myConn
    9.             Dim da1 As New OleDbDataAdapter()
    10.             Dim da2 As New OleDbDataAdapter()
    11.  
    12.             dsXMLImport.ReadXml(Me.tbFilePath.Text)
    13.             'For Table 1
    14.             da1.SelectCommand = cmdAdapter
    15.             da1.InsertCommand.Parameters.Add(dsXMLImport.Tables(0))
    16.             da1.Fill(dsXMLImport)
    17.             da1.Update(dsXMLImport)
    18.             'For Table 2
    19.             da2.SelectCommand = cmdAdapter
    20.             da2.InsertCommand.Parameters.Add(dsXMLImport.Tables(1))
    21.             da2.Fill(dsXMLImport)
    22.             da2.Update(dsXMLImport)
    23.  
    24.         Catch ex As Exception
    25.  
    26.         Finally
    27.             myConn.Close()
    28.             cmdAdapter.Dispose()
    29.         End Try
    30.     End Sub
    Attached Files Attached Files

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [2005] readXML populate different Tables

    **bump**

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [2005] readXML populate different Tables

    bump....bum......bu...b.
    Any way i am seeking to resolve this thread. Any helper?

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [2005] readXML populate different Tables

    I get no error...but i get no results too!
    If you dont like my Bump then Quit the Club. The majority say it usually serves its purpose in here.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [2005] readXML populate different Tables

    I am reading from an XML file and not writting to it.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [2005] readXML populate different Tables

    thanks shakti5385, i would rather you read my first post before posting any further. It describes what i am doing. As indicated,i am using 2005, and as indicated again, i am using readXML.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [2005] readXML populate different Tables

    But that does not mean that you Hijack threads just because you have the same problem. Besides your problem is almost the same but a little different.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [2005] readXML populate different Tables

    I have scratched my head and failed to get it,searched through the cyber but failed to get an answer. I thought it was the table name but after specifying the table name, i cant still have it. Some one help my head!
    VB Code:
    1. Private Sub updateFromXML()
    2.         Dim dsXMLImport As New DataSet
    3.         Dim cmdAdapter As New OleDbCommand
    4.         myConn = New OleDbConnection(strAccessConn)
    5.  
    6.         Try
    7.             myConn.Open()
    8.             cmdAdapter.Connection = myConn
    9.             Dim da1 As New OleDbDataAdapter()
    10.             Dim da2 As New OleDbDataAdapter()
    11.  
    12.             dsXMLImport.ReadXml(Me.tbFilePath.Text)
    13.             'For Table 1
    14.             da1.SelectCommand = cmdAdapter
    15.             da1.InsertCommand.Parameters.Add(dsXMLImport.Tables(0).TableName)
    16.             da1.Fill(dsXMLImport, dsXMLImport.Tables(0).TableName)
    17.             da1.Update(dsXMLImport, dsXMLImport.Tables(0).TableName)
    18.             'For Table 2
    19.             da2.SelectCommand = cmdAdapter
    20.             da2.InsertCommand.Parameters.Add(dsXMLImport.Tables(1).TableName)
    21.             da2.Fill(dsXMLImport, dsXMLImport.Tables(1).TableName)
    22.             da2.Update(dsXMLImport, dsXMLImport.Tables(1).TableName)
    23.  
    24.  
    25.         Catch ex As Exception
    26.  
    27.         Finally
    28.             myConn.Close()
    29.             cmdAdapter.Dispose()
    30.         End Try
    31.     End Sub

    Ok...when i catch the exception, it tells me that object reference not set o an instance of object.
    And when i remove the try catch block..i see the exception is here.
    da1.InsertCommand.Parameters.Add(dsXMLImport.Tables(0).TableName).
    Last edited by maps; Nov 8th, 2006 at 06:51 AM.

  9. #9
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] readXML populate different Tables

    What does the insertcommand text look like?

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [2005] readXML populate different Tables

    Thanks stanav. there is no command text, i thought that the insertCommand given the tablename as the parameter can replace the insertcommand text.
    I have tried to instantiate the insert command but still i get an exception on the same line saying...
    "The OleDbParameterCollection only accepts non-null OleDbParameter type objects, not String objects."
    I dont understand this now!...
    i thought the parameter i am mean't to pass here should be the name of the first table in the XML! so if it does not allow this, then what can i pass?!
    below is my code.

    i would use insert statements and add fields as parameters to the insertcommand, but i want to import more than 10 tables with each having a minimum of 10 fields! So hardcording more than 10 insert command texts with each having 10 fields passed as parameters to the insert command would be alot of code.

    VB Code:
    1. Private Sub updateTableFromXML()
    2.         Dim dsXMLImport As New DataSet
    3.         Dim cmdAdapter As New OleDbCommand
    4.         myConn = New OleDbConnection(strAccessConn)
    5.  
    6.         Try
    7.             myConn.Open()
    8.             cmdAdapter.Connection = myConn
    9.             Dim da1 As New OleDbDataAdapter()
    10.             Dim da2 As New OleDbDataAdapter()
    11.  
    12.             dsXMLImport.ReadXml(Me.tbFilePath.Text)
    13.             'For Table 1
    14.             da1.SelectCommand = cmdAdapter
    15.             da1.InsertCommand = New OleDbCommand()
    16.             da1.InsertCommand.Parameters.Add(dsXMLImport.Tables(0).TableName)
    17.             da1.Fill(dsXMLImport, dsXMLImport.Tables(0).TableName)
    18.             da1.Update(dsXMLImport, dsXMLImport.Tables(0).TableName)
    19.             'For Table 2
    20.             da2.SelectCommand = cmdAdapter
    21.             da2.InsertCommand = New OleDbCommand()
    22.             da2.InsertCommand.Parameters.Add(dsXMLImport.Tables(1).TableName)
    23.             da2.Fill(dsXMLImport, dsXMLImport.Tables(1).TableName)
    24.             da2.Update(dsXMLImport, dsXMLImport.Tables(1).TableName)
    25.  
    26.  
    27.         Catch ex As Exception
    28.             MessageBox.Show(ex.Message)
    29.         Finally
    30.             myConn.Close()
    31.             cmdAdapter.Dispose()
    32.         End Try
    33.     End Sub
    Last edited by maps; Nov 9th, 2006 at 12:38 AM.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [2005] readXML populate different Tables

    Ok. i got it work.
    Here is what i did.
    Created an SQL statement, a new adapter and a new insertCommand
    read my XML, inserted parameters for the insertCommand with items from the dataset.
    Updated the dataAdapter...and Bingo i imported one table.

    I think i will stay with this,which implies if i have 10 tables to update on the Fly, i will repeat the same routine for all the 10 tables.
    Last edited by maps; Nov 9th, 2006 at 02:24 AM.

  12. #12
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: [RESOLVED] [2005] readXML populate different Tables

    maps, if you don't mind could you please post your code. I have been trying to accomplish the same thing now for a couple of weeks and have not been successful.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [RESOLVED] [2005] readXML populate different Tables

    Here you are.
    You will need to modify it a little to suit your needs.
    VB Code:
    1. Private Sub update_tblPopulatedPlaces_fromXML()
    2.         Dim dsXMLImport As New DataSet
    3.         myConn = New OleDbConnection(strAccessConn)
    4.         Dim strSQL As String = "INSERT INTO tblPopulatedPlaces(AdminBdry1_ID,AdminBdry2_ID,AdminBdry3_ID,AdminBdry4_ID,PPname,uid_Created,date_Created,uid_Modified,date_Modified)" & _
    5.         " VALUES(@AdminBdry1_ID,@AdminBdry2_ID,@AdminBdry3_ID,@AdminBdry4_ID,@PPname,@uid_Created,@date_Create  d,@uid_Modified,@date_Modified)"
    6.         Dim da As New OleDbDataAdapter()
    7.         da.AcceptChangesDuringFill = False
    8.         da.InsertCommand = New OleDbCommand(strSQL, myConn)
    9.         Try
    10.             myConn.Open()
    11.             dsXMLImport.ReadXml(Me.tbFilePath.Text)
    12.             With da.InsertCommand.Parameters
    13.                 For i As Integer = 0 To dsXMLImport.Tables(1).Rows.Count - 1
    14.                     drPublic = dsXMLImport.Tables(1).Rows(i)
    15.                     .AddWithValue("@AdminBdry1_ID", drPublic(1))
    16.                     .AddWithValue("@AdminBdry2_ID", drPublic(2))
    17.                     .AddWithValue("@AdminBdry3_ID", drPublic(3))
    18.                     .AddWithValue("@AdminBdry4_ID", drPublic(4))
    19.                     .AddWithValue("@PPname", drPublic(5))
    20.                     .AddWithValue("@uid_Created", drPublic(6))
    21.                     .AddWithValue("@date_Created", drPublic(7))
    22.                     .AddWithValue("@uid_Modified", drPublic(8))
    23.                     .AddWithValue("@date_Modified", drPublic(9))
    24.                 Next i
    25.             End With
    26.             da.Update(dsXMLImport, dsXMLImport.Tables(1).TableName)
    27.         Catch exSQL As OleDbException
    28.             ShowErrorBoxOK("update_tblPopulatedPlaces_fromXML():", exSQL.Message, My.Resources.myStrings.capSQLError)
    29.         Catch ex As Exception
    30.             ShowErrorBoxOK("update_tblPopulatedPlaces_fromXML():", ex.Message, My.Resources.myStrings.capGeneralError)
    31.         Finally
    32.             myConn.Close()
    33.             myConn.Dispose()
    34.             da.Dispose()
    35.         End Try
    36.     End Sub

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