C#.NET Insert/Update values in DataTable Row

Posted on 2007-10-15
Last Modified: 2013-12-17
Hi all

This might have been answered before, but I have been unable to find an answer I can use, so here goes....

I have a DataTable filled with data from an Excel file. Using a for-loop I run through each row in this DataTable and extract 3 values, which I then run through an SQL statement which returns 3 new values. I now need to update the row I got the original 3 values from in the DataTable with the 3 new values.

How do I insert the 3 new values in the correct places in the row?

How do I insert then in the SAME row as I got the 3 original values from?

Thanks in advance

Søren Augustesen

Question by:poultarp
    LVL 14

    Expert Comment

    for loop

    'you get the values here

    'get the new value from the SQL server (using sme sql statement)

    'Update the CURRENT row of the datatable with the new value


    2) keep a record of what is the ID of the OLD values that you are retriving...
    after fetching the records from the SQL table...use the ID that you have saved before and update the records accordingly...
    LVL 1

    Author Comment

    Hello jaiganeshsrinivasan

    "Update the CURRENT row of the datatable with the new value"

    How do I update the current row of the datatable with the new data in the correct places?

    Søren Augustesen
    LVL 14

    Accepted Solution

    if dt is your data table and i is your LOOP int value
                DataRow dr;
                dr = new DataRow();
    for(int i=0;i<dt.rows.count;i++)
                dr = dt.Rows(i); - gives you the specific row...
    dr["colname"] = "some new value"
    LVL 3

    Expert Comment


    The solution proposed by jaiganeshrinivasan will work, but in my opinion a foreach() approach looks cleaner:

    // dt is the name of the DataTable
    foreach (DataRow dr in dt.Rows) {
       // send current values to SQL based update routine
       // and retrieve updated values
       // colname1, colname2 etc represent the column names of the
       // DataTable you want to update
       Object[] updatedValues=GetUpdatedValues(dr[colname1],dr[colname2],dr[colname3]);
       dr[colname1]=updatedValues[0]; //you might have to use Convert.ToXXX()

    Open in new window


    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

    Suggested Solutions

    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    734 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

    18 Experts available now in Live!

    Get 1:1 Help Now