Results 1 to 8 of 8

Thread: Dataset does not update database

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    6

    Unhappy Dataset does not update database

    Using VB.net 2.0

    I have created a form in to which i have dragged a details view of a table from a database.



    When i add a new client and save, close the program and open it, the new client is present, if i close VS2005 and open the DB there is no new record, open VS2005 again and the new client record has disappeared.

    I think the data set is not updating the database. I have the database properties set to copy if newer.

    I have tried using the built in toolstrip to the same outcome.

    The code for my form:

    Code:
    
    Public Class frmClient
    
        Private Sub frmClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'DsGT2000New.tblClient' table. You can move, or remove it, as needed.
            Me.TblClientTableAdapter.Fill(Me.DsGT2000New.tblClient)
    
            'Check if User name = Admin if so enable the delete button
            If frmPass.txtUserName.Text = "Admin" Then
                btnDel.Enabled = True
            End If
    
        End Sub
        
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            'close program
            Me.Close()
            End
        End Sub
                   
     
        Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
            Me.TblClientBindingSource.MovePrevious()
        End Sub
        '-----------------------------------------------------------------------------------------------------------------------------
        '-----------------------------------------------------------------------------------------------------------------------------
        'Procedure              :btnNext                 
        'Project                :GT2000Project
        'Version                :1.0 
        'Date                   :18/04/2008
        'Author                 :Craig Hendley
        'Description            :button to take the user to the next record
        '                       :
        '-----------------------------------------------------------------------------------------------------------------------------
        '-----------------------------------------------------------------------------------------------------------------------------
        Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
            Me.TblClientBindingSource.MoveNext()
        End Sub
        '-----------------------------------------------------------------------------------------------------------------------------
        '-----------------------------------------------------------------------------------------------------------------------------
        'Procedure              :btnAddNew                 
        'Project                :GT2000Project
        'Version                :1.0 
        'Date                   :18/04/2008
        'Author                 :Craig Hendley
        'Description            :button to start a blank record for new input
        '                       :
        '-----------------------------------------------------------------------------------------------------------------------------
        '-----------------------------------------------------------------------------------------------------------------------------
        Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
            Me.TblClientBindingSource.AddNew()
        End Sub
        '-----------------------------------------------------------------------------------------------------------------------------
        '-----------------------------------------------------------------------------------------------------------------------------
        'Procedure              :btnDel                 
        'Project                :GT2000Project
        'Version                :1.0 
        'Date                   :18/04/2008
        'Author                 :Craig Hendley
        'Description            :button to delete record
        '                       :
        '-----------------------------------------------------------------------------------------------------------------------------
        '-----------------------------------------------------------------------------------------------------------------------------
        Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
            MessageBox.Show("Are You Sure You Wish To Delete This Record?", "Confirm ", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
    
        End Sub
        '-----------------------------------------------------------------------------------------------------------------------------
        '-----------------------------------------------------------------------------------------------------------------------------
        'Procedure              :CIDTextBox_KeyPress                 
        'Project                :GT2000Project
        'Version                :1.0 
        'Date                   :18/04/2008
        'Author                 :Craig Hendley
        'Description            :Limits allowed character presses to "C", Numeric values, tab and backspace
        '                       :
        '-----------------------------------------------------------------------------------------------------------------------------
        '-----------------------------------------------------------------------------------------------------------------------------
        Private Sub CIDTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles CIDTextBox.KeyPress
            If (e.KeyChar <> "C") Then
                If (e.KeyChar < "0" Or e.KeyChar > "9") Then
                    If (AscW(e.KeyChar) <> AscW(ControlChars.Back) AndAlso AscW(e.KeyChar) <> AscW(ControlChars.Tab)) Then
                        e.Handled = True
                    End If
                End If
            End If
        End Sub
        '-----------------------------------------------------------------------------------------------------------------------------
        '-----------------------------------------------------------------------------------------------------------------------------
        'Procedure              :DOBDateTimePicker_ValueChanged                 
        'Project                :GT2000Project
        'Version                :1.0 
        'Date                   :18/04/2008
        'Author                 :Craig Hendley
        'Description            :Limits allowed character presses to "C", Numeric values, tab and backspace
        '                       :
        '-----------------------------------------------------------------------------------------------------------------------------
        '-----------------------------------------------------------------------------------------------------------------------------
    
        Private Sub DOBDateTimePicker_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DOBDateTimePicker.ValueChanged
            Dim d As Date = Date.Now
            d = New Date(d.Year - 18, d.Month, d.Day)
    
    
            If DOBDateTimePicker.Value <= d Then
                Me.err1.SetError(DOBDateTimePicker, "")
    
            Else
    
                Me.err1.SetError(DOBDateTimePicker, "Must Be over 18!")
                Me.DOBDateTimePicker.Focus()
    
            End If
        End Sub
        '-----------------------------------------------------------------------------------------------------------------------------
        '-----------------------------------------------------------------------------------------------------------------------------
        'Procedure              :btnSav                
        'Project                :GT2000Project
        'Version                :1.0 
        'Date                   :18/04/2008
        'Author                 :Craig Hendley
        'Description            :button to save date in the dataset to the database
        '                       :
        '-----------------------------------------------------------------------------------------------------------------------------
        '-----------------------------------------------------------------------------------------------------------------------------
        Private Sub btnSav_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSav.Click
            Me.Validate()
            Me.TblClientBindingSource.EndEdit()
            Me.TblClientTableAdapter.Update(Me.DsGT2000New.tblClient)
        End Sub
    End Class



    This is really worrying me, i need to sort it out ASAP so anything you guys can advise me on would be much appreciated
    Last edited by ABOU; May 2nd, 2008 at 01:44 PM.

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Dataset does not update database

    Does your table have a primary key defined?

    I've never used a tableAdapter I'm afraid, I find the graphical method of linking data far too confusing.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Dataset does not update database

    Is your connection string referencing your MDF in the |DataDirectory|?

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    6

    Re: Dataset does not update database

    My table does have a primary key.

    Mendhak: How would i check if the connection string is referencing the MDF in the |DataDirectory|?

    I dont know what an MDF is, or where to find the DataDirectory sorry

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

    Re: Dataset does not update database

    When you add a database to your project it creates an MDF file and places it in your source folder, i.e. the project folder containing all your VB code files.

    When you build your project, which happens each time you run it in the debugger, that MDF file is copied to the output folder along with your compiled EXE. It is that copy that your application interacts with.

    By default, the database file is copied to the output folder EVERY TIME you build, therefore all the changes you make during one debugging session will be erased as soon as you build again.

    Follow the first link in my signature for an explanation of how to change this behaviour.

    Note also that your TableAdapters each wrap an SqlConnection that has a ConnectionString that controls how it connects to your database. That connection string contains an AttachDbFileName property whose value is the path to your database file. By default it will be something like "|DataDirectory|\MyDatabase.mdf". The "|DataDirectory|" part is interpreted by the Framework at run time and replaced with the actual folder path. In this way you don't have to change your connection string no matter where you install your application.
    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

  6. #6
    New Member
    Join Date
    May 2008
    Posts
    6

    Re: Dataset does not update database

    Quote Originally Posted by jmcilhinney
    When you add a database to your project it creates an MDF file and places it in your source folder, i.e. the project folder containing all your VB code files.

    When you build your project, which happens each time you run it in the debugger, that MDF file is copied to the output folder along with your compiled EXE. It is that copy that your application interacts with.

    By default, the database file is copied to the output folder EVERY TIME you build, therefore all the changes you make during one debugging session will be erased as soon as you build again.

    Follow the first link in my signature for an explanation of how to change this behaviour.

    Note also that your TableAdapters each wrap an SqlConnection that has a ConnectionString that controls how it connects to your database. That connection string contains an AttachDbFileName property whose value is the path to your database file. By default it will be something like "|DataDirectory|\MyDatabase.mdf". The "|DataDirectory|" part is interpreted by the Framework at run time and replaced with the actual folder path. In this way you don't have to change your connection string no matter where you install your application.

    i have the seem ISSUE but seting Copy to Output Directory to Copy if newer not helps

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

    Re: Dataset does not update database

    Try setting it to Do Not Copy and see if that works.
    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

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    6

    Re: Dataset does not update database

    Thank you, you have no idea the panic was in just then!!

    I did not know that the database was copied again from the application directory to the bin folder.

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