Tuesday, 30 October 2012

Get Particular Value from a Dataset

Get Particular Value from a Dataset

We came across situations when we have to access a particular value from the Dataset.. There must be scenarios when we have to assign those values to a variable.. Here is a method to achieve this..


string title = ds.Tables[0].Rows[0]["Title"].ToString();

The above code actually assigns the value from the Dataset to variable..


DataSet dsUserDetails = FillUserDetails();
string username ="";
if (dsUserDetails != null && dsUserDetails.Tables[0].Rows.Count > 0)
         username = dsUserDetails.Tables[0].Rows[0]["username"].ToString();

where the first square braket after the "Rows"  contains the integer value of the row from which the field must be retrieved and the second square bracket contains the Column Name of the Datatable.Thus the value is retrived from the datatable of a Dataset.
Dataset will have n-number of tables.
The value in the square Bracket after the Tables(eg dsUserDetails.Tables[0].Rows.Count) denote the table location of the dataset.We are checking if the dataset is not empty i.e null and if the first table records is greater than zero then fetch the cell value from the datatable of the dataset.




No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top