views:

113

answers:

1

I have C# Window Forms project and MySQL ODBC connector to localhost server with all privileges. I also succeeded to fill dataGridView with data, but the changes I make are not stored back to the database. How do I create save button?

+1  A: 

One easy, as in automagical, way to do it is to make a dataset: Menu>Data>Add New Data Source
When the wizard shows, choose Database, DataSet and choose new connection to connect to the database.
When you have the dataset in DataSource window move the dataset over a empty form and VS will create a number of buttons for you including a Save button.
When you have done that you can look at the code to se how it works and adapt that to your own need.
Good luck
edit
based on the comment you made to may answer I can guess that the database adapter you are using doesn't allow the generation of methods that talks directly to the database, hence the errrors depenency on the database connector. That is at least my understanding of this MSDN page
(See headline TableAdapter GenerateDbDirectMethods)

To resolve this you have to set the GenerateDbDirectMethods to false in TableAdapter and only use the InsertCommand, UpdateCommand, and DeleteCommand

Gorgen
Yes, of course. But attempt to GenerateDBDirectMethods fails with message "The type of database object does not allow to set GenerateDBDirect to true" - the object is DataTable in myDataSet.xsd. (I use MySQL ODBC 5.1.7 if it is relevant) Am I missing something?
Jan Turoň
@Jan: Maybe you can edit your question to include more of of your code and how and when it goes wrong.
Gorgen
@Gorgen: I have been trying and searching how to add Insert/Update/DeleteCommand with no success in Visual tools of MSVC. But finally I found a solution using Microsoft.Data.Odbc library here. It is not Visual, but works perfectly with MySQL ODBC Connector 5.1, the link is here http://dev.mysql.com/doc/refman/5.0/en/connector-odbc-examples-programming.html Anyway, thanks for help, your solution worked for me with MsSQL.
Jan Turoň
@Jan: Glad to be of any help.
Gorgen