1

I am creating an application with MS Access as back-end. I am running delete query but it is not working Code:

conchek.ConnectionString = ConfigurationManager.ConnectionStrings["KedarnathDB"].ConnectionString;
conchek.Open();
OleDbCommand cmdc = new OleDbCommand("select * from ReceiptsTrns Where ID=@CallerName", conchek);
cmdc.Parameters.Add("@CallerName", OleDbType.Numeric).Value = txtRcptNo.Text.Trim();
OleDbDataReader rd = cmdc.ExecuteReader();

if (rd.HasRows)
{
 conchek.Close();
 con1.ConnectionString = ConfigurationManager.ConnectionStrings["KedarnathDB"].ConnectionString;
 con1.Open();
 OleDbCommand cmd = new OleDbCommand("DELETE from ReceiptsTrns Where ID=@RCName", con1);
 cmd.Parameters.Add("@RCName", OleDbType.Numeric).Value = txtRcptNo.Text.Trim();
 con1.Close();
 MessageBox.Show("Receipt deleted successfully");
}

else
{
 conchek.Close();
 MessageBox.Show("No receipt found with this number");
}

the code is running successfully but it is not reflecting the change when I see my MS Access database.

2
  • Are you getting the Receipt deleted successfully message? Commented Jun 11, 2013 at 12:32
  • Please consider using something more robust than Access. The Access database engine is terrible compared to SQL Server Compact or Express. Commented Aug 27, 2013 at 16:22

3 Answers 3

2

You are missing an Execute call on your cmd. You create it and then close the connection, the database never gets to run the statement.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried this cmd.ExecuteNonQuery(); but it is still not showing up the change in Database
Did you check the return value (rows affected) and did it return 1?
I did not get you, from where do I check this?
The ExecuteNonQuery() method returns a value. An Int32. That's the number of records affected on the database. As you want to delete a single record, it should return 1, if it doesn't, the statement did not do what you expected it to do.
0
con1.ConnectionString =   ConfigurationManager.ConnectionStrings["KedarnathDB"].ConnectionString;

con1.Open();

OleDbCommand cmd = new OleDbCommand("DELETE from ReceiptsTrns Where ID=@RCName", con1);
cmd.Parameters.Add("@RCName", OleDbType.Numeric).Value = txtRcptNo.Text.Trim();
**cmd.ExecuteNoQuery();**
con1.Close();

3 Comments

I did it but it is still not showing up the change in the database
does @RCName has numeric/valid value?
Yes! because it is working fine for the if condition, I have used the same column for it.
0

I followed these steps for same issue:

1) Open your access database,

2) Click windows icon in the top ribbon,

3) Click Access options,

4) Click trust center settings,

5) Click Show the Message Bar in all applications when content has been blocked

6) Close database and reopen it again,

Access will show you a warning "Certain content in the database has been disabled"

7) Click options,

8) Click enable this content.

Your database is ready to be manipulated...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.