How to use a data view in c#
Here is a sample code which shows how to use the dataview in c# for filtering the data
protected void btnSubmit_Click(object sender, EventArgs e)
{
DataSet dsStates = null;
DataView dvRow = null;
try
{
dsStates = new DataSet();
dsStates.ReadXml( Path.Combine(Request.PhysicalApplicationPath , "xmlfile.xml"));
dvRow = dsStates.Tables[0].DefaultView;
dvRow.RowFilter = "state_Text='a'";
TextBox1.Text = dvRow.Table.Rows[0].ItemArray[0].ToString();
}
catch (Exception )
{
throw ;
}
finally
{
dsStates = null;
dvRow = null;
}
}
Description:
From above we are forming the datatable from the xml file, and from the datatable we are filtering the data into the dataview using the dataview's row filter method.
Note:
Good coding practices from the above code are:
1) While combining the path then use the Path.Combine method which is present in the System.IO namespace.
2) Use exceptional handling.
3) Clear the data in the finally block.
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment