title: How to get error line number of code using try-catch
body: <p>I want to get line number of code which cause error. For example;</p>
  
  <pre><code>static void Main(string[] args)
  {
      using (SqlConnection conn = new SqlConnection(bagcum))
      {
          SqlCommand cmd = conn.CreateCommand();
          cmd.CommandText = "DONTINSERT into GIVEMEERROR(CamNo,Statu) values (" + 23 + "," + 0 + ")";
          conn.Open();
          int n = cmd.ExecuteNonQuery();
      }
  }
  </code></pre>
  
  <p>so As we know that code doesn't work, it will throw exception Line number of code which is:</p>
  
  <pre><code>int n = cmd.ExecuteNonQuery();
  </code></pre>
  
  <p>So how can get that line number of using <code>try-catch</code>? I tried using a <code>StackTrace</code> class but it gives line number as 0:</p>
  
  <pre><code>static void Main(string[] args)
  {
      try
      {
          using (SqlConnection conn = new SqlConnection(bagcum))
          {
              SqlCommand cmd = conn.CreateCommand();
              cmd.CommandText = "DONTINSERT into GIVEMEERROR(CamNo,Statu) values (" + 23 + "," + 0 + ")";
              conn.Open();
              int n = cmd.ExecuteNonQuery();
          }        
      }
      catch (Exception ex)
      {
          System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);            
          Console.WriteLine("Line: " + trace.GetFrame(0).GetFileLineNumber());
      }
  }
  </code></pre>
  
  <p>OUTPUT:</p>
  
  <pre><code>Line:0
  </code></pre>
  
  <p><strong>Update:</strong><br />
  Usually error line of code is 22 so I have to get that number.</p>
  
  <p>Thanks</p>