title: Inspect sql statement with filled parameters
body: <p>I have this code and compiler throws this error: </p>
  
  <blockquote>
    <p>SqlException: Incorrect syntax near '0'</p>
  </blockquote>
  
  <p>I can't find the actual SQL statement used by SQL Server to cause this error. </p>
  
  <p>Is there a way to get the SQL statement with parameters for me to reproduce the error and fix it? Or maybe something wrong with my code?</p>
  
  <pre><code>void Main()
  {
      DataTable table = new DataTable("Task");
      table.Columns.Add("ID");
      table.Columns.Add("Type");
      table.Columns.Add("Worker");    
      table.Columns.Add("CaseID");
      table.Columns.Add("Name");
  
      DataRow row1 = table.NewRow();
      row1["Type"]="Type1";
      row1["Worker"]="ABCD";
      row1["CaseID"] = "1234567";
      row1["Name"] = "ABC";
      table.Rows.Add( row1 );
  
  
  
      SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
      sqlDataAdapter.InsertCommand = new SqlCommand( sqlStatement(),(SqlConnection)Connection);
      sqlDataAdapter.InsertCommand.CommandType = CommandType.Text;
  
      sqlDataAdapter.RowUpdating += new SqlRowUpdatingEventHandler(da_RowUpdating);
  
      sqlDataAdapter.InsertCommand.Parameters.Add( "@Type",SqlDbType.VarChar,4, "Type" );
      sqlDataAdapter.InsertCommand.Parameters.Add( "@Worker",SqlDbType.VarChar,4, "Worker" );
      sqlDataAdapter.InsertCommand.Parameters.Add( "@CaseID",SqlDbType.VarChar,7, "CaseID" );
      sqlDataAdapter.InsertCommand.Parameters.Add( "@Name",SqlDbType.VarChar,50, "Name" );
  
      sqlDataAdapter.Update( table );
  }
  
  static void da_RowUpdating(object sender, SqlRowUpdatingEventArgs e)
  {
    Console.WriteLine("\nRow Updating...");
    Console.WriteLine("Command text: \n{0}", e.Command.CommandText);
    Console.WriteLine("\nParameters:");
    foreach (SqlParameter p in e.Command.Parameters)
    {
        Console.WriteLine("\t{0} - {1}", p.ParameterName, p.Value);
    }
  }
  string sqlStatement()
  {
      return @"EXECUTE sp_executesql
      N'INSERT  INTO Task
              (
                  Type,
                CaseID,
                Worker,
                Name
              )
      VALUES  (
  @Type,
                @CaseID,
                @Worker,
                @Name
              );
  SELECT SCOPE_IDENTITY() AS ID',
  N'
       @Type varchar(7),
      @CaseID CHAR(7),
      @Worker CHAR(6),
      @Name VARCHAR(50)
      ',{0},{1},{2},{3}";
  }
  </code></pre>