title: Stored Procedure with output parameters connected to method in C#
body: <p>Do you see something <strong>blatantly</strong> wrong with this line: </p>
  
  <pre><code>commaSepString1 = com.Parameters["@CommaSepString"].Value.ToString();
  </code></pre>
  
  <p>It shows an error like: </p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <p><strong>For short, the C# method looks like:</strong></p>
  
  <pre><code>public static DataTable GetFilmDetails(string filmId, out string
  commaSepString1)
  </code></pre>
  
  <p>It takes the following stored procedure (that executes correctly on its own):  </p>
  
  <pre><code>com.CommandText = "CatalogGetFilmDetails2";
  </code></pre>
  
  <p>And since <strong>@CommaSepString is an output parameter in the stored procedure</strong>, in C# I have this:</p>
  
  <pre><code>param = com.CreateParameter();
  param.ParameterName = "@CommaSepString";
  param.Direction = ParameterDirection.Output;
  param.DbType = DbType.String;
  com.Parameters.Add(param);
  
  commaSepString1 = com.Parameters["@CommaSepString"].Value.ToString();
  </code></pre>
  
  <p>If you are curious about the Stored Procedure, it starts like this:
  CREATE PROCEDURE CatalogGetFilmDetails2</p>
  
  <pre><code>(@FilmID int, @CommaSepString VARCHAR(50) OUTPUT) AS
  </code></pre>
  
  <p>The SP inserts some rows into a table variable based on some joins, it puts all values from column x from that table into a string with comma separated values: </p>
  
  <blockquote>
  <pre><code>SELECT @CommaSepString 
  = STUFF((SELECT ', ' + Categ  FROM @Filme1 FOR XML PATH('')), 1,1,'')
  </code></pre>
  </blockquote>
  
  <p>then it selects the table.</p>
  
  <p>Thank you for the patience to read this. Would it be more clear if I published the entire code for the stored procedure and the C# method? Not sure if I am allowed that and worried it might be too lengthy to read. </p>
  
  <p>PS: There must be <strong>something wrong about the commaSepString! The code worked perfectly before I added it in both places.</strong></p>