title: Nullable Table Valued Parameter in Stored Procedure
body: <p>I have this procedure</p>
  
  <pre><code>CREATE PROCEDURE dbo.spProcedure1
      @intArray as dbo.intArray READONLY
  AS
  BEGIN
  -- ...
  END
  </code></pre>
  
  <p>which use user type as a parameter</p>
  
  <pre><code>CREATE TYPE dbo.IntArray AS TABLE (IntValue int NULL)
  </code></pre>
  
  <p>and I am calling the procedure from the C# ASP.NET MVC 4 project</p>
  
  <pre><code>    // creating empty SQL @IntArray parameter
  
          var emptyIntDataTable = new DataTable();
          emptyIntDataTable.Columns.Add("IntValue");
  
      // calling stored procedure
  
          return Database.SqlQuery&lt;int&gt;(
              @"spProcedure1 @p1",
              new SqlParameter("p1", (object)Utils.ToDataTable(m.IntArray) ?? emptyIntDataTable)
          ).ToList();
  
      // ToDataTable method which is returning null
  
          public static DataTable ToDataTable&lt;T&gt;(this IList&lt;T&gt; data)
                  {
                      if (data == null)
                          return null;
  
                      ... // code omitted because it is not working yet                
  }
  </code></pre>
  
  <p>the error which is throwed when calling stored procedure is</p>
  
  <blockquote>
    <p>The table type parameter 'p1' must have a valid type name.</p>
  </blockquote>
  
  <p>How to pass an empty table value?</p>
  
  <p>Passing the list instead of datatable throw following error</p>
  
  <pre><code>var emptyIntDataTable = new List&lt;int&gt;;
  </code></pre>
  
  <blockquote>
    <p>No mapping exists from object type System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] to a known managed provider native type. </p>
  </blockquote>