I've been looking into [How to check user id already exists](https://stackoverflow.com/questions/2887160/how-to-check-user-id-already-exists) to see how to do this.

I am trying to get this working in my code, however it's not working. I don't get errors or something, but it just write data in database even if order number already exists.

The function:

```
 private void createorderButton_Click(object sender, EventArgs e)
        {
            SqlConnection myConnection = dbHelper.initiallizeDB();
            String query = "INSERT INTO testtabel (knaam, korder) VALUES ('" + knaamTextBox.Text + "','" + kordernrTextBox.Text + "')";
            SqlCommand sqlCommand = new SqlCommand(query, myConnection);
            SqlCommand cmd = new SqlCommand("select * from testtabel where korder = @korder", myConnection);
            SqlParameter param = new SqlParameter();
            param.ParameterName = "@korder";
            param.Value = kordernrTextBox.Text;
            cmd.Parameters.Add(param);

            //sqlCommand.Connection.Open();
            SqlDataReader reader = sqlCommand.ExecuteReader();

            if (reader.HasRows)
            {
                MessageBox.Show("Order already exist");

            }
            else
            {
                reader.Close();
            }

            // opens execute non query 
            int rows_inserted = sqlCommand.ExecuteNonQuery();

            if (rows_inserted > 0)
            {
                label2.Text = "Order has been created";
            }

            else
            {
                Console.Write("Oops! Something wrong!");

            }

        }

```

Sorry for this kinda well known and duplicated question, but for some reason I can't get it working.