title: How to create SqlParameterCollection with multiple parameters?
body: <p>I am trying to create a <code>SqlParameterCollection</code>, but gives error while adding some <code>SqlParameter</code> in <code>sp.Add()</code> method.</p>
  
  <p>Please help me how to add parameter and how to pass it to my another function where I declare a <code>SqlConnection</code> and <code>SqlCommand</code>.</p>
  
  <pre><code>SqlParameterCollection sp = null;                    
  sp.Add(new SqlParameter("@CmpyCode", SqlDbType.NVarChar)).Value = CV.Global.CMPYCODE;
  sp.Add(new SqlParameter("@Code", SqlDbType.NVarChar)).Value = codeName;
  sp.Add(new SqlParameter("@DisplayCode", SqlDbType.NVarChar)).Value = codeName + "-";
  sp.Add(new SqlParameter("@TotalDigit", SqlDbType.Int)).Value = CV.Global.PARAMTOTALDIGIT;
  insertData("&lt;Sp Name&gt;", sp);
  </code></pre>
  
  <p>My another function  is insertData(...)</p>
  
  <pre><code>internal static int insertData(string spName, SqlParameterCollection sp)
  {
          int retObj = 0;
  
          using (SqlConnection con = new SqlConnection(CV.Global.CONSTRING))
          {
              try
              {
                  con.Open();
                  SqlCommand cmd = new SqlCommand(spName, con);
                  cmd.CommandType = CommandType.StoredProcedure;
  
                  if (sp.Count &gt; 0)
                  {
                      foreach (SqlParameter param in sp)
                          cmd.Parameters.Add(param);
                  }
  
                  retObj = cmd.ExecuteNonQuery();
              }
              catch (Exception ev) 
              { 
                  Util.Log(ev); 
                  throw; 
              }
              finally
              {
                  try
                  {
                      con.Close();
                  }
                  catch (Exception ev) { Util.Log(ev); throw; }
              }
          }
          return retObj;
      }
  </code></pre>
  
  <p>I am trying to create a <code>SqlParameterCollection</code> and passed it to the <code>insertData</code> function. But it throws an error while I am calling <code>sp.Add()</code> method in my first function.</p>
  
  <p>The error is</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object</p>
  </blockquote>