Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this instead:</p> <pre><code> private void stripSave_Click(object sender, EventArgs e) { int id; string code; SqlCeCommand cmd; /* If textbox is empty or input is not integer, then take the next id from table. Otherwise, id is set to input value. */ if (txt1.Text == String.Empty || !int.TryParse(txt1.Text, out id)) { /* Selects max id + 1 If the table is empty, the result will be null and coalesce will return 0 for the first id number */ code = "SELECT COALESCE((SELECT MAX(CustomerID) + 1 FROM Customers), 0);"; cmd = new SqlCeCommand(code, clsMain.con); id = (int)cmd.ExecuteScalar(); } code = "INSERT INTO Customers VALUES (@CustomerID, @Name, @Phone1, @Phone2, @Address, @Notes);"; cmd = new SqlCeCommand(code, clsMain.con); cmd.Parameters.AddWithValue("@CustomerID", id); cmd.Parameters.AddWithValue("@Name", txt2.Text); cmd.Parameters.AddWithValue("@Phone1", txt3.Text); cmd.Parameters.AddWithValue("@Phone2", txt4.Text); cmd.Parameters.AddWithValue("@Address", txt5.Text); cmd.Parameters.AddWithValue("@Notes", txt6.Text); cmd.ExecuteNonQuery(); MessageBox.Show("Data stored."); } </code></pre> <p>By the way, an important note about your code: Prefer to connect to database everytime you execute a query and close it afterwards. What I mean is that instead of keeping connection open for all the execution time, use something like this:</p> <pre><code> try { clsMain.con.Open(); cmd.ExecuteNonQuery(); clsMain.con.Close(); } catch (Exception ex) { if (clsMain.con.State != ConnectionState.Closed) clsMain.con.Close(); MessageBox.Show(ex.Message); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload