Results 1 to 10 of 10

Thread: Saving To A Database

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    15

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    15

    Re: Saving To A Database

    Quote 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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving To A Database

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    15

    Re: Saving To A Database

    Quote 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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving To A Database

    VB Code:
    1. Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    2.         If Me.DataSet1.HasChanges() Then
    3.             Select Case MessageBox.Show("Would you like to save the changes?", _
    4.                                         "Save Changes", _
    5.                                         MessageBoxButtons.YesNoCancel, _
    6.                                         MessageBoxIcon.Question)
    7.                 Case Windows.Forms.DialogResult.Yes
    8.                     'Save changes and allow the form to close.
    9.                 Case Windows.Forms.DialogResult.No
    10.                     'Allow the form to close without saving changes.
    11.                 Case Windows.Forms.DialogResult.Cancel
    12.                     'Do not save changes but do not allow the form to close.
    13.                     e.Cancel = True
    14.             End Select
    15.  
    16.         End If
    17.     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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    15

    Re: Saving To A Database

    Quote Originally Posted by jmcilhinney
    VB Code:
    1. Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    2.         If Me.DataSet1.HasChanges() Then
    3.             Select Case MessageBox.Show("Would you like to save the changes?", _
    4.                                         "Save Changes", _
    5.                                         MessageBoxButtons.YesNoCancel, _
    6.                                         MessageBoxIcon.Question)
    7.                 Case Windows.Forms.DialogResult.Yes
    8.                     'Save changes and allow the form to close.
    9.                 Case Windows.Forms.DialogResult.No
    10.                     'Allow the form to close without saving changes.
    11.                 Case Windows.Forms.DialogResult.Cancel
    12.                     'Do not save changes but do not allow the form to close.
    13.                     e.Cancel = True
    14.             End Select
    15.  
    16.         End If
    17.     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

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    15

    Re: Saving To A Database

    Quote 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

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    2.         If Me.DataSet1.HasChanges() Then
    3.             Select Case MessageBox.Show("Would you like to save the changes?", _
    4.                                         "Save Changes", _
    5.                                         MessageBoxButtons.YesNoCancel, _
    6.                                         MessageBoxIcon.Question)
    7.                 Case Windows.Forms.DialogResult.Yes
    8.                     'Save changes and allow the form to close.
    9.                     [color=red][B][U]SaveData1()[/U][/B][/color] 'This is where the data gets saved.
    10.                 Case Windows.Forms.DialogResult.No
    11.                     'Allow the form to close without saving changes.
    12.                 Case Windows.Forms.DialogResult.Cancel
    13.                     'Do not save changes but do not allow the form to close.
    14.                     e.Cancel = True
    15.             End Select
    16.  
    17.         End If
    18.     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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width