Hanumendra
Hanumendra

Reputation: 319

delete query on access database c#

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.

Upvotes: 1

Views: 4677

Answers (3)

Xameer
Xameer

Reputation: 31237

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...

Upvotes: 0

indiPy
indiPy

Reputation: 8062

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();

Upvotes: 0

nvoigt
nvoigt

Reputation: 77285

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.

Upvotes: 2

Related Questions