J
Joe
I have a method in my remote object which takes a DataTable. When the
function returns my ChildRelations are gone. This means I have no way of
updating the rows in the child table.
Is there anyway around this?
public override void UpdateTable(ref DataTable dt)
{
if (dt == null)
return;
SqlDataAdapter da = new SqlDataAdapter();
SqlDataAdapter datmp = new SqlDataAdapter();
SqlCommand cmd = m_conn.CreateCommand();
SqlCommandBuilder cb = new SqlCommandBuilder();
cmd.CommandText = string.Format("select * from {0}", dt.TableName);
datmp.SelectCommand = cmd;
da.SelectCommand = cmd;
cb.DataAdapter = datmp;
DataColumn dc = null;
foreach (DataColumn c in dt.Columns)
{
if (c.AutoIncrement == true)
{
dc = c;
break;
}
}
da.InsertCommand = cb.GetInsertCommand();
if (dc != null)
da.InsertCommand.CommandText = da.InsertCommand.CommandText + " SELECT
SCOPE_IDENTITY() As " + dc.ColumnName;
da.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;
da.UpdateCommand = cb.GetUpdateCommand();
da.DeleteCommand = cb.GetDeleteCommand();
try
{
da.Update(dt);
}
catch (Exception ex)
{
throw new Exception(ex.Message + "\nTable: " + dt.TableName,
ex.InnerException);
}
}
function returns my ChildRelations are gone. This means I have no way of
updating the rows in the child table.
Is there anyway around this?
public override void UpdateTable(ref DataTable dt)
{
if (dt == null)
return;
SqlDataAdapter da = new SqlDataAdapter();
SqlDataAdapter datmp = new SqlDataAdapter();
SqlCommand cmd = m_conn.CreateCommand();
SqlCommandBuilder cb = new SqlCommandBuilder();
cmd.CommandText = string.Format("select * from {0}", dt.TableName);
datmp.SelectCommand = cmd;
da.SelectCommand = cmd;
cb.DataAdapter = datmp;
DataColumn dc = null;
foreach (DataColumn c in dt.Columns)
{
if (c.AutoIncrement == true)
{
dc = c;
break;
}
}
da.InsertCommand = cb.GetInsertCommand();
if (dc != null)
da.InsertCommand.CommandText = da.InsertCommand.CommandText + " SELECT
SCOPE_IDENTITY() As " + dc.ColumnName;
da.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;
da.UpdateCommand = cb.GetUpdateCommand();
da.DeleteCommand = cb.GetDeleteCommand();
try
{
da.Update(dt);
}
catch (Exception ex)
{
throw new Exception(ex.Message + "\nTable: " + dt.TableName,
ex.InnerException);
}
}