|
-
Apr 19th, 2006, 08:10 PM
#1
Thread Starter
New Member
Saving To A Database
Hi Guys,
There seems to be a little problem with my database application i was wondering if you guys could help. But basically in my application - when you update or add something into the application - it updates it while its still running but if you close down the application it doesnt actually write the changes to the database. The code im using is:
Friend Function SaveData1() As Integer
If IsUpdating Then
Return 0
End If
Dim rowsSaved As Integer = 0
'try to get changes and save to database
Try
'only push update to database if changes are detected
If Me.ExpertDataSet.HasChanges Then
IsUpdating = True
rowsSaved = Me.DisorderTableAdapter.Update(Me.ExpertDataSet.Disorder)
End If
Catch ex As Exception
MsgBox(String.Format("There was a problem saving data: {0}", ex.Message))
My.Application.Log.WriteException(ex)
Finally
IsUpdating = False
End Try
Return rowsSaved
-------------------------
i have tried several others but it doesnt seem to work!!
any ideas? thanks
-
Apr 19th, 2006, 08:26 PM
#2
Re: Saving To A Database
If you want the changes saved when the application closes then you have to tell it to do so. You haven't specified which VB version you're using (*sigh* please use the radio buttons provided to specify your IDE/Framework version when creating a thread) so I'm not sure what the best option is. You can handle the Closing (2003) or FormClosing (2005) event of the form and prompt the user to save there if there are pending changes. In 2005 you also have the Shutdown event of the application. Note that if you call Application.Exit in 2003 then the Closing and Closed events are not raised on any of your forms, so you must call Close on the main form to ensure that the evnts are raised. In 2005 the FormClosing and FormClosed events will still be raised if you call Application.Exit.
-
Apr 20th, 2006, 11:21 AM
#3
Thread Starter
New Member
Re: Saving To A Database
 Originally Posted by jmcilhinney
If you want the changes saved when the application closes then you have to tell it to do so. You haven't specified which VB version you're using (*sigh* please use the radio buttons provided to specify your IDE/Framework version when creating a thread) so I'm not sure what the best option is. You can handle the Closing (2003) or FormClosing (2005) event of the form and prompt the user to save there if there are pending changes. In 2005 you also have the Shutdown event of the application. Note that if you call Application.Exit in 2003 then the Closing and Closed events are not raised on any of your forms, so you must call Close on the main form to ensure that the evnts are raised. In 2005 the FormClosing and FormClosed events will still be raised if you call Application.Exit.
Hi sorry, im using VB 2005 express addition and sql 2005 express. I have a exit button on my program...should i add the code in there to save it?
thanks
-
Apr 20th, 2006, 09:29 PM
#4
Re: Saving To A Database
 Originally Posted by jmcilhinney
You can handle the ... FormClosing ... event of the form and prompt the user to save there if there are pending changes.
This will allow for the user closing the form by the button in the title bar too.
-
Apr 21st, 2006, 06:26 AM
#5
Thread Starter
New Member
Re: Saving To A Database
 Originally Posted by jmcilhinney
This will allow for the user closing the form by the button in the title bar too.
so the code i typed above include it in the button, so when the user exits it should be saved?
does that code look right to you?
thanks
-
Apr 21st, 2006, 07:28 AM
#6
Re: Saving To A Database
VB Code:
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If Me.DataSet1.HasChanges() Then
Select Case MessageBox.Show("Would you like to save the changes?", _
"Save Changes", _
MessageBoxButtons.YesNoCancel, _
MessageBoxIcon.Question)
Case Windows.Forms.DialogResult.Yes
'Save changes and allow the form to close.
Case Windows.Forms.DialogResult.No
'Allow the form to close without saving changes.
Case Windows.Forms.DialogResult.Cancel
'Do not save changes but do not allow the form to close.
e.Cancel = True
End Select
End If
End Sub
You would then have a separate method that actually saved the changes, which you could call from this event handler or anywhere else.
-
Apr 21st, 2006, 10:01 AM
#7
Thread Starter
New Member
Re: Saving To A Database
 Originally Posted by jmcilhinney
VB Code:
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If Me.DataSet1.HasChanges() Then
Select Case MessageBox.Show("Would you like to save the changes?", _
"Save Changes", _
MessageBoxButtons.YesNoCancel, _
MessageBoxIcon.Question)
Case Windows.Forms.DialogResult.Yes
'Save changes and allow the form to close.
Case Windows.Forms.DialogResult.No
'Allow the form to close without saving changes.
Case Windows.Forms.DialogResult.Cancel
'Do not save changes but do not allow the form to close.
e.Cancel = True
End Select
End If
End Sub
You would then have a separate method that actually saved the changes, which you could call from this event handler or anywhere else.
cheers man il try this out thanks! the seperate method that saves the changes, is that what i have written above? would that work? thanks
-
Apr 21st, 2006, 10:03 AM
#8
Re: Saving To A Database
Indeed. You can then call that method from the FormClosing event handler or a Button's Click event handler or elsewhere.
-
Apr 22nd, 2006, 03:42 PM
#9
Thread Starter
New Member
Re: Saving To A Database
 Originally Posted by jmcilhinney
Indeed. You can then call that method from the FormClosing event handler or a Button's Click event handler or elsewhere.
hey man
tried that code and it didnt work. When i closed it down, it didnt save to my database...this is strange
-
Apr 22nd, 2006, 07:25 PM
#10
Re: Saving To A Database
My code doesn't save a thing, as I said. If the user selects 'Yes' then you need to call your method that actually saves the data.
VB Code:
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If Me.DataSet1.HasChanges() Then
Select Case MessageBox.Show("Would you like to save the changes?", _
"Save Changes", _
MessageBoxButtons.YesNoCancel, _
MessageBoxIcon.Question)
Case Windows.Forms.DialogResult.Yes
'Save changes and allow the form to close.
[color=red][B][U]SaveData1()[/U][/B][/color] 'This is where the data gets saved.
Case Windows.Forms.DialogResult.No
'Allow the form to close without saving changes.
Case Windows.Forms.DialogResult.Cancel
'Do not save changes but do not allow the form to close.
e.Cancel = True
End Select
End If
End Sub
If you've done that and the data is not saved to the database then it's your code to do the saving that's faulty and you need to debug that.
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
|