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..
Working:
string title = ds.Tables[0].Rows[0]["Title"].ToString();
The above code actually assigns the value from the Dataset to variable..
Description:
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.References:
http://stackoverflow.com/questions/7284575/getting-value-from-a-dataset-into-a-variable
No comments:
Post a Comment
Thank You for Your Comments. We will get back to you soon.