Question Upadating the DataGridView control after editing one of its entries

iliekater

New member
Joined
Jul 14, 2011
Messages
3
Programming Experience
3-5
I'd like to ask for your help for a problem I am facing in .NET :) . Recently I decided to work with data bases (Access files) , but it seems I am going to need help in order to succeed it that !
I have placed a DataGridView control on a form of mine and what I wish is to update the data base file after I made a change in one of the DataGridView's entries . I am using this code :

'Binding the DataGridView control to a data source of a data base file :
Dim DataBaseConnectionMY As New OleDb.OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source="C:\AAA\aaa.mdb")
Dim DataAdapterMY As New OleDb.OleDbDataAdapter("SELECT * FROM Companies", DataBaseConnectionMY)
Dim DataSetMY As New DataSet
DataBaseConnectionMY.Open()
DataAdapterMY.Fill(DataSetMY, "Companies")
DataBaseConnectionMY.Close()
DataGridViewMY.DataSource = DataSetMY
DataGridViewMY.DataMember = "Companies"
'Saving the changes in the data base file :
Dim CommandBuilderMY As New OleDb.OleDbCommandBuilder(DataAdapterMY)
CommandBuilderMY.GetUpdateCommand()
DataAdapterMY.Update(DataSetMY, "Companies")

but it seems that I am doing something wrong , because once I press the button to run the above code , the DataGridView reloads the table from the data base file but it is the same as before (no change incorporated) :( .
 
Just to make sure, can you check the bin\Debug folder for your application and see if Visual Studio has put a copy of your Access DB there. Also, have you tried creating a data source in the dataset designer using this table? Once you do that, you can drop the data source on to a blank form, and it will create the datagridview and everything, without any coding on your part. It's training wheels, but it's helpful at first.
 
but it seems that I am doing something wrong , because once I press the button to run the above code , the DataGridView reloads the table from the data base file but it is the same as before (no change incorporated) :( .
If you run all the code then of course the same data will reload. The code first loads the data from the database to the dataset then it updates the dataset, but no changes have been made. If you want to update changes (after initially loading the data from the database), don't load the original data first.

Also, have you tried creating a data source in the dataset designer using this table? Once you do that, you can drop the data source on to a blank form, and it will create the datagridview and everything, without any coding on your part. It's training wheels, but it's helpful at first.

I believe the data wizards should be used and that they are not training wheels. Do you create forms by writing the code by hand or do you use the designer to do that work for you? .NET, and especially VB.NET was designed for rapid application development (RAD), and using the provided tools to write code for you only makes sense. You can always view/modify the code that was created by the wizards.
 
[You may still read this post though , as I mention in the next post , I moved past this problem . The only reason I am leaving this post is just in case someone else encounters the same problem as me]

Yes , you are right . That's what I was thinking too . If all that code is ran all together , then the DGV control actually reloads the database file before any changes are incorporated .

Robert_SF , I looked in the Bin and in the obj\Debug folder , but there is no data base file copied in there .

No , I didn't use any wizzard ; everything is done by code .

By the way , here's a snapshot of my project in the Editor itself . This picture shows it all :


As you can see , I have separated the code in 3 sections : at the begining there are the declarations that are used by the following subs . The 2nd section determines the code to be executed during loading the form (that is to load the data base file in the DGV control) and the 3rd section is the code to be executed when I press the button (that is to update the data base file itself) .
There is also the error message that appears when I press the button . You see , the program starts fine by succesfully loading the proper table ("Companies") in the DGV control , but when I modify the text in a cell and press the button to save the changes , the program throws an exception saying :
"Synatx error (missing operator) in query expression '(ID = ?) AND ((? = 1 ...... bla bla bla)" .
What's this ? !

Nevertheless , at least I managed to present you the whole project and its code (which is not long) , so I hope maybe you can detect what I am doing wrong .
 
Last edited:
I made a little progress . Just out of nowhere I thought maybe it was the fact that some of the columns in the table of the data base file have spaces , for example one such a column is "Phone number 1" . Therefore , I renamed all those columns (i.e. "Phone_number_1" ) and I got rid of the above error mesage . I never thought of that before , because I remember the classic ADO had no such problems , so I presumed that the new ADO .NET would do the same . Oh well .
However , now I fell into another problem . When I press the button to update the changes into the data base file , I got another error mssage . So , once again , here's the whole project and its code , along with the new error message (reading : "No value given for one or more parameters) :
 
Last edited:
Back
Top