title: Why variable isn’t working in select query?
body: <p>Why this is working when I use absolute value but when i try to use variable as query parameter the query returns no data ?</p>
  <pre><code>private void SetBomNumber()
  {
     
      try
      {
          da2 = new SqlDataAdapter(&quot;SELECT Products.[ProductCode], Max(ISNULL([BomNumber], 0) + 1) AS NewBomSerial FROM Products LEFT JOIN Bom ON Products.ProductCode = Bom.ProductCode GROUP BY Products.[ProductCode] HAVING(((Products.[ProductCode]) = '210002')) &quot;, Cn);
          da2.Fill(dt2);
          if (dt2.Rows.Count &gt; 0)
          {
              int BomNumber= Convert.ToInt32(dt2.Rows[0][&quot;NewBomSerial&quot;].ToString());
              txtBomNum.Text = BomNumber.ToString();
              MessageBox.Show(&quot;The Next Serial Is :&quot; + BomNumber);
          }
          else
          {
              MessageBox.Show(&quot;The Query Doesn’t Work&quot;);
          }
          
      }
      catch (Exception Err)
      {
          MessageBox.Show(&quot;This Error Occured :&quot; + Err.Message, &quot;Error&quot;, MessageBoxButtons.OK, MessageBoxIcon.Error);
  
  
      }
  
  }
  </code></pre>
  <p>With variable this shows the error message &quot;The Query Doesn't work&quot; ?</p>
  <pre><code>private void SetBomNumber()
  
  {
  
  
  try
  {
  
      da2 = new SqlDataAdapter(&quot;SELECT Products.[ProductCode], Max(ISNULL([BomNumber], 0) + 1) AS NewBomSerial FROM Products LEFT JOIN Bom ON Products.ProductCode = Bom.ProductCode GROUP BY Products.[ProductCode] HAVING(((Products.[ProductCode]) = @prcode)) &quot;, Cn);
      da2.SelectCommand.Parameters.AddWithValue(&quot;@prcode&quot;, &quot;%&quot; + txtprcode.Text + &quot;%&quot;);
      da2.Fill(dt2);
      if (dt2.Rows.Count &gt; 0)
      {
          int BomNumber= Convert.ToInt32(dt2.Rows[0][&quot;NewBomSerial&quot;].ToString());
          txtBomNum.Text = BomNumber.ToString();
          MessageBox.Show(&quot;The Next Serial Is :&quot; + BomNumber);
      }
      else
      {
          MessageBox.Show(&quot;The Query Doesn’t Work&quot;);
      }
      
  }
  catch (Exception Err)
  {
      MessageBox.Show(&quot;This Error Occured :&quot; + Err.Message, &quot;Error&quot;, MessageBoxButtons.OK, MessageBoxIcon.Error);
  </code></pre>
  <p>This code works in other form with no problem , Thanks in Advance.</p>