Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 16013

rows in incorrect order when inserting into SQL database

$
0
0
Hi, I have a piece of software that helps us rapidly change entries in our sql compact database. The issue we are having is when we insert many rows after deleting rows, the inserted rows are not in the correct order.

To attempt to clarify:

If we simply add new rows, irregardless of how many, they will always be in the correct order.

If we start by pulling a set or rows and editing them (possibly removing, adding or changing the content). Our software then deletes that block of rows, and reinserts what we have just changed. It does this instead of updating rows because some may have been removed or some may have been added. If we have only manipulated a small block of rows (say <20ish) it will insert them in the correct order. If it is a large number of rows it will scramble the order.

We have found a work around where we do this:

Pull the block of rows from the database into our software for editing.
After editing, use SDF viewer to manually select and delete those rows.
Go back to our software and reinsert the edited rows.

No matter how many rows we manipulate, if we use the work around it will insert them in the correct order.

We cannot figure out why our software scrambles the rows order.

Here is the code to delete a block of rows:

Code:

Dim connstring As String = "Data Source='" & filelocation.Text & "';Password='***'"
                Dim conn As New SqlCeConnection(connstring)
                Dim CommandText As String = "delete From Cat where cat='" + catbox.Text + "'"
                Dim cmd As New SqlCeCommand(CommandText, conn)

                conn.Open()

                Try
                    cmd.ExecuteNonQuery()
                    Catch ex As Exception
                    MsgBox("Error!")
                End Try


                conn.Close()

Here is the code to insert:

Code:

Dim rowcount As Integer = 0

        'build insert statement

        Dim connstring As String = "Data Source='" & filelocation.Text & "';Password='***'"
        Dim conn As New SqlCeConnection(connstring)
        Dim sqlstatement As String = ""
        Dim cmd As New SqlCeCommand(sqlstatement, conn)
        Dim dt As New DataTable
        Dim da As New SqlCeDataAdapter(cmd)

        conn.Open()

        DataGrid.RefreshEdit()

        For Each row As DataGridViewRow In DataGrid.Rows

            If DataGrid.Rows.Count - 1 > rowcount Then

                If String.IsNullOrEmpty(row.Cells(0).Value) Then
                    row.Cells(0).Value = 0
                End If

                If String.IsNullOrEmpty(row.Cells(1).Value) Then
                    row.Cells(1).Value = ""
                End If

                If String.IsNullOrEmpty(row.Cells(2).Value) Then
                    row.Cells(2).Value = ""
                End If

                If String.IsNullOrEmpty(row.Cells(3).Value) Then
                    row.Cells(3).Value = ""
                End If

                If String.IsNullOrEmpty(row.Cells(4).Value) Then
                    row.Cells(4).Value = ""
                End If

                If String.IsNullOrEmpty(row.Cells(5).Value) Then
                    row.Cells(5).Value = ""
                End If

                If String.IsNullOrEmpty(row.Cells(6).Value) Then
                    row.Cells(6).Value = 0
                End If

                If String.IsNullOrEmpty(row.Cells(7).Value) Then
                    row.Cells(7).Value = ""
                End If

                If String.IsNullOrEmpty(row.Cells(8).Value) Then
                    row.Cells(8).Value = ""
                End If

                If String.IsNullOrEmpty(row.Cells(9).Value) Then
                    row.Cells(9).Value = 0
                End If

                sqlstatement = "INSERT INTO Cat(ID, Cat, Product, [Key], [Desc], link, visible, jump, prev, user) VALUES('" & idtextbox.Text & "', '" & catbox.Text & "', '" & row.Cells(2).Value.ToString & "', '" & row.Cells(3).Value.ToString & "', @descr, '" & row.Cells(5).Value.ToString & "', '" & visible & "', '" & row.Cells(7).Value.ToString & "', '" & row.Cells(8).Value.ToString & "', '" & user.Text & "')"

                cmd.Parameters.Clear()
                cmd.Parameters.AddWithValue("@descr", row.Cells(4).Value)

                cmd.CommandText = sqlstatement

                cmd.ExecuteNonQuery()

                rowcount = rowcount + 1

                sqlstatement = ""

            End If

        Next


Any help is greatly appreciated!

Viewing all articles
Browse latest Browse all 16013

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>