Now my Updating command for the Database is playing with my mind. :-(
The following code appears to work. I can even close the program and restart it, noting that the changes appear to have been saved.
Try
Me.Validate()
Me.ProductAndUsesBindingSource1.EndEdit()
Me.Product_and_UsesTableAdapter1.Update(Me.DataSet1.Product_and_Uses)
MsgBox(" Update successful")
Catch ex As Exception
MsgBox(" Update failed")
End Try
I get the Update Successful message but then later I come into the program and the changes have disappeared.
It's like the changes have been held in a temporary file but not updated the main database.
My reading tells me that the EndEdit should have fixed the problem but it hasn't.
I've tried using Me.Product_and_UsesTableAdapter1.Update(me.Dataset1) without the .Product_and_Uses, but that doesn't make a difference.
Thank you for your assistance.
David

David CarterPosted Aug 12, 2010, 8:18 AM
What was wrong:
When creating a new OleDbAdapter, you are asked for the database name. When you select the Database, you are asked if you want to "copy the file to your project and modify the connection". If you answer "Yes" (which to my way of thinking was logical) it creates a copy of the database.
When you run your program, you modify the database file in the output directory and when in design you use the database file in the solution directory.
The way around this is to say "No" when asked and then only one database file is created and used in all circumstances.
All updates are then made to the one and only database file.
Thank you for your patience with this and I hope this assists others with the problem in future.
Regards,
David
David CarterPosted Aug 12, 2010, 12:21 AM
UPDATE ProductandUses
SET ProductName = ?, Under10Years = ?, Over10Years = ?, Under50Kg = ?, Over50Kg = ?
WHERE (Product_ID = ?) AND (? = 1 AND ProductName IS NULL OR
ProductName = ?) AND (? = 1 AND Under10Years IS NULL OR
Under10Years = ?) AND (? = 1 AND Over10Years IS NULL OR
Over10Years = ?) AND (? = 1 AND Under50Kg IS NULL OR
Under50Kg = ?) AND (? = 1 AND Over50Kg IS NULL OR
Over50Kg = ?)
I have tried your smaller code, I deleted the above code and typed in your example manually, but neither works.
Regards,
David
Abhimanyu K VatsaPosted Aug 11, 2010, 2:15 AM
You are using full wizard based project but my friend for your future in development wizard is no longer supported. You have to work with lot in code-behind. Well....let's talk about your project.
You are missing somewhere as given below:
Follow the steps:-
************************
Step 1:-
Select "OleDbDataAdapter1" and expand the UpdateCommand Properties from Properties window
Step 2:-
In "CommandText" sub-properties write the following code:
UPDATE ProductandUses
SET ProductName = ?, Under10Years = ?, Over10Years = ?, Under50Kg = ?, Over50Kg = ?
WHERE (Product_ID = ?)
Now run the project....and I hope it will work now.
I also recommand you to download this book given below for better understanding in code-behind.
[Click here]
Thanks
David CarterPosted Aug 10, 2010, 7:14 AM
Please not that the new product appears to be saved, but if you make a change to the code.
Eg. Just put a REM in front of the line Dataset11.Clear() and then I retyped it on a new line, the newly added product disappears from the DataGrid.
David
Abhimanyu K VatsaPosted Aug 10, 2010, 3:59 AM
Please..no need to mail me
you must upload the zipped (rar) file (including entire project) here in this forum question reply (there is option to upload zipped file in below Post a reply).
I will check it out.
thanks
David CarterPosted Aug 9, 2010, 11:06 PM
I don't know what Update Query means.
I have stripped the code down to bare basics.
Form1 has DataGridView1 and 2 buttons; btnClose and btnUpdate
OleDbAdapter1 connects to database called TheProducts.mdb, which has 1 table called ProductsandUses, with Fields: Product_ID (AutoNumber), ProductName (Text), Under10Years(Yes/No), Over10Years(Yes/No), Under50Kg(Yes/No), Over50Kg(Yes/No).
When I run the program, the DataGridView shows the products and the ticks against the suitable ages and weights.
When I add products and press the Update button it appears to work perfectly.
I can shut down Visual Studio 2008, reopen it and run the program and the new data will appear to have been saved.
The code is very simple and it appears to save the data to the database, but if I edit any of the code in any way the updates are lost.
For instance, if I delete the line "DataSet11.Clear()" and retype it exacly the same, the updated changes are lost forever.
If I turn the computer off, the data is lost also.
I got the code from Michael Halvorson's book "Visual Basic.NET Step by Step".
Imports System
Imports System.Windows.Forms
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Sql
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataSet11.Clear()
OleDbDataAdapter1.Fill(DataSet11)
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Try
OleDbDataAdapter1.Update(DataSet11)
MsgBox(" Update successful")
Catch ex As Exception
MsgBox(" Update failed")
End Try
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
End Class
If you give me permission I will e-mail you small screen dumps to show the process as I am experiencing it.
Regards,
David
Abhimanyu K VatsaPosted Aug 9, 2010, 1:02 AM
David, as per your question...updating command is not effecting the database, right?
but the code you are provided has only SELECT query...where is UPDATE query...
please, it would be nice if you attach your entire project.
thanks
David CarterPosted Aug 8, 2010, 2:49 AM
Their existence might be affecting the update function, but I'm not sure how to get around that.
D.
David CarterPosted Aug 7, 2010, 9:18 PM
Public Function EndEdit(ByVal context As DataGridViewDataErrorContexts) As Boolean
End Function
Private Sub dataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
DataGridView1.Rows(e.RowIndex).ErrorText = String.Empty
End Sub
'The dataAdapter pulls all data from the Table Product_and_Uses into datagridview1
SELECT Product_and_Uses.*
FROM Product_and_Uses
'It generates SELECT, INSERT, UPDATE, DELETE statements and generates Table Mappings.
Private Sub FormAddData_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Product_and_UsesTableAdapter1.Fill(Me.Dataset1.Product_and_Uses)
End Sub
Public Sub UpdateTheData()
Try
'Me.Validate() 'This is behind a remark and is not used at the moment.
Me.ProductandUsesBindingSource1.EndEdit()
Me.Product_and_UsesTableAdapter1.Update(Me.Dataset1)
MsgBox(" Update successful")
Catch ex As Exception
MsgBox(" Update failed")
End Try
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
The other Queries in the program are not associated with this form, Adapter or Grid.
The sole purpose of this grid is to show the products and allow changes to me made to the database.
So the above is the full code to date.
Regards,
David
Abhimanyu K VatsaPosted Aug 7, 2010, 10:20 AM
please provide the full function named 'EndEdit()' and all except function with queries what you are using.
I mean provide full codes.
thanks