Can't delete a record from access DB

P

Pete Davis

I have a web-based C# app. I've got a routine to delete a record from a
database. It executes just fine. No exceptions, no errors that I can find,
but it simply doesn't do anything.

It's not a file permissions issue. I can add new records just fine. I can
delete the records from within Access, no problem.

I'm using ODBC (don't ask, it's a limitation of the environment I'm
deploying to). The code is as follows:

string query = "DELETE FROM Comment WHERE CommentID=" +
commentID.ToString();
using (OdbcConnection conn = new OdbcConnection(_connectionString))
{
using(OdbcCommand cmd = new OdbcCommand(query, conn))
{
conn.Open();
cmd.ExecuteNonQuery();
}
}

Am I missing something? Surely I'm just missing something basic. Everything
else in the app works okay. I can read records and add new records just
fine. I haven't tried updates because they're not needed. Any help would be
greatly appreciated. Thanks.

Pete
 
B

Bishop

I have a web-based C# app. I've got a routine to delete a record from a
database. It executes just fine. No exceptions, no errors that I can find,

but it simply doesn't do anything.

It's not a file permissions issue. I can add new records just fine. I can
delete the records from within Access, no problem.

I'm using ODBC (don't ask, it's a limitation of the environment I'm
deploying to). The code is as follows:

string query = "DELETE FROM Comment WHERE CommentID=" +
commentID.ToString();
using (OdbcConnection conn = new OdbcConnection(_connectionString))
{
using(OdbcCommand cmd = new OdbcCommand(query, conn))
{
conn.Open();
cmd.ExecuteNonQuery();
}
}

Am I missing something? Surely I'm just missing something basic.
Everything
else in the app works okay. I can read records and add new records just
fine. I haven't tried updates because they're not needed. Any help would
be
greatly appreciated. Thanks.

Strings in DB need the ' used before and after the variable....

string query = "DELETE FROM Comment WHERE CommentID = '" + "'" +
commentID.ToString();
 
S

Sericinus hunter

Pete said:
string query = "DELETE FROM Comment WHERE CommentID=" +
commentID.ToString();
[...]
Am I missing something? Surely I'm just missing something basic. Everything
else in the app works okay. I can read records and add new records just
fine. I haven't tried updates because they're not needed. Any help would be
greatly appreciated. Thanks.

It is hard to say without knowing your database table structure.
If CommentId column is of char type, try to add single quotes:

string query = "DELETE FROM Comment WHERE CommentID='" + commentID.ToString() + "'";

Or better yet, use parameterized command.
 
P

Pete Davis

Sorry, CommentID is an int and the CommentID field in the database is an
int. So the resulting string, query, would look like:

DELETE FROM Comment WHERE CommentID=121

There should be no single quotes because CommentID is not a string in code
or in the DB.

Pete
 
P

Pete Davis

Please see my response to (e-mail address removed).


Sericinus hunter said:
Pete said:
string query = "DELETE FROM Comment WHERE CommentID=" +
commentID.ToString();
[...]
Am I missing something? Surely I'm just missing something basic.
Everything else in the app works okay. I can read records and add new
records just fine. I haven't tried updates because they're not needed.
Any help would be greatly appreciated. Thanks.

It is hard to say without knowing your database table structure.
If CommentId column is of char type, try to add single quotes:

string query = "DELETE FROM Comment WHERE CommentID='" +
commentID.ToString() + "'";

Or better yet, use parameterized command.
 
S

Sericinus hunter

Pete said:
Please see my response to (e-mail address removed).

Does it work if you use parameters?
Sericinus hunter said:
Pete said:
string query = "DELETE FROM Comment WHERE CommentID=" +
commentID.ToString(); [...]
Am I missing something? Surely I'm just missing something basic.
Everything else in the app works okay. I can read records and add new
records just fine. I haven't tried updates because they're not needed.
Any help would be greatly appreciated. Thanks.
It is hard to say without knowing your database table structure.
If CommentId column is of char type, try to add single quotes:

string query = "DELETE FROM Comment WHERE CommentID='" +
commentID.ToString() + "'";

Or better yet, use parameterized command.
 
P

Pete Davis

Wow, my bad. I was just beeing a complete knucklehead. The field in the
database got renamed, just slightly, a while back and I just didn't realize
it. Sure wish it had given an error, but I guess based on SQL syntax, it
wasn't technically incorrect. Sorry for wasting your time (both of you
guys). Thanks for the help.

Pete

Sericinus hunter said:
Pete said:
Please see my response to (e-mail address removed).

Does it work if you use parameters?
Sericinus hunter said:
Pete Davis wrote:

string query = "DELETE FROM Comment WHERE CommentID=" +
commentID.ToString();
[...]
Am I missing something? Surely I'm just missing something basic.
Everything else in the app works okay. I can read records and add new
records just fine. I haven't tried updates because they're not needed.
Any help would be greatly appreciated. Thanks.
It is hard to say without knowing your database table structure.
If CommentId column is of char type, try to add single quotes:

string query = "DELETE FROM Comment WHERE CommentID='" +
commentID.ToString() + "'";

Or better yet, use parameterized command.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top