title: ASP.NET, C# How to Pass a StringQuery to a custom SQL Command
body: <p>I have a slight issue, I have a ASP.NET Webforms application. I'm sending over a <code>url?id=X</code> were <code>X</code> is my database index or id.</p>
  
  <p>I have a C# class file to run my SQL connection and query. Here is the code:</p>
  
  <pre><code>public DataTable ViewProduct(string id)
  {
      try
      {
          string cmdStr = "SELECT * Products WHERE Idx_ProductId = " + id;
  
          DBOps dbops = new DBOps();
          DataTable vpTbl = dbops.RetrieveTable(cmdStr, ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString);
          return vpTbl;
      }
      catch (Exception e)
      {
          return null;
      }
  }
  </code></pre>
  
  <p>So as you can see my problem lies within <code>string cmdStr = "SQL Query" + variable;</code></p>
  
  <p>I'm passing over my index or id through the URL then requesting it and turning it into a string then using <code>ViewProduct(productId)</code>.</p>
  
  <p>I don't know what syntax or how to add the id into my C# string sql query. I've tried:</p>
  
  <pre><code>string cmdStr = "SELECT * Products WHERE Idx_ProductId = @0" + id;
  string cmdStr = "SELECT * Products WHERE Idx_ProductId = {0}" + id;
  </code></pre>
  
  <p>also what I have currently to no avail.</p>