PC Too Fast For Access?

J

Jeff Gaines

I have several apps using data from an Access 2007 database.

They usually have a ListBox on the left containing identifying details of
the record and details are filled in to various controls on the right when
a name is clicked in the ListBox.

I find that when I delete a record and then re-fill the ListBox the old
record still appears, it's as though the clearing/re-filling of the
ListBox happens before Access has caught up with what's happening.

Is there a way to force Access to update/flush itself before I re-fill the
ListBox?

I don't use any of the data controls, it's all hand-written code using
ADO.NET.
 
W

WenYuan Wang [MSFT]

Hello Jeff,

According to your description, you noticed the listbox still shows the
record which has been deleted. You suspect this is because
Clearing/ReFilling of the listbox happens before the Access DB has caught
up with what's happening, correct? If I misunderstood anything here, please
don't hesitate to correct me.

Could you please paste some code snippet about how do you
delete/clear/re-filling the listbox?

It sounds like you forget to update your dataset to Access database.
Did you add the code statement (OleDbDataAdapter.update(dataset)) after you
delete the record from cached DataSet?
If you didn't update the change to underlying database, Access DB will not
delete the records from its table.

Hope this helps.
Best regards,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeff Gaines

According to your description, you noticed the listbox still shows the
record which has been deleted. You suspect this is because
Clearing/ReFilling of the listbox happens before the Access DB has caught
up with what's happening, correct? If I misunderstood anything here, please
don't hesitate to correct me.

Could you please paste some code snippet about how do you
delete/clear/re-filling the listbox?

The delete function in the main app is:

if (diaryRecord.DeleteRecord())
{
FireMessageHandler("Record Deleted", false);
ClearDiaryEventsForm();
Application.DoEvents();
FillDiaryEventsListView();
}
else
{
FireMessageHandler("Unable To Delete Record", true);
}

The delete record function is:

public bool DeleteRecord()
{
return JCalEventUpdater.DeleteRecord(this);
}

which calls:

public static bool DeleteRecord(JDiaryRecord diaryRecord)
{
string delString = "DELETE * FROM ";
delString += m_TableName;
delString += " WHERE RecordNumber = ";
delString += diaryRecord.RecordNumber.ToString();

m_OleDbConnection = GetOleDbConnection();
m_OleDbConnection.Open();
OleDbCommand commandDelete = new OleDbCommand(delString,
m_OleDbConnection);
m_OleDbDataAdapter.DeleteCommand = commandDelete;

try
{
m_OleDbDataAdapter.DeleteCommand.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
}

As you can see I have an Application.DoEvents(); call in the main delete
function ti give Access time to catch up :)
It sounds like you forget to update your dataset to Access database.
Did you add the code statement (OleDbDataAdapter.update(dataset)) after you
delete the record from cached DataSet?

I don't use any of the graphical data controls and the
FillDiaryEventsListView(); pulls the records directly from the database:

// Returns all index data in an ArrayList
public static ArrayList AllIndexDataAfter(int dateIndex, bool dateOrder)
{
ArrayList alRecords = new ArrayList();
// Create the SelectCommand.
string strSelect = "SELECT RecordNumber, DateIndex, TimeIndex, Details
FROM " + m_TableName;
strSelect += " WHERE DateIndex >= " + dateIndex.ToString();
// TODO Check Ordering
if (dateOrder)
strSelect += " ORDER BY DateIndex, TimeIndex";
else
strSelect += " ORDER BY Details, DateIndex";
// Create Connection
m_OleDbConnection = GetOleDbConnection();
m_OleDbDataAdapter = CreateSelectAdapter(m_OleDbConnection, strSelect);
// Create / Fill the dataset
m_DataSet = new DataSet();
m_OleDbDataAdapter.Fill(m_DataSet, m_TableName);
// Get the Table from the Dataset
m_DataTable = m_DataSet.Tables[0];

JDiaryRecord.JCalEventIndexData indexData;
// Loop through the Dataset and add each Row to the ArrayList
foreach (DataRow dataRow in m_DataTable.Rows)
{
indexData = new JDiaryRecord.JCalEventIndexData();
GetRecordDetailsForIndexdata(dataRow, ref indexData);
alRecords.Add(indexData);
}
return alRecords;
}

As you can see I call Application.DoEvents(); in the first function to
give Access time to catch up :)

I would appreciate any thoughts.
 
W

WenYuan Wang [MSFT]

Hello Jeff,
Thanks for your reply.

I have reproduced the issue on my side. Apparently, you didn't close the
connection after you deleted recodes from Access Database. I believe this
results the second connection return the wrong data from Access Database.
Is it possible to close it after executing deleteCommand?

public static bool DeleteRecord(JDiaryRecord diaryRecord)
{
.........
try
{
m_OleDbDataAdapter.DeleteCommand.ExecuteNonQuery();

// please add the following line

m_OleDbDataAdapter.DeleteCommand.Connection.Close();

return true;
}
.........
}

On my side, the issue goes away after I closed the connection. Please try
it and let me know if this works fine on your side. We are glad to assist
you.

Have a great day,
Best regards,

Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeff Gaines

On my side, the issue goes away after I closed the connection. Please try
it and let me know if this works fine on your side. We are glad to assist
you.


Wen Yuan thank you, you are a star :)

It works perfectly now, I'd better plough through my source and see where
else I need to do that!
 
W

WenYuan Wang [MSFT]

You are welcome, Jeff.
I am glad to be of assistance. :)

Have a great day,
Best regards,

Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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