-
Aug 19th, 2013, 05:00 AM
#1
Thread Starter
New Member
Updating tables in a database, why does this single line of code make it work?
Hi, Im new to VB.NET and I have developed and application that works fine but in order to make it work as expected I added a line of code but I am unsure why it makes it work. I would really like a good understanding of whats happening and why redefining the dataview is making the database be updated as expected. I have posted the relevant code below, any help would be much appreciated
Heres where I populate my datatables.
Public Sub FillTables()
'Fills datatables with data from the database
Try
CountryRuleDataAdapter.Fill(RulesDataset, "database.PSCT_CRULE")
EncRuleDataAdapter.Fill(RulesDataset, "database.PSCT_ENCRULE")
CountryRuleTable = RulesDataset.Tables("database.PSCT_CRULE")
EncRuleTable = RulesDataset.Tables("database.PSCT_ENCRULE")
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Heres where I setup the dataview when loading a particular form
Private Sub Countryrulesform_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'sets up the dataview when form opens
CountryRuleEditingView = New DataView(CountryRuleTable)
CountryRuleEditingView.AllowEdit = True
CountryRuleEditingView.AllowDelete = False
CountryRuleEditingView.AllowNew = False
End Sub
Heres where I populate the dataview using a row filter so the user can make changes to the data
Public Sub CountryPopulate(ByVal Ccode As String)
'runs a rowfilter to populate the view then populates the form with the results
CountryRuleEditingView.RowFilter = "Country_Code = '" & Ccode & "'"
If CountryRuleEditingView.Count = 0 Then
MessageBox.Show("Country " & Ccode & " not found!")
Else
CountryRulesWindow.CodeTextBox.Text = CountryRuleEditingView.Item(0).Row.Field(Of String)(0)
CountryRulesWindow.NewChartCombo.Text = CountryRuleEditingView.Item(0).Row.Field(Of String)(1)
CountryRulesWindow.NewEditionCombo.Text = CountryRuleEditingView.Item(0).Row.Field(Of String)(2)
CountryRulesWindow.UpdateCombo.Text = CountryRuleEditingView.Item(0).Row.Field(Of String)(3)
CountryRulesWindow.NewChartCombo.Enabled = True
CountryRulesWindow.NewEditionCombo.Enabled = True
CountryRulesWindow.UpdateCombo.Enabled = True
End If
End Sub
Heres where I edit the values in the dataview once the user has made a change, from my understanding this also edits the datatables, I know the data table has been edited after this as I have another form showing the datatable and the edit has happened.
Private Sub NewChartCombo_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewChartCombo.SelectedValueChanged
'when a value is changed in the form the corresponding value is changed in the view/datatable
CountryRuleEditingView(0)("New_Cell") = NewChartCombo.Text
End Sub
Private Sub NewEditionCombo_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewEditionCombo.SelectedValueChanged
'when a value is changed in the form the corresponding value is changed in the view which amends datatable
CountryRuleEditingView(0)("New_Edition") = NewEditionCombo.Text
End Sub
Private Sub UpdateCombo_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateCombo.SelectedValueChanged
'when a value is changed in the form the corresponding value is changed in the view which amends datatable
CountryRuleEditingView(0)("UPDT") = UpdateCombo.Text
End Sub
I commit the changes when closing the form using these 2 Subs but without the line CountryRuleEditingView = New DataView(CountryRuleTable) the update doesnt happen. Why does resseting the dataview make it work when Im updating from the datables not the view?
Private Sub Countryrulesform_close(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.FormClosing
'Upon closing resets variables and uploads changes to tables to the database
CountryRuleTableForm = Nothing
IsTableOpen = False
Try
CountryRuleEditingView = New DataView(CountryRuleTable) 'This is the line that is frustrating me!
OpenConnection()
Updatetables()
CloseConnection()
Catch ex As Exception
MessageBox.Show(String.Format("There was a problem updating the database.{0}" & ex.ToString, Environment.NewLine))
Finally
SDRAConnect.Close()
End Try
End Sub
Public Sub Updatetables()
'Applies edits to datatables back to database
Try
CountryRuleDataAdapter.Update(RulesDataset, "database.PSCT_CRULE")
EncRuleDataAdapter.Update(RulesDataset, "database.PSCT_ENCRULE")
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
So in my understanding when I update tables it updates the database by looking at modified rows on the datatable, the dataview should not be involved, the dataview is just used so the user can edit the datatables so why does it need the view to be redefined using CountryRuleEditingView = New DataView(CountryRuleTable) for the database to be updated?
Any help explaining this to me is very much appreciated.
Jav
Last edited by Javrel; Aug 19th, 2013 at 05:52 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|