title: Passing datatable from C# to SQL Server 2008
body: <p>How can I pass a DataTable from C# to SQL Server 2008?</p>
  
  <p><strong>Exception</strong>:</p>
  
  <blockquote>
    <p>The table type parameter '@CurrentTableInitial' must have a valid type
    name.</p>
  </blockquote>
  
  <p><strong>Table structure:</strong></p>
  
  <pre><code>CREATE TABLE [dbo].[RegisterChoiceUserInitial](
      [RegisterChoiceUserInitialID] [int] IDENTITY(1,1) NOT NULL,
      [UserId] [uniqueidentifier] NOT NULL,
      [RegisterChoiceUserInitialJob] [nvarchar](50) NULL,
      [RegisterChoiceUserInitialOrganization] [nvarchar](50) NULL,
      [RegisterChoiceUserInitialUnit] [nvarchar](50) NULL,
      [RegisterChoiceUserInitialMembershipType] [nvarchar](50) NULL,
      [RegisterChoiceUserInitialStart] [nvarchar](10) NULL,
      [RegisterChoiceUserInitialEnd] [nvarchar](10) NULL,
   CONSTRAINT [PK_RegisterChoiceUserInitial] PRIMARY KEY CLUSTERED 
  (
      [RegisterChoiceUserInitialID] ASC
  )
  </code></pre>
  
  <p><strong>User-defined type:</strong></p>
  
  <pre><code>CREATE TYPE [dbo].[TableTypeInitial] AS TABLE(
      [ID] [int] NULL,
      [InitialJob] [nvarchar](50) NULL,
      [InitialOrganization] [nvarchar](50) NULL,
      [InitialUnit] [nvarchar](50) NULL,
      [InitialMembershipType] [nvarchar](50) NULL,
      [InitialMembershipStart] [nvarchar](10) NULL,
      [InitialMembershipEnd] [nvarchar](10) NULL
  )
  </code></pre>
  
  <p><strong>Stored procedure:</strong></p>
  
  <pre><code>create PROCEDURE [dbo].[FinishRegisterChoiceUserInitial]
        ( @UserId                         uniqueidentifier,
         @TableVariable TableTypeInitial READONLY)
   AS
   BEGIN
  
   INSERT INTO [Election].[dbo].[RegisterChoiceUserInitial]
             ([UserId]
             ,[RegisterChoiceUserInitialJob]
             ,[RegisterChoiceUserInitialOrganization]
             ,[RegisterChoiceUserInitialUnit]
             ,[RegisterChoiceUserInitialMembershipType]
             ,[RegisterChoiceUserInitialStart]
             ,[RegisterChoiceUserInitialEnd])
         SELECT
               @UserId AS UserId
               ,InitialJob
               ,InitialOrganization
               ,InitialUnit
               ,InitialMembershipType
               ,InitialMembershipStart
               ,InitialMembershipEnd
         FROM
               @TableVariable   
   END
  </code></pre>
  
  <p><strong>DataTable:</strong></p>
  
  <pre><code>DataTableInitial.Columns.Add(new DataColumn("ID", typeof(int)));
  DataTableInitial.Columns.Add(new DataColumn("InitialJob", typeof(String)));
  DataTableInitial.Columns.Add(new DataColumn("InitialOrganization", typeof(String)));
  DataTableInitial.Columns.Add(new DataColumn("InitialUnit", typeof(String)));
  DataTableInitial.Columns.Add(new DataColumn("InitialMembershipType", typeof(String)));
  DataTableInitial.Columns.Add(new DataColumn("InitialMembershipStart", typeof(String)));
  DataTableInitial.Columns.Add(new DataColumn("InitialMembershipEnd", typeof(String)));
  
  cmd.CommandText = "EXEC FinishRegisterChoiceUserInitial @UserId, @CurrentTableInitial ";
  cmd.Parameters.AddWithValue("@UserId",user.ProviderUserKey);
  
  DataTable TableInitial=(DataTable)ViewState["CurrentTableInitial"];
  SqlParameter a = cmd.Parameters.AddWithValue("@CurrentTableInitial", TableInitial);
  a.SqlDbType = SqlDbType.Structured;
  
  cmd.ExecuteNonQuery();
  </code></pre>