title: c# update if record exists else insert new record
body: <p>I have code that inserts data into a table when a user enters certain values into three boxes on the page.</p>
  
  <p>The boxes are order number, total weight and tracking reference.</p>
  
  <p>I now need to add further functionality to this code and check first to see if the order number exists, if it does i need to update the columns, if it doesn't I need to insert a new row and add data to that.</p>
  
  <p>I was thinking simply, something like IF results = 0, Insert NEW, ELSE update</p>
  
  <p>How can I modify my code to do this?</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      errorLabel.Visible = false;
      successLabel.Visible = false;
      errorPanel.Visible = false;
  }
  
  protected void submitBtn_Click(object sender, EventArgs e)
  {
      if (Page.IsValid)
      {
          int _orderID = Convert.ToInt32(orderID.Text);
          string _trackingID = trackingNumber.Text;
          DateTime _date = DateTime.UtcNow;
          int _weightID = Convert.ToInt32(weightID.Text);
  
          SqlConnection myConnection = new SqlConnection("Data Source=localhost\\Sqlexpress;Initial Catalog=databasename;User ID=username;Password=password");
          SqlCommand myCommand = new SqlCommand("INSERT into Shipment (TrackingNumber, OrderId, ShippedDateUtc, CreatedOnUtc, TotalWeight) VALUES (@tracking, @order, @date, @date, @weight)", myConnection);
  
          try
          {
              myConnection.Open();
              myCommand.Parameters.AddWithValue("@order", _orderID);
              myCommand.Parameters.AddWithValue("@tracking", _trackingID);
              myCommand.Parameters.AddWithValue("@date", _date);
              myCommand.Parameters.AddWithValue("@weight", _weightID);
              int rowsUpdated = myCommand.ExecuteNonQuery();
              myConnection.Close();
              if (rowsUpdated &gt; 0)
              {
                  alertdiv.Attributes.Add("class", "alert alert-success form-signin");
                  successLabel.Text = "Thank you, tracking details have been updated";
                  successLabel.Visible = true;
                  errorPanel.Visible = true;
  
              }
              else
              {
  
                  alertdiv.Attributes.Add("class", "alert alert-error form-signin");
                  errorLabel.Text = "Oh dear, the order number is not recognised, please check and try again";
                  errorLabel.Visible = true;
                  errorPanel.Visible = true;
              }
  
              orderID.Text = "";
              trackingNumber.Text = "";
              weightID.Text = "";
          }
          catch (Exception f)
          {
              errorLabel.Text = "This order number does not exist, please check";
              errorLabel.Visible = true;
              errorPanel.Visible = true;
              return;
  
          }
      }
  }
  
  protected void Signout_Click(object sender, EventArgs e)
  {
      FormsAuthentication.SignOut();
      Response.Redirect("Login.aspx");
  }
  </code></pre>