Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

update creating dups

Posted on 2012-04-11
4
245 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

839 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