Why dataset doesn't update the database? What am I missing?
-
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.
-
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.
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.”
-
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.”
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.
-
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.
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... -
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.
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.”
-
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.”
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.
-
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.
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.”