title: how datatable can be updated to database in vb.net
body: <p>I am trying to solve a problem but fail till now</p>
  
  <p>my datagrid view is bound to    BindingSource but when i delete some row from my datagriview the update(table) command executes but does not make any change to my database</p>
  
  <p>I am loading datagridview in this code</p>
  
  <pre><code>Private Sub loadSalueUnits(ByVal itemName As String)
  
      table.Clear()
      If (itemName &lt;&gt; "0") Then
          ItemID = db.GetVal("select RawItemId from tblRawItem where RawItemName='" &amp; itemName &amp; "'")
          dsSaleUnit = db.GetData("SELECT * from tblItemSaleUnits where RawItemId=" &amp; ItemID &amp; "")
      Else
          ItemID = "0"
      End If
      loadComboBox()
  
      'SaleUnit
      If (ItemID = "0") Then
          Try
  
              Dim objNetworksetting As New NetworkSetting
              Dim source As String = objNetworksetting.ReadConnectionXML()
              Dim connectionString As String = source
  
              Dim selectCommand As String = "SELECT * from tblItemSaleUnits where RawItemId=" &amp; ItemID &amp; ""
              Me.dataAdapter = New OdbcDataAdapter(selectCommand, connectionString)
              Dim commandBuilder As New OdbcCommandBuilder(Me.dataAdapter)
              table.Locale = System.Globalization.CultureInfo.InvariantCulture
              Me.dataAdapter.Fill(table)
              Me.bindingSource.DataSource = table
              data.Locale = System.Globalization.CultureInfo.InvariantCulture
              dgvSaleUnit.DataSource = Me.bindingSource
              SaleUnit.DataPropertyName = "Unit"
              UnitDifference.DataPropertyName = "UnitDifference"
              For i As Integer = 0 To table.Rows.Count - 1
                  dgvSaleUnit.Rows(i).Cells("UnitDifference").Value() = table.Rows.Item(i)(3)
                  dgvSaleUnit.Rows(i).Cells("SaleUnit").Value() = table.Rows.Item(i)(2)
                  dgvSaleUnit.Rows(i).Cells("SalePrice").Value() = table.Rows.Item(i)(4)
              Next
  
              Me.dgvSaleUnit.AutoResizeColumns( _
              DataGridViewAutoSizeColumnsMode.AllCells)
              dgvSaleUnit.Columns(2).Visible = False
              dgvSaleUnit.Columns(3).Visible = False
              dgvSaleUnit.Columns(4).Visible = False
              dgvSaleUnit.Columns(5).Visible = False
              dgvSaleUnit.Columns(6).Visible = False
          Catch ex As Exception
              MessageBox.Show(ex.Message)
          End Try
  
      ElseIf dsSaleUnit.Tables(0).Rows.Count &lt;&gt; 0 Then
  
          Try
  
              Dim objNetworksetting As New NetworkSetting
              Dim source As String = objNetworksetting.ReadConnectionXML()
              Dim connectionString As String = source
  
              Dim selectCommand As String = "SELECT * from tblItemSaleUnits where RawItemId=" &amp; ItemID &amp; ""
              Me.dataAdapter = New OdbcDataAdapter(selectCommand, connectionString)
              Dim commandBuilder As New OdbcCommandBuilder(Me.dataAdapter)
              table.Locale = System.Globalization.CultureInfo.InvariantCulture
              Me.dataAdapter.Fill(table)
              Me.bindingSource.DataSource = table
              data.Locale = System.Globalization.CultureInfo.InvariantCulture
              dgvSaleUnit.DataSource = Me.bindingSource
              SaleUnit.DataPropertyName = "Unit"
              UnitDifference.DataPropertyName = "UnitDifference"
              salePrice.DataPropertyName = "SalePrice"
              For i As Integer = 0 To table.Rows.Count - 1
                  dgvSaleUnit.Rows(i).Cells("UnitDifference").Value() = table.Rows.Item(i)(3)
                  dgvSaleUnit.Rows(i).Cells("SaleUnit").Value() = table.Rows.Item(i)(2)
                  dgvSaleUnit.Rows(i).Cells("SalePrice").Value() = table.Rows.Item(i)(4)
              Next
  
              Me.dgvSaleUnit.AutoResizeColumns( _
              DataGridViewAutoSizeColumnsMode.AllCells)
              dgvSaleUnit.Columns(2).Visible = False
              dgvSaleUnit.Columns(3).Visible = False
              dgvSaleUnit.Columns(4).Visible = False
              dgvSaleUnit.Columns(5).Visible = False
              dgvSaleUnit.Columns(6).Visible = False
  
          Catch ex As Exception
              MessageBox.Show(ex.Message)
          End Try
      End If
  
  End Sub
  </code></pre>
  
  <p>and after deleting row from datagridiew update in this code</p>
  
  <pre><code>Dim objNetworksetting As New NetworkSetting
              Dim source As String = objNetworksetting.ReadConnectionXML()
              Dim connectionString As String = source
              Dim con As New OdbcConnection(connectionString)
              con.Open()
  
              Dim selectCommand As String = "SELECT * from tblItemSaleUnits"
              Me.dataAdapter = New OdbcDataAdapter(selectCommand, connectionString)
              Dim commandBuilder As New OdbcCommandBuilder(Me.dataAdapter)
              table.Locale = System.Globalization.CultureInfo.InvariantCulture
              dataAdapter.Fill(table)
  
  
              dataAdapter.UpdateCommand = commandBuilder.GetUpdateCommand()
              table = Me.bindingSource.DataSource
              table.AcceptChanges()
              Me.dataAdapter.Update(table)
  </code></pre>
  
  <p>it executes but does ot make any change to database</p>