Solved

update creating dups

Posted on 2012-04-11
4
242 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now