my code is as follows
DataSet ds = new DataSet();
string connStr = "Data Source=PARITAS00024;Initial Catalog=MenuDb;Persist Security Info=True;User ID=sa;Password=paritas123";
using (SqlConnection conn = new SqlConnection(connStr))
{
string sql = "Select MenuId, MenuTitle, MenuDesc, MenuURL, ParentMenuId from tblMenus where Status=1 and RecordStatus=1 order by ParentMenuId, DisplayOrder";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.Fill(ds);
da.Dispose();
}
ds.DataSetName = "Menus";
ds.Tables[0].TableName = "Menu";
DataRelation relation = new DataRelation("ParentChild", ds.Tables["Menu"].Columns["MenuId"], ds.Tables["Menu"].Columns["ParentMenuId"], true);
relation.Nested = true;
ds.Relations.Add(relation);
System.Web.UI.WebControls.XmlDataSource xds = new System.Web.UI.WebControls.XmlDataSource();
xds.TransformFile = "~/TransformXSLT.xsl";
xds.XPath = "MenuItems/MenuItem";
xds.Data = ds.GetXml();
xds.ID = "xmlDataSourceMenu";
Menu1.DataSource = xds;
Menu1.DataBind();
is this correct way of using the xmldatasource ?
The advantages of using of data sources are related to declarative programming: move the focus from how work must be done to the results. If you are using a datasource in imperative way, you lose all the advantages.
In that code you are giving your menu some XML data got transforming the XML representation of a DataSet returned by a query through an XSL transformation: do you really need to do all that work?
Why don't populate the menu programmatically?
foreach (DataRow parentItem in ds.Tables[0].Rows)
{
MenuItem item = new MenuItem((string)parentItem["Name"]);
menu.Items.Add(categoryItem);
...
}
or, why don't use the XmlDataSource in the aspx:
<asp:XmlDataSource TransformFile="~/TransformXSLT.xsl" XPath="MenuItems/MenuItem" ID="xmlDataSourceMenu" runat="server" />
and, in code behind:
...
xmlDataSourceMenu.Data = ds.GetXml();
...