Writing data to a database from a datagrid

Posted on 2004-11-19
Last Modified: 2010-04-15
How would I write data back into a database using a DataGrid on a non-web project?

Question by:PoeticAudio
    LVL 1

    Expert Comment

    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;

    cmd.CommandText = "UPDATE myTableName SET theColumnName= @myValue WHERE nID = @myID";
    cmd.Parameters.Add("@myValue", myValue);
    cmd.Parameters.Add("@myID", theID);
    LVL 1

    Expert Comment

    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.

    LVL 6

    Author Comment

    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
    LVL 1

    Accepted Solution

    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.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now