Writing data to a database from a datagrid

How would I write data back into a database using a DataGrid on a non-web project?

Thanks,
LVL 6
PoeticAudioAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

stone123Commented:
Two solutions are provided here. For simplification, I only give examples of using OleDb type of objects. You can replace all OleDb* objects to Sql* objects if necessary.

Solution #1:

I presume that you are using an OleDbDataApapter and a DataSet object between your database and datagird.
Then use Update method to save your datagrid data back to database:

oleDbDataAdapter1.Update(myDataSet, "myTableName");


Solution #2: use embedded SQL statement.

Create an OleDbConnection to open a connection to your database, and an OleDbCommand object to execute SQL statement.

OleDbConnection myConn;
OleDbCommand cmd;

myConn.Open();
cmd.CommandText = "UPDATE myTableName SET theColumnName= @myValue WHERE nID = @myID";
cmd.Parameters.Add("@myValue", myValue);
cmd.Parameters.Add("@myID", theID);
cmd.ExecuteNonQuery();
myConn.Close();
0
stone123Commented:
In my second approach,

To initiate your objects, use:

myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\myFolder\\myDataBase.mdb");
cmd = new OleDbCommand("", myConn);

This example uses Microsoft Acess database.

If you have difficulty of making it work with other type of database, let me know.

stone.
0
PoeticAudioAuthor Commented:
I'm getting an exception:

I'm trying to use
dataAdapter1.Update(dataSet1, "tblClient");

but i'm getting this

"Update requires a valid UpdateCommand when passed DataRow collection with modified rows."


Sorry, i'm totally new to this
0
stone123Commented:
You can write code to define the OleDbUpdateCommand.CommandText, which is basically a SQL statement, for your OleDbDataAdapter object.

Alternatively, if you create the OleDbDataAdapter object by Data Adapter Configuration Wizard, you should be able to set it there.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.