Click here to Skip to main content
11,704,107 members (55,925 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# dataset
Hi... I am using a dataset and to retrieve a particular column value usual synatx is like


but the column name i am getting dynamically means it can be name, animal, place etc but that is completely dynamic not static so how to pass that value in that dataset line... i tried like this:

i get the dymacic column names in a variable by name like this:
string ColumnNames ;

ds.Tables[0].Rows[j]['+ ColumnNames +'].ToString 

so how can i pass this dynamic column name in that line... where i am checking a particular string... THanking you in advance!!
Posted 1-Feb-13 20:51pm
Krunal Rohit at 2-Feb-13 2:57am
Try without using ''
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can use index like arrays as below you wont need name in that case:

ds.Tables[0].Rows[0][0].ToString(); //first row first column
ds.Tables[0].Rows[1][2].ToString(); //second row third column(index start 0)

Best of luck!!
Shruthi.BT at 2-Feb-13 4:18am
thank u .. Mantu Singh .... this worked using index... if in case there is a row.. which is blank for ex...

ds.Tables[0].Rows[0][0].ToString() == ""

then i need to delete that.. row. how can that be done.. as i want the null values from dataset to be removed from the dattaset...

please help!!
Mantu Singh at 2-Feb-13 4:24am
To delete you can use
ds.Tables[0].Rows[0].Delete(); //delete first row
ds.Tables[0].Rows[1].Delete(); //delete second row
Shruthi.BT at 2-Feb-13 5:37am
but this line not working shows error.... as

'object' does not contain a definition for 'Delete'
is there any other way to delete...?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

to retrieve columns value through its name use
string ColumnName = "myColumnsName";
string columnsValue = ds.Tables[0].Rows[j][ColumnName].ToString(); or
string columnsValue = ds.Tables[0].Rows[j][ColumnIndex].ToString();//ColumnIndex is a number representing your column

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 745
1 OriginalGriff 520
2 Maciej Los 475
3 Andy Lanng 284
4 CPallini 275
0 OriginalGriff 8,784
1 Sergey Alexandrovich Kryukov 7,843
2 CPallini 5,107
3 Maciej Los 4,726
4 Mika Wendelius 3,606

Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 2 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100