The CommandType Enumeration Value 512 Is Not Supported – Complete Fix in ASP.NET / .NET Framework

When Developers working with ADO.NET and SQL Server in .NET Framework or ASP.NET applications may get this error: “The CommandType enumeration value 512 is not supported by the .Net Framework SqlClient Data Provider” at some point in their programming life. This issue occurs when executing database commands using ADO.NET with an incorrect CommandType. The problem occurs because the SQL Server provider does not support CommandType.TableDirect (value 512). In this article, we’ll explain why this error happens and show the correct path to fix it when working with Microsoft SQL Server using SqlCommand.

System.ArgumentOutOfRangeException:
The CommandType enumeration value, 512, is not supported
by the .Net Framework SqlClient Data Provider.
Parameter name: CommandType

CommandType enumeration value 512 is not supported

🔎 Why This “CommandType enumeration value 512 is not supported” Error Happens

In .NET Framework, the CommandType enum includes:

CommandType.Text = 1
CommandType.StoredProcedure = 4
CommandType.TableDirect = 512

However:

TableDirect (512) is NOT supported by the SQL Server provider (SqlClient)

It is supported only by OleDb providers.

If you are using:

using System.Data.SqlClient;

Then CommandType.TableDirect will throw the above exception.

Common Code That Causes the Error

❌ Example 1 – Casting Integer

cmd.CommandType = (CommandType)512;

❌ Example 2 – Using TableDirect

cmd.CommandType = CommandType.TableDirect;

❌ Example 3 – Value From Database

cmd.CommandType = (CommandType)Convert.ToInt32(row["CommandType"]);

If the database value = 512 The app crashes.

✅ How to Fix the Error

✔ Solution 1 – Use CommandType.Text

For normal SQL queries:

cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Employees";

✔ Solution 2 – Use StoredProcedure

For stored procedures:

cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetEmployees";

✔ Solution 3 – Fix Database Stored Values

Instead of storing numbers like:

1
4
512

Store string values:

Text
StoredProcedure

Then safely map:

cmd.CommandType = Enum.Parse<CommandType>(commandTypeString);

🔍 Why SQLClient Does Not Support TableDirect

The SQL Server provider in ADO.NET does not implement TableDirect.

TableDirect is mainly for OleDb providers and is not valid for:

  • SQL Server
  • System.Data.SqlClient
  • Microsoft.Data.SqlClient

So if you’re connecting to Microsoft SQL Server, you must use:

  • Text
  • StoredProcedure

Only.

Working Example

using System.Data;
using System.Data.SqlClient;

string connectionString = "your_connection_string";
using (SqlConnection con = new SqlConnection(connectionString))
{
    using (SqlCommand cmd = new SqlCommand("SELECT * FROM Employees", con))
    {
        cmd.CommandType = CommandType.Text;        
        con.Open();        
        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                Console.WriteLine(reader["Name"]);
            }
        }
    }
}

✔ No more 512 error
✔ Fully compatible with SQL Server
✔ Safe and production-ready

Leave a Reply

Your email address will not be published. Required fields are marked *