Click here to Skip to main content
12,395,946 members (62,240 online)
Rate this:
 
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

ds.Tables[0].Rows[j]["Name"].ToString


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
Comments
Krunal Rohit 2-Feb-13 2:57am
   
Try without using ''
Rate this: bad
 
good
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!!
  Permalink  
Comments
Shruthi.BT 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 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 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
 
good
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
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 2 Feb 2013
Copyright © CodeProject, 1999-2016
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