Share via


getting a column's value from a DataTable using c#

Question

Wednesday, March 12, 2014 10:43 AM

Hi,

I have populated a datatable from sharepoint list which has 1 row and 2 columns. I want to put value of the second column (ID) in to the batch parameter of UpdateListItem(). here is my batch

string Batch = "<Method ID='1' Cmd='Update'>" +
            "<Field Name='ID'>30</Field>" +
            "<Field Name='PName'>AAA</Field>" +
            "<Field Name='code'>000</Field></Method>";

here i have tried passing the ID column with hard coded value. However I have to populate this value programatically. For this I have pulled ID column's values in a datatable. Now I want to use this value in place of hard coded one.

string Batch = "<Method ID='1' Cmd='Update'>" +
            "<Field Name='ID'>" + value + "</Field>" +
            "<Field Name='PName'>AAA</Field>" +
            "<Field Name='code'>000</Field></Method>";

How can I get 2nd column's value of the only row in the datatable?

All replies (3)

Wednesday, March 12, 2014 11:12 AM âś…Answered

I'm able to do it now. Is this the right approach?

DataTable dt = new DataTable();
            dt = dtdata; //dtData came from function call
            var pno = dt.Rows[0][1];

Wednesday, March 12, 2014 11:22 AM | 2 votes

That should do.

Or for multiple rows you can

foreach(DataRow row in dt.Rows)
{    
     row["COLUMN_NAME"].ToString();
}

Amit


Wednesday, March 12, 2014 11:48 AM

Thanks Amit! I'll keep a note of this. For now I needed just the one row.