Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Why dataset doesn't update the database? What am I missing?

Why dataset doesn't update the database? What am I missing?

Scheduled Pinned Locked Moved Visual Basic
databasehelpquestionannouncement
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JUNEYT
    wrote on last edited by
    #1

    Hello, My database is not getting updated via dataset even all the fields in the form have been binded to validation occurs. When I push the button I want to transfer dataset to database. However when I browse the database to see if there are the most recent changes from dataset, There is no record! What am I missing? I am not getting any error messages.

    Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim Row As DataRow = ProjectsDataSet1.Tables("Projects").NewRow()
    
      
        Try
            Me.Validate()
            Me.ProjelerBindingSource.EndEdit()
            Me.ProjelerTableAdapter.Update(Me.ProjectsDataSet1.Tables("Projects"))
    
            MessageBox.Show(ProjectName.Text & vbCrLf & vbCrLf & "identified!")
        Catch ex As Exception
            MsgBox("Identification is not successful.")
        End Try
    
        Me.Close()
    
    
    End Sub
    

    What a curious mind needs to discover knowledge is noting else than a pin-hole.

    H 1 Reply Last reply
    0
    • J JUNEYT

      Hello, My database is not getting updated via dataset even all the fields in the form have been binded to validation occurs. When I push the button I want to transfer dataset to database. However when I browse the database to see if there are the most recent changes from dataset, There is no record! What am I missing? I am not getting any error messages.

      Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      
          Dim Row As DataRow = ProjectsDataSet1.Tables("Projects").NewRow()
      
        
          Try
              Me.Validate()
              Me.ProjelerBindingSource.EndEdit()
              Me.ProjelerTableAdapter.Update(Me.ProjectsDataSet1.Tables("Projects"))
      
              MessageBox.Show(ProjectName.Text & vbCrLf & vbCrLf & "identified!")
          Catch ex As Exception
              MsgBox("Identification is not successful.")
          End Try
      
          Me.Close()
      
      
      End Sub
      

      What a curious mind needs to discover knowledge is noting else than a pin-hole.

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      It is difficult to give a precise answer, as there can be many causes for your problem. Are you trying to add a row to an empty table? Trying to update existing data? Or what? You need to restate your question more clearly.

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

      J 1 Reply Last reply
      0
      • H Henry Minute

        It is difficult to give a precise answer, as there can be many causes for your problem. Are you trying to add a row to an empty table? Trying to update existing data? Or what? You need to restate your question more clearly.

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

        J Offline
        J Offline
        JUNEYT
        wrote on last edited by
        #3

        It is an empty table inside the dataset. There are no records. I have a form and there are fields binded to dataset which means if a validation occurs, the entries will be automatically added in to table. However it does neither add a record nor at the end of update it does update the table in database (not in dataset).

        What a curious mind needs to discover knowledge is noting else than a pin-hole.

        D H 2 Replies Last reply
        0
        • J JUNEYT

          It is an empty table inside the dataset. There are no records. I have a form and there are fields binded to dataset which means if a validation occurs, the entries will be automatically added in to table. However it does neither add a record nor at the end of update it does update the table in database (not in dataset).

          What a curious mind needs to discover knowledge is noting else than a pin-hole.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Does the SQL that was used to return the data from the database return that table's Primary Key column?

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008
          But no longer in 2009...

          1 Reply Last reply
          0
          • J JUNEYT

            It is an empty table inside the dataset. There are no records. I have a form and there are fields binded to dataset which means if a validation occurs, the entries will be automatically added in to table. However it does neither add a record nor at the end of update it does update the table in database (not in dataset).

            What a curious mind needs to discover knowledge is noting else than a pin-hole.

            H Offline
            H Offline
            Henry Minute
            wrote on last edited by
            #5

            In the code you posted, you create a new DataRow, but you never do anything with it.

            Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            
                Dim Row As DataRow = ProjectsDataSet1.Tables("Projects").NewRow()    // <============================ HERE ==================
                ........................
                ........................
                ........................
            
                Me.Close()    // <======================= SEE LATER IN POST =========================
            End Sub
            

            To try to get this working I will suggest a solution using the code that you already have.

            Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            
                Dim Row As DataRow = ProjectsDataSet1.Tables("Projects").NewRow()
                Row("FirstName") = textBoxFName.Text    // <============ OBVIOUSLY, REPLACE "FirstName", "LastName" WITH YOUR COLUMN NAMES
                Row("LastName") = textBoxLName.Text     // <============ AND textBoxWhatever WITH YOUR TEXTBOXES
            
                ProjectsDataSet1.Tables("Projects").Rows.Add(Row)   // <========== THEN PUT THE NEWLY FILLED ROW INTO THE TABLE
              
                Try
                    Me.Validate()
                    Me.ProjelerBindingSource.EndEdit()
                    Me.ProjelerTableAdapter.Update(Me.ProjectsDataSet1.Tables("Projects"))
            
                    MessageBox.Show(ProjectName.Text & vbCrLf & vbCrLf & "identified!")
                Catch ex As Exception
                    MsgBox("Identification is not successful.")
                End Try
            
                Me.Close()
            
            
            End Sub
            

            I have not tested this but it should work. Give it a try and get back, if not. You do realise that your Me.Close() line will close the form, don't you? Possibly a better way to do this is to give the users a 'New' button. When this is clicked, add a new row to the table and leave it blank. Then since your data entry controls are bound to the table they will be looking at this empty row so that when data is entered into the controls it will also be added to the table. You could then use your Button1_Click as it is except for the NewRow() line, which will no longer be needed. Hope this makes sense, and helps. :)

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            J 1 Reply Last reply
            0
            • H Henry Minute

              In the code you posted, you create a new DataRow, but you never do anything with it.

              Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
              
                  Dim Row As DataRow = ProjectsDataSet1.Tables("Projects").NewRow()    // <============================ HERE ==================
                  ........................
                  ........................
                  ........................
              
                  Me.Close()    // <======================= SEE LATER IN POST =========================
              End Sub
              

              To try to get this working I will suggest a solution using the code that you already have.

              Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
              
                  Dim Row As DataRow = ProjectsDataSet1.Tables("Projects").NewRow()
                  Row("FirstName") = textBoxFName.Text    // <============ OBVIOUSLY, REPLACE "FirstName", "LastName" WITH YOUR COLUMN NAMES
                  Row("LastName") = textBoxLName.Text     // <============ AND textBoxWhatever WITH YOUR TEXTBOXES
              
                  ProjectsDataSet1.Tables("Projects").Rows.Add(Row)   // <========== THEN PUT THE NEWLY FILLED ROW INTO THE TABLE
                
                  Try
                      Me.Validate()
                      Me.ProjelerBindingSource.EndEdit()
                      Me.ProjelerTableAdapter.Update(Me.ProjectsDataSet1.Tables("Projects"))
              
                      MessageBox.Show(ProjectName.Text & vbCrLf & vbCrLf & "identified!")
                  Catch ex As Exception
                      MsgBox("Identification is not successful.")
                  End Try
              
                  Me.Close()
              
              
              End Sub
              

              I have not tested this but it should work. Give it a try and get back, if not. You do realise that your Me.Close() line will close the form, don't you? Possibly a better way to do this is to give the users a 'New' button. When this is clicked, add a new row to the table and leave it blank. Then since your data entry controls are bound to the table they will be looking at this empty row so that when data is entered into the controls it will also be added to the table. You could then use your Button1_Click as it is except for the NewRow() line, which will no longer be needed. Hope this makes sense, and helps. :)

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

              J Offline
              J Offline
              JUNEYT
              wrote on last edited by
              #6

              Thanks Henry! Yes I did it with the way as you have suggested but it is not working at all. I don't understand why do I have to assign a new row for the table if I have already bind all the fields in the form to fields in the table in dataset. Whenever validation occurs in the form, I assume that it should create a new row in the table in dataset. Am I wrong about it? The otherwise it doesn't make enough sense to use dataset because I have to add statetements for each row e.g (Row("Name") = ProjectName.Text). I could solve the problem with SQL and dataadapter but my aim is to learn how to use dataset. The table is empty ib both dataset and database. I am trying to create the first record with the use of dataset and then transfer dataset to database.

              What a curious mind needs to discover knowledge is noting else than a pin-hole.

              H 1 Reply Last reply
              0
              • J JUNEYT

                Thanks Henry! Yes I did it with the way as you have suggested but it is not working at all. I don't understand why do I have to assign a new row for the table if I have already bind all the fields in the form to fields in the table in dataset. Whenever validation occurs in the form, I assume that it should create a new row in the table in dataset. Am I wrong about it? The otherwise it doesn't make enough sense to use dataset because I have to add statetements for each row e.g (Row("Name") = ProjectName.Text). I could solve the problem with SQL and dataadapter but my aim is to learn how to use dataset. The table is empty ib both dataset and database. I am trying to create the first record with the use of dataset and then transfer dataset to database.

                What a curious mind needs to discover knowledge is noting else than a pin-hole.

                H Offline
                H Offline
                Henry Minute
                wrote on last edited by
                #7

                You may have already seen this but in case you haven't, take a look at How to: Insert New Records into a Database[^] on MSDN.

                JUNEYT wrote:

                I don't understand why do I have to assign a new row for the table if I have already bind all the fields in the form to fields in the table in dataset.

                You have to assign a new row so that there is somewhere for the new data to go. An empty table has no rows and therefore does not have anywhere to put your data. If your table already has data, your controls will be showing that data and any changes to their contents will change the existing data, therefore requiring a new row as before. Something has occurred to me. When I was starting out with DB stuff, I had a similar mysterious problem. In my case it was something really silly. You might want to check this for your application. If you select your database in Solution Explorer then hit F4, the Properties Window will open for the file. Fairly near the top you should see a 'Copy to Output Directory' entry. Is that set to 'Copy Always'? If so, change it to 'Copy if Newer' because otherwise what happens is that every time you run your app, the empty DataBase is copied over the one with the data in it. Oh how I laughed, when I found that. NOT!! :)

                Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups