Hi
I am trying to delete a name from my database however my code seems to have a problem. It seems to cut out before i actually excutes the SQL statement. Is there something else i have missed in the code. PatientNumber is equal to the value to the text.
This is reading the value for i put MessageBox in to see

I am using C# compact framework and sql ce if it makes any differences

thanks for any help

SqlCeConnection  connDB = new SqlCeConnection("Data Source = \\My Documents\\system.sdf");
connDB.Open();
SqlCeCommand sqlDelete = connDB.CreateCommand();
sqlDelete.CommandText = "DELETE FROM Patient WHERE (((name)= + '"PatientName"'+");)"); 		
sqlDelete.ExecuteNonQuery();
connDB.Close();

Dani AI

Generated

The immediate problem was a malformed SQL string/quoting — 's reply fixed that by building a correct command text and confirmed it worked. For production code, avoid building SQL by concatenating user data. Use parameterized commands, dispose objects with using, and check the ExecuteNonQuery return value so you can tell if a row was actually deleted.

Example (use this pattern instead of concatenation):

using (var cmd = conn.CreateCommand())
{
  cmd.CommandText = "DELETE FROM Patient WHERE [name] = @name";
  var p = new SqlCeParameter("@name", SqlDbType.NVarChar) { Value = patientName ?? (object)DBNull.Value };
  cmd.Parameters.Add(p);
  int rowsDeleted = cmd.ExecuteNonQuery(); // 0 = no match, >0 = number deleted
}

Troubleshooting and cautions:

  • Wrap database calls in try/catch and inspect exception messages during development.
  • If rowsDeleted is 0, verify the column name (consider [name] if it clashes with SQL keywords), collation/case-sensitivity, and that the value exactly matches a row.
  • Validate patientName for nulls; use DBNull.Value when appropriate.
  • Prefer using blocks to ensure connections/commands are closed even on exceptions.
  • Parameterization prevents SQL injection and avoids string-escaping issues.

For details on SqlServerCe command/parameter usage and ADO.NET parameter best practices, see Microsoft documentation on SqlCeCommand and on Commands and Parameters in ADO.NET: and Commands and Parameters (ADO.NET).

Recommended Answers

All 2 Replies

try this

SqlCeConnection  connDB = new SqlCeConnection("Data Source = \\My Documents\\system.sdf");
connDB.Open();
SqlCeCommand sqlDelete = connDB.CreateCommand();
sqlDelete.CommandText = "DELETE FROM Patient WHERE name= '"+ PatientName+ "'"; 		
sqlDelete.ExecuteNonQuery();
connDB.Close();

Hi thanks that worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.