Microsoft KB Archive/828279

= How To: Traverse an ADO MD Cellset Object by Using Visual C# .NET =

Article ID: 828279

Article Last Modified on 11/21/2006

-

APPLIES TO


 * Microsoft SQL Server 2000 Analysis Services
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



SUMMARY
This article describes how to traverse a Microsoft ActiveX Data Objects (Multidimensional) (ADO MD) Cellset object by using Microsoft Visual C# .NET.



MORE INFORMATION
The sample application that is provided in this article shows how to traverse an ADO MD Cellset object by using Visual C# .NET. The sample application provides a user interface that permits you to enter a Multidimensional Expressions (MDX) query in a text box. When you click the button in the application, the application retrieves the Cellset object and displays the retrieved Cellset object in a tabular format.

Note The sample application assumes that the MDX query has at least two axes and at most three axes.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

To create the sample application, follow these steps:  Start Visual Studio .NET. On the File menu, point to New, and then click Project. Under Project Types, click Visual C# Projects. Under Templates, click ASP.NET Web Application, and then click OK. On the Project menu, click Add Reference. In the Add Reference dialog box, click the COM tab. Under Component Name, click Microsoft ActiveX Data Objects (Multi-dimensional) 2.7 Library.

Note You can use ADO MD version 2.7 or later.</li> Click Select, and then click OK.</li> Add a TextBox control and a Button control from the toolbox to the WebForm1.aspx WebForm.</li>  Add the following code to the Button1_Click event handler: string query; // Assign the MDX query to a string. query = TextBox1.Text.ToString; // Create an ADO MD catalog and an ADO MD Cellset. ADOMD.Catalog oAdoMDCat = new ADOMD.CatalogClass; ADOMD.Cellset oAdoMDCellSet = new ADOMD.Cellset; try {   // Open a connection to the Analysis Server, // and then run the MDX query. oAdoMDCat.let_ActiveConnection(&quot;Provider=MSOLAP.1;&quot; +        &quot;Data Source=localhost;&quot; +         &quot;Initial Catalog=Foodmart 2000&quot;); oAdoMDCellSet.Open(query, oAdoMDCat.ActiveConnection);

// Create the pos object to contain two ordinals // for the coordinate. int numaxes = oAdoMDCellSet.Axes.Count; object[] pos = new object[numaxes]; int posval = 350;

// Traverse through the pages. for (int pages = 0; pages <= oAdoMDCellSet.Axes.Count ; pages++) {

// Traverse through the columns to write a table header. Response.Write(&quot;<Table style =&quot; + '&quot;' + &quot;Z-ORDER = 103; LEFT: 20px;&quot; +                &quot; WIDTH:&quot; + &quot;800px; POSITION: absolute; TOP: &quot; +                 posval.ToString+&quot;px&quot; + '&quot;' + &quot;HEIGHT: 116px&quot; +                 &quot;cellSpacing=1 cellPadding=1 width=600 border=1> <TR>&quot;); for (int ia = 0; ia <= oAdoMDCellSet.Axes[0].Positions.Count - 1; ia++) {           // If a page exists, write the name of the page. if (numaxes>2) Response.Write(&quot;<TD>&quot; +                    oAdoMDCellSet.Axes[2].Positions[pages].Members[0].                            Caption.ToString                     + &quot;</TD>&quot;); // Write the name of the columns. Response.Write(&quot;<TD>&quot; +                oAdoMDCellSet.Axes[0].Positions[ia].Members[0].                            Caption.ToString+                &quot;</TD>&quot;); }       Response.Write(&quot;</TR>&quot;);

// Traverse through the rows. for (int j = 0; j <=oAdoMDCellSet.Axes[1].Positions.Count - 1;                    j ++) {           // Write the dimension member name. Response.Write(&quot;<TR><TD>&quot; +                oAdoMDCellSet.Axes[1].Positions[j].Members[0].                        Caption.ToString +                 &quot;</TD>&quot;);

// Traverse through the columns. for (int i = 0; i <= oAdoMDCellSet.Axes[0].Positions.Count - 1;                        i++) {               // Load the cell coordinates to the pos object. pos[0] = System.Convert.ToInt16(oAdoMDCellSet.Axes[0].                               Positions[i].Ordinal.ToString); pos[1] = System.Convert.ToInt16(oAdoMDCellSet.Axes[1].                               Positions[j].Ordinal.ToString); if (numaxes>2) pos[2] = System.Convert.ToInt16(                       oAdoMDCellSet.Axes[2].Positions[pages].                                Ordinal.ToString); // Retrieve the value of a cell. ADOMD.Cell cell = oAdoMDCellSet.get_Item(ref pos);

// Write the FormattedValue property. Response.Write(&quot;<TD>&quot; + cell.FormattedValue + &quot;</TD>&quot;); posval=posval+25; }           Response.Write(&quot;</TR>&quot;); }       Response.Write(&quot;</Table>&quot;); }   // Close the Cellset. oAdoMDCellSet.Close; } catch (Exception goof) {   string err_message; err_message=goof.Message.ToString; Response.Write(err_message.ToString); }

// Release the Cellset object and the Catalog object from the memory. System.Runtime.InteropServices.Marshal. ReleaseComObject(oAdoMDCellSet); System.Runtime.InteropServices.Marshal. ReleaseComObject(oAdoMDCat); GC.Collect; GC.GetTotalMemory(true); GC.WaitForPendingFinalizers; </li> On the Build menu, click Build Solution.</li> On the Debug menu, click Start.

The WebForm1.aspx Web form is displayed in your Web browser.</li>  Copy the following MDX query to the text box on the WebForm1.aspx WebForm: select {[Measures].Members} on columns, {[Promotion Media].Members} on rows from Sales </li> Click Button.

A Cellset object that corresponds to the MDX query that you entered in step 13 is displayed in a tabular format.</li></ol>

Keywords: kbprovider kbquery kbhowto KB828279

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.