title: Checking if a user exists, and stopping a database insert (access database)
body: <p>I just don't know how to check if the users exists in the database and stop it from inserting a new row to the db (which will cause an error as I set the user to be a primary key)</p>
  
  <pre><code>protected void Button1_Click1(object sender, EventArgs e)
  {
      {
  
          OleDbConnection myconnection = new OleDbConnection();
          myconnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Event.mdb";
          myconnection.Open();
  
          OleDbCommand myCommand = new OleDbCommand();
          myCommand.Connection = myconnection;
          myCommand.CommandType = CommandType.Text;
          string query = string.Format("SELECT COUNT(*) FROM users WHERE uname = '{0}'");
          myCommand.CommandText = query;
          try
          {
  
              int amountOfUsers = (int)myCommand.ExecuteScalar();
              if (amountOfUsers &lt; 1)
              {
                  String myQuery = "insert into users (uname,upassword,email,type) Values   ('" + UserName.Text + "','" + Password.Text + "' ,'" + Email.Text + "',' user');";
                  myCommand.CommandText = myQuery;
                  myCommand.ExecuteNonQuery();
                  Label1.Text = "user registered";
              }
              else
              {
                  Label1.Text = "user already exists";
                  UserName.Text = "";
  
                  Email.Text = "";
              }
          }
          finally
          {
              myconnection.Close();
          }
      }
  }
  </code></pre>