Code to Read a Resource Collection and Display Its Contents
Use the following code to read a Commerce resource collection and display its contents. The code sample should be used within a Commerce Server project. For more information about creating a Commerce Server project, see Creating a Commerce Project.
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="Microsoft.CommerceServer.Runtime" %>
<%@ Import Namespace="Microsoft.CommerceServer.Runtime.Configuration" %>
<%@ Page Language="C#" Debug=true%>
<HTML>
<HEAD>
<title>Resource Collection</title>
<script language="C#" >
void Page_Load(object source, EventArgs args)
{
if (!IsPostBack)
{
DisplayResources();
btnProperties.Text = "Display Properties";
}
}
///
/// This function gets the resource collection and binds it
/// to the drop-down list.
///
void DisplayResources()
{
try
{
CommerceResourceCollection resCollection = null;
// Check that the CommerceContext object for this request is
// not null.
if (CommerceContext.Current == null)
{
throw new Exception("CommerceContext object is null.Please check settings in web.config");
}
// Get the resource collection.
resCollection = CommerceContext.Current.Resources;
// resCollection.Count contains the number of resources.
string[] resArray = new string[resCollection.Count];
int resIndex =0;
// Iterate through the resource collection and build the
// string array.
foreach (CommerceResource resource in resCollection)
{
resArray[resIndex++] = resource.Name;
}
// Bind the array to the drop-down list.
lbResources.DataSource = resArray;
lbResources.DataBind();
}
catch(Exception ex)
{
// Exception handling.
throw new Exception("An error occurred", ex);
}
}
///
/// Displays the properties for the selected resource.
///
void DisplayPropertiesBtn_Click(object source, EventArgs args)
{
string resourceName;
CommerceResource resource ;
try
{
CommerceResourceCollection resCollection = null;
// Check that the CommerceContext object for this request is
// not null.
if (CommerceContext.Current == null)
{
throw new Exception("CommerceContext object is null.");
}
// Get the resource collection.
resCollection = CommerceContext.Current.Resources;
// Get the resource name whose properties are to be displayed.
resourceName = lbResources.SelectedItem.Text.Trim();
// Index the resource collection using the resource name to get the
// configuration properties for this resource.
resource = resCollection[resourceName];
// Display the Name, MajorVersion, MinorVersion, and Revision
// properties of the resource.
lblResourceName.Text = String.Format("Resource Name = {0}", resource.Name);
lblMajorVersion.Text = String.Format("Major Version = {0}", resource.MajorVerion);
lblMinorVersion.Text = String.Format("Minor Version = {0}", resource.MinorVersion);
lblRevision.Text = String.Format("Revision = {0}", resource.Revision);
// Build a hash table containing each property and its value.
Hashtable hashTable = new Hashtable();
// The value of a property can be accessed by indexing the
// resource using property name.
foreach (string propName in resource)
{
hashTable.Add(propName, resource[propName]);
}
// Bind the properites to the repeater
LineItemRepeater.DataSource = hashTable;
LineItemRepeater.DataBind();
}
catch(Exception ex)
{
throw new Exception("An error occurred", ex);
}
}
</script>
</HEAD>
<body>
<form >
<asp:DropDownList id="lbResources" />
<asp:Button id="btnProperties" OnClick="DisplayPropertiesBtn_Click" />
<asp:table width="500" Runat="server">
<asp:tablerow>
<asp:tablecell>
<asp:Label width="100%" id="lblResourceName" />
</asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell>
<asp:Label width="100%" id="lblMajorVersion" />
</asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell>
<asp:Label width="100%" id="lblMinorVersion" />
</asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell>
<asp:Label width="100%" id="lblRevision" />
</asp:tablecell>
</asp:tablerow>
</asp:table>
<asp:repeater id="LineItemRepeater" >
<ItemTemplate>
<asp:Table GridLines="both" BorderWidth="1px">
<asp:TableRow>
<asp:TableCell id="label1" width="50%">
<%# DataBinder.Eval(Container.DataItem ,"Key")%>
</asp:TableCell>
<asp:TableCell font-bold="true" id="label2" width="50%">
<%# DataBinder.Eval(Container.DataItem ,"Value")%>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</ItemTemplate>
</asp:repeater>
</form>
</body>
</HTML>
Copyright © 2005 Microsoft Corporation.
All rights reserved.