title: Check if a value exists before creating a new database row
body: <p>I am looking for a way to check if the DiscountCode value of my voucher item exists before creating a new database table row of the new voucher item. I've tried doing it with a few ways on my own previously but they didn't work for me and I am new to learning ASP.NET and C#.</p>
  <p>How can I check and make sure the DiscountCode does not exist in the database table before creating a new row?</p>
  <p>Here are the codes to create a new database row I have came up with.</p>
  <pre><code>public int VoucherInsert()
          {
              int result = 0;
  
              string queryStr = &quot;INSERT INTO Voucher(VoucherName,VoucherDescription,DiscountAmount,DiscountCode)&quot; + &quot; values (@Name,@Description,@Amount,@Code)&quot;;
              SqlConnection conn = new SqlConnection(_connStr);
              SqlCommand cmd = new SqlCommand(queryStr, conn);
              cmd.Parameters.AddWithValue(&quot;@Name&quot;, this.Name);
              cmd.Parameters.AddWithValue(&quot;@Description&quot;, this.Description);
              cmd.Parameters.AddWithValue(&quot;@Amount&quot;, this.Amount);
              cmd.Parameters.AddWithValue(&quot;@Code&quot;, this.Code);
  
              conn.Open();
              result += cmd.ExecuteNonQuery();
              conn.Close();
  
              return result;
          }
  </code></pre>