determine after using addnew

  • Thread starter Thread starter jaYPee
  • Start date Start date
J

jaYPee

is there a way to know if the form is edited or not after calling
addnew? Me.BindingContext(DsStudentCourse1, "Students").AddNew()

because i got an error after closing the form. because in my form
closing event i have a code to call the EndCurrentEdit to determine if
the dataset has changes or not. here is my code in closing event

Private Sub Students_Closing(ByVal sender As Object, ByVal e
As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Me.BindingContext(DsStudentCourse1,
"Students").EndCurrentEdit()
Try
If DsStudentCourse1.HasChanges Then
If MessageBox.Show("There are unsave changes in
the Form, Yes to Save, No to Discard and exit?", "Save changes",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes
Then
Save_Record()
Else
Me.BindingContext(DsStudentCourse1,
"Students").CancelCurrentEdit()
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

the problem is in the Me.BindingContext(DsStudentCourse1,
"Students").EndCurrentEdit() line of code after calling addnew and
then closing the form without editing the form.

the error generated is "Column 'IDNo' does not allow nulls." because
IDNo is a primary key.

thanks in advance for any help.
 
I don't know of a way to determine its state, but why not call cancel
current edit. you can tell if the dataset has changes or not by using
DataSet.HasChanges(); which returns a bool. If it doesn't go ahead and
cancel the current edit (depending on how you want the app to behave this
should do it for you).
 
thanks for the reply. however DataSet.HasChanges() is only applicable
when the control is datagrid. don't know if i'm wrong or not. but as i
have experience eventhough i edit the textbox in my form and close the
form and call DataSet.HasChanges() is it returns false.
 
Actually, that's not true. HasChanges has nothign to do with a
Datagrid...the datagrid is only incidental in that it's one mechanism to
edit the data. If HasChanges is false, your update isn't going to do
anything. TO prove it, trap the StateChanged event of your connection. Put
a messagebox in it. Then, right after dataadapter.fill the very next line
(test HasChanges, it will be false). Next, create a loop to 1000 or 10000
or whatever suits you. Then, in the loop call daadapter.Update. You'll
notice that your messagebox never shows. That's because the connection
(which is closed at this point) doesn't open any more (while in the loop
calling Update) which proves it's not sending anything to the db. Next
line, Change a value of the datatable. call dataAdapter.Update again, your
messagebox will show this time. It will show twice on fill, once when it
opens, once when it closes.

hasChanges is a property of the dataset not the grid.
 
Back
Top