Solved

update creating dups

Posted on 2012-04-11
4
244 Views
Last Modified: 2012-04-18
When this runs it doesn't update the rows it inserts them.                    


Dim data_adapter As New OleDb.OleDbDataAdapter("SELECT * FROM thierorderguide WHERE Company = '" & lbCompanies.SelectedItem.ToString & "'", mainform.dbconnection)
                    Dim command_builder As New OleDb.OleDbCommandBuilder(data_adapter)
                    data_adapter.Update(DataGridViewToDataTable(dgadditem))
0
Comment
Question by:Millkind
  • 2
  • 2
4 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37835289
Its because you recreate the adapter. Declare the adapter at class level and use the same instance to populate and update the datatable.
0
 

Author Comment

by:Millkind
ID: 37837103
I switched up and am using the dataadapter from the loaddatagrid sub, it is still inserting and not updating

Here is the code for loading the datagrid:
    Sub loaddatagrid(ByVal sql As String)
        ''fill the datagrid with all items in the order guide by the company selected
        dsadditems = New DataSet
        dvdataview = New DataView
        daeditdeleteorderguide = New OleDb.OleDbDataAdapter(sql, mainform.dbconnection)
        daeditdeleteorderguide.Fill(dsadditems)
        dgadditem.DataSource = dsadditems.Tables(0).DefaultView.ToTable(True)
        dvdataview = New DataView(dgadditem.DataSource)
        dgadditem.DataSource = dvdataview
    End Sub

Open in new window


Here is the code when saved is clicked:
                Try
                    Dim command_builder As New OleDb.OleDbCommandBuilder(daeditdeleteorderguide)
                    daeditdeleteorderguide.Update(DataGridViewToDataTable(dgadditem))
                    mainform.runcommand("insert into actionszebra (actionzebra,userzebra,datezebra) values ('Made Changes to Data','" & mainform.curruser & "','" & Date.Now & "')")
                    MsgBox("Edits Saved")
                    loaddatagrid("SELECT * FROM thierorderguide WHERE Company = '" & lbCompanies.SelectedItem.ToString & "'")
                Catch ex As Exception
                    mainform.errorwrite(ex.ToString & " saved edits to company ordre guide")
                End Try

Open in new window


Here is the code for taking a datagridview to a table:
    Public Shared Function DataGridViewToDataTable(ByVal dtg As DataGridView) As DataTable
        Try
            Dim dt As New DataTable
            Dim row As DataRow
            Dim TotalDatagridviewColumns As Integer = dtg.ColumnCount - 1
            'Add Datacolumn
            For Each c As DataGridViewColumn In dtg.Columns
                Dim idColumn As DataColumn = New DataColumn()
                idColumn.ColumnName = c.Name
                dt.Columns.Add(idColumn)
            Next
            'Now Iterate thru Datagrid and create the data row
            For Each dr As DataGridViewRow In dtg.Rows
                'Iterate thru datagrid
                row = dt.NewRow 'Create new row
                'Iterate thru Column 1 up to the total number of columns
                For cn As Integer = 0 To TotalDatagridviewColumns
                    row.Item(cn) = IfNullObj(dr.Cells(cn).Value) ' This Will handle error datagridviewcell on NULL Values
                Next
                'Now add the row to Datarow Collection
                dt.Rows.Add(row)
            Next
            'Now return the data table
            Return dt
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

Open in new window

0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 37853663
Why are you doing this bit in 3rd code snippet? Off course you are creating new rows which would be inserted instead of updated.
0
 

Author Comment

by:Millkind
ID: 37855691
Okay this is what i feared.  Since im using a datagridview and not a datagrid there is not table bound from the database to the source.  Is that why i can't update?
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

778 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