Updating a database from a datagrid

Posted on 2007-08-06
Last Modified: 2010-04-23
Hi all,

I am using VB.NET (VS 2003), with SQL Server 2000.

I have a datagrid which is populated through a dataset with information from a SQL Server table. The user is to be able to add rows to the existing data and also change some of the data that has already been pulled out of SQL Server.

My question is, what is the best way to go about updating the database with all changes? When should the update occur? What code should I use?

Any comments would be great.
Question by:FMabey
    LVL 18

    Expert Comment

    by:Ajay Sharma
    Do it in three way

    1) fille the datagrid with the specified table.

    2) create some text fields and a button to add the field's values to the dataTable , and then also insert to the database. use this :

    3) use an Update column in the datagrid to update the existing details.
    LVL 3

    Author Comment

    I have found this way of updating the database with any changes:

    Can anyone see any problems with this?
    LVL 18

    Expert Comment

    by:Ajay Sharma
    see this link

    public DataSet updateData ( DataSet myDataSet,
          string connString, string query ) {
       OleDbConnection myConn = new OleDbConnection ( connString );
       OleDbDataAdapter adapter = new OleDbDataAdapter ( );
       adapter.SelectCommand = new OleDbCommand ( query, myConn );
       OleDbCommandBuilder custCB = new OleDbCommandBuilder ( adapter );
       DataSet myDataSet = new DataSet ( );

    us used to update the database.

    u need OleDbCommandBuilder to update the dataset
    LVL 3

    Author Comment

    Why can I not use the code I have posted? It seems to work fine for what I want to do....
    LVL 18

    Expert Comment

    by:Ajay Sharma
    k it means u got some full fleash code to fullfill ur requirement.

    If it is working fine then carry on.

    Good luck
    LVL 3

    Author Comment

    What I wondered was if this is the correct way to go about things or is it (unsafe?!) not a great method.

    How do people usually update their data in these situations?
    LVL 18

    Accepted Solution

    Its not unsafe. It is provided by .net itself.

    When using dataset with datagrid it is normally used.

    It depends, some people (like me) uses the sql update command because of we got a full control over data.
    For exmaple, to run the validation on the new data or the updated data. or to run multiple tasks based on the
    new values.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
    This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
    Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

    760 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

    8 Experts available now in Live!

    Get 1:1 Help Now