Microsoft KB Archive/823401

= BUG: A System.InvalidOperationException exception may occur when you run a SQL Server query by using Visual Studio .NET =

Article ID: 823401

Article Last Modified on 5/21/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0

-





SYMPTOMS
When you run a Microsoft SQL Server query by using Microsoft Visual Studio .NET, you may receive the following error message:

An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll

Additional information: COMPUTE BY statements not supported.



CAUSE
The behavior occurs if your SQL Server query contains a COMPUTE BY clause. The behavior occurs because the Microsoft .NET Framework does not support queries that contain the COMPUTE BY clause.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section of this article.



Steps to reproduce the behavior
 Start Visual Studio .NET. On the File menu, point to New, and then click Project. Under Project Types, click Visual C# Projects, and then click Class Library under Templates. Name the project ClassLibrary1. By default, the Class1.cs file is created.  Replace the existing code in the Class1.cs file with the following code.

Note In the following code, replace the connection string with a connection string that is appropriate for your environment. using System; using System.Data; using System.Data.SqlClient;

namespace ManagedProviderRepro {   public class ComputeBy {       public static void Repro {           string connectionString = &quot;server=ServerName;uid=UserName;pwd=Password&quot;;

SqlConnection c = new SqlConnection(connectionString); c.Open;

SqlCommand cmd = new SqlCommand(&quot;use NorthWind&quot;, c); IDataReader dr = cmd.ExecuteReader; ProcessReader(dr); dr.Close; cmd.Dispose;

cmd = new SqlCommand(&quot;SELECT ProductID, Quantity, SUM(UnitPrice * Quantity) AS Total &quot; +               &quot;FROM dbo.[Order Details] &quot; +                &quot;GROUP BY ProductID, Quantity &quot; +                &quot;ORDER BY ProductID, Quantity &quot; +                &quot;COMPUTE SUM(SUM(UnitPrice * Quantity)) BY ProductID, Quantity &quot; +                &quot;COMPUTE SUM(SUM(UnitPrice * Quantity)) BY ProductID&quot;, c);

dr = cmd.ExecuteReader; try {               ProcessReader(dr); }            finally {               dr.Close; cmd.Dispose; System.Windows.Forms.MessageBox.Show(c.State.ToString); }       }

private static void ProcessReader(IDataReader dr) {           bool bNextResult = false; do           { if (dr.FieldCount <= 0) {                   bNextResult = dr.NextResult; continue; }

Object oVal; int fieldCount = dr.FieldCount; while (dr.Read) {                   for (int i = 0; i < fieldCount; i++) {                       oVal = dr.GetValue(i); Console.WriteLine(oVal.ToString); }               }                bNextResult = dr.NextResult; } while (bNextResult); }   } }  Add a reference to the System.Windows.Forms.dll assembly, and then build the project.</li> In Visual Studio .NET, point to New on the File menu, and then click Project.</li> Under Project Types, click Visual C# .Projects, and then click Windows Application under Templates.</li> Name the project WindowsApplication1. By default, the Form1.cs file is created.</li>  Replace the existing code in the Form1.cs file with the following code: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using ManagedProviderRepro;

namespace WindowsApplication1 {   ///     /// Summary description for Form1. ///    public class Form1 : System.Windows.Forms.Form {       private System.Windows.Forms.Button button1; ///        /// Required designer variable. ///        private System.ComponentModel.Container components = null;

public Form1 {           //            // Required for Windows Form Designer support //           InitializeComponent;

//           // TODO: Add any constructor code after InitializeComponent call //       }

///        /// Clean up any resources being used. ///        protected override void Dispose( bool disposing ) {           if( disposing ) {               if (components != null) {                   components.Dispose; }           }            base.Dispose( disposing ); }

#region Windows Form Designer generated code ///        /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///        private void InitializeComponent {           this.button1 = new System.Windows.Forms.Button; this.SuspendLayout; //            // button1 //            this.button1.Location = new System.Drawing.Point(120, 104); this.button1.Name = &quot;button1&quot;; this.button1.TabIndex = 0; this.button1.Text = &quot;button1&quot;; this.button1.Click += new System.EventHandler(this.button1_Click); //            // Form1 //            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.button1); this.Name = &quot;Form1&quot;; this.Text = &quot;Form1&quot;; this.ResumeLayout(false);

}       #endregion

///        /// The main entry point for the application. ///        [STAThread] static void Main {           Application.Run(new Form1); }

private void button1_Click(object sender, System.EventArgs e)       { ManagedProviderRepro.ComputeBy.Repro; }   } } </li> Add a reference to the ClassLibrary1.dll assembly that you built in step 4.</li> Build and then run the WindowsApplication1 project. You receive the error message that is mentioned in the &quot;Symptoms&quot; section.</li></ol>

<div class="references_section">