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.Rows["Title"].ToString();
The above code actually assigns the value from the Dataset to variable..
DataSet dsUserDetails = FillUserDetails();
string username ="";
if (dsUserDetails != null && dsUserDetails.Tables.Rows.Count > 0)
username = dsUserDetails.Tables.Rows["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.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.