I have 2 simple forms. The first one had a datagrid that displays movie titles. When you click on a row header the second form opens and displays the movie details. Both forms have an update button on them. for the first name you can change the movie title and click the update button to send the changes back to the dataset. The second form should work the same way only it is not a datagrid but it is text boxes that are bound to the dataset. I use the exact same code for both update buttons however, when I change some of the details in the 2nd form and hit the update button the changes are not made to the dataset. Does anyone know what the cause of this may be? I'll put some sample code below.
And the second form that doesn't update properly.
Code:
Public Class Form1
Inherits System.Windows.Forms.Form
Private bmBind As BindingManagerBase
Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
SqlDataAdapter1.Fill(DSmtgHW6)
'DSmtgHW6.Movies.Clear()
bmBind = BindingContext(DSmtgHW6, "Movies")
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
If DSmtgHW6.HasChanges Then
SqlDataAdapter1.Update(DSmtgHW6)
MessageBox.Show("The database has been updated, have a nice day", "Dabase Update Window")
End If
End Sub
Private Sub DataGrid1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseUp
Try
' Declare the variable myHitTest for the hit test method
Dim myHitTest As DataGrid.HitTestInfo
Dim frmMovieDetails As New frmMovieDetails
' HitTest method with the x and y properties starts here
myHitTest = DataGrid1.HitTest(e.X, e.Y)
If myHitTest.Type = DataGrid1.HitTestType.RowHeader Then
frmMovieDetails.MovieID = bmBind.Current("MovieID")
frmMovieDetails.ShowDialog()
End If
Catch ex As Exception
MessageBox.Show("Please click on the Show Movies button to view movie selections", "Selection Error")
End Try
End Sub
End Class
And the second form that doesn't update properly.
Code:
Public Class frmMovieDetails
Inherits System.Windows.Forms.Form
Private bmBind As BindingManagerBase
Private intMovieID As Integer
Property MovieID() As Integer
Get
Return intMovieID
End Get
Set(ByVal Value As Integer)
intMovieID = Value
End Set
End Property
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub frmMovieDetails_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SqlDataAdapter1.Fill(DSmtghw6)
bmBind = BindingContext(DSmtghw6, "Movies")
DSmtghw6.Movies.DefaultView.Sort = "MovieID"
bmBind.Position = DSmtghw6.Movies.DefaultView.Find(intMovieID)
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Try
bmBind.EndCurrentEdit()
If DSmtghw6.HasChanges Then
SqlDataAdapter1.Update(DSmtghw6, "movies")
MessageBox.Show("The database has been updated, have a nice day", "Database Update Window")
End If
Catch ex As DBConcurrencyException
MessageBox.Show("Someone else is working on this database")
End Try
End Sub
End Class