Copy datarow from Table A to same schema Table

QEDDave

New member
Joined
Jun 14, 2010
Messages
3
Programming Experience
Beginner
Hello,

I'm getting an error as it's not saving to the database but I can see the datatable on the console - not sure why the database isn't committing.... Here's the code

    Private Sub btnConsole_Click(sender As Object, e As EventArgs) Handles btnConsole.Click
        Dim ds As DataSet = New DataSet()
        Dim tblPicture As DataTable
        Dim tblNewPics As DataTable
        Dim tblPictureCount As Integer
        Dim i As Integer

        Dim conn As SqlConnection = New SqlConnection("Server=R850-PC\SQLEXPRESS2012;database=NewParts;Integrated Security=True;Persist Security Info=False")
        Dim da As SqlDataAdapter = New SqlDataAdapter("Select * from Picture Where IsNew = 1", conn)
        da.Fill(ds, "Picture")
        tblPicture = ds.Tables("Picture")
        tblPictureCount = tblPicture.Rows.Count
        tblPictureCount = tblPictureCount - 1

        'SOURCE Picture table to the screen.
        Console.WriteLine("Source Table tblPicture has " & tblPictureCount.ToString & " Rows")

        For i = 0 To tblPictureCount
            Console.WriteLine("Row(" & i.ToString & ") = " & tblPicture.Rows(i)(3))
        Next

        '********************* ADD TO NEW TABLE DATA **************************************************************************************

        Dim da2 As SqlDataAdapter = New SqlDataAdapter("Select * from Picture_Temp", conn)
        da2.Fill(ds, "Picture_Temp")
        tblNewPics = ds.Tables("Picture_Temp")
        tblNewPics = tblPicture.Clone

        For i = 0 To tblPictureCount
            tblNewPics.ImportRow(tblPicture.Rows(i))
            Dim dr2 As DataRow = tblNewPics.Rows(i)
            dr2.SetAdded()
        Next
        da2.Update(ds.Tables("Picture_Temp"))
        tblNewPics.AcceptChanges()

        '******************* DEBUG ... check new database table
        Console.WriteLine()
        Console.WriteLine("Destination TABLE has " & tblNewPics.ToString & " Rows")
        For i = 0 To tblPictureCount
            Console.WriteLine("Row(" & i.ToString & ") = " & tblNewPics.Rows(i)(3))
        Next
        Console.WriteLine("RowState = " & tblNewPics.Rows(i - 1).RowState.ToString)
        Console.ReadLine()

        txtMessages.Text = "Done"

    End Sub

Any ideas??

Thanks for any help
 
Last edited by a moderator:
Firstly, I have added the appropriate formatting tags to your post. Please make sure to always format your code snippets for readability.

Secondly, it's not enough to just tell as that an error occurs. The IDE provides you with plenty of information on the error, including an error message, a location and a call stack. That information is all to help you diagnose the issue. If you want us to help you diagnose the issue then logic dictates that you pass that information on to us. That way we can often hone in on exactly what the issue is without having to wade through irrelevant code.
 
Back
Top