Thursday, 3 October 2013

SQLCE Issue: update requires the command clone to have a connection object

SQLCE Issue: update requires the command clone to have a connection object

I am working on a new solution for an existing project. now I am facing
some troubles with the sql ce connection. I modified my connection with
the following class:
namespace CommonTools
{
/// <summary>
///
/// </summary>
public class SQLCEConnectorClass
{
private SqlCeConnection m_openConnection;
/// <summary>
/// Opens the SQLCE connection.
/// </summary>
/// <param name="connectionString">The connection string.</param>
/// <returns></returns>
public SqlCeConnection OpenSQLCEConnection(string connectionString)
{
bool isWorking = false;
while (!isWorking)
{
if (TryToConnect(connectionString))
{
isWorking = true;
}
else
{
// sleep for 100 ms to try the connection again
Thread.Sleep(100);
}
}
return m_openConnection;
}
/// <summary>
/// Tries to connect.
/// </summary>
/// <param name="connectionString">The connection string.</param>
/// <returns></returns>
private bool TryToConnect(string connectionString)
{
try
{
m_openConnection = new SqlCeConnection(connectionString);
if (m_openConnection.State == ConnectionState.Closed)
{
m_openConnection.Open();
return true;
}
}
catch
{
return false;
}
return false;
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
public void Dispose()
{
m_openConnection.Close();
m_openConnection.Dispose();
m_openConnection = null;
}
}
}
so it's possible to use the SQL CE with multiple user, otherwise it is
used by another process and I can not access from a different machine. I
changed also the following code segements:
ConnectionString = "DataSource='" + dbPath + fileName + "';
Password ='" + password + "';";
cn = sqlce.OpenSQLCEConnection(ConnectionString);
// only if the connection string is not empty and a connection to the
database was possible
if (ConnectionString != string.Empty)
{
// for the categories
string sql = "select * from Kategorien";
SqlCeCommand cmd = new SqlCeCommand(sql, cn);
var reader = cmd.ExecuteReader();
while (reader.Read())
{
categories.Add(Convert.ToString(reader[0]));
categoriesSAP.Add(Convert.ToString(reader[0]),
Convert.ToString(reader[1]));
}
}
else
{
XtraMessageBox.Show("Es konnte keine Verbindung zur Datenbank
hergestellt werden!", "Fehler", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
sqlce.Dispose();
now I want to update my changes:
private void SaveTable()
{
try
{
DataRow[] rows = editedRows.ToArray();
dAdapter.SelectCommand.Connection =
sqlce.OpenSQLCEConnection(ConnectionString);
dAdapter.Update(rows);
int rowCount = dAdapter.Update(dTable);
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.Message, "Fehler",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
sqlce.Dispose();
}
the first change (SaveTable) will work, without any exception. the second
change does't work. I got the following exception at
dAdapter.Update(rows):
Update requires the command clone to have a connection object. The
Connection property of the command clone has not been initialized.
before I changed it to the new connection class, everything worked as
expected. any suggestions what I made wrong?
thanks, tro

No comments:

Post a Comment