Inconsistency in Stongly typed dataset

  • Thread starter Thread starter mb
  • Start date Start date
M

mb

Hi

I am using strongly typed datasets in my application. I want to delete
a row. Visual studio.net generated the dataset for me using my xsd
file. It also generated functions to add and remove a row. But the
remove function doesnt work properly. The reason being that it in turn
calls the remove function of the datarow. The remove function in
effect calls the delete function and the acceptchanges function of the
datarow. Now if I call update on the dataadapter, obviously the change
is not reflected in the database as acceptchanges resets my delete
flag. Instead if I use the delete function directly on my datarow, it
works fine as the delete function just sets a delete flag. This means
that I cannot use the function generated by visual studio.net for
deleting a row. The add function doesnt call acceptchanges so the add
function works fine. So there is definitely some inconsistency in the
way the add and remove functions behave.

Am I doing something wrong ? Am I missing something ?

MB
 
public Form1()
{
InitializeComponent();
SqlDataAdapter testAdapter = CreateDataAdapter();
int i = testAdapter.Fill(m_templatePressesDataSet,
"TemplatePresses");
m_templatePressesDataSet.WriteXml("Test.xml",XmlWriteMode.WriteSchema);
TemplatePressesDataSet.TemplatePressesRow[] rows =
(TemplatePressesDataSet.TemplatePressesRow[])
m_templatePressesDataSet.TemplatePresses.Select();
m_templatePressesDataSet.TemplatePresses.AddTemplatePressesRow("Test7",
"", 1);
m_templatePressesDataSet.TemplatePresses.RemoveTemplatePressesRow(rows[0]);

m_TemplatePressAdapter = testAdapter;
i = m_TemplatePressAdapter.Update(m_templatePressesDataSet.TemplatePresses);
}

public void RemoveTemplatePressesRow(TemplatePressesRow row)
{
this.Rows.Remove(row);
//row.Delete();
}


public TemplatePressesRow AddTemplatePressesRow(string prName, string
prComments, short prType)
{
TemplatePressesRow rowTemplatePressesRow =
((TemplatePressesRow)(this.NewRow()));
rowTemplatePressesRow.ItemArray = new object[] {
null,
prName,
prComments,
prType};
this.Rows.Add(rowTemplatePressesRow);
return rowTemplatePressesRow;
}

Just wanted to put some code for reference. So this is what I am
trying to do.
The AddTemplatePressesRow and RemoveTemplatePressesRow functions have
been added by visual studio.net by using the xsd file. The
AddTemplatePressesRow works fine. It adds the row to the dataset and
after the Update is called by the adapter it updates the new row in
the database. RemoveTemplatePressesRow function removes the row from
the dataset. But after Update is called by the adapter the change is
not reflected in the database. The autogenerated
RemoveTemplatePressesRow function calls the Remove function on the
row. I did a bit of research and came to know that Remove is in effect
calling Delete and AcceptChanges on the row. AcceptChanges resets all
the flags on the row. And hence when Update is called the deleted row
is not actually deleted from the database. I found a way around this.
If I change the RemoveTemplatePressesRow function and call
row.Delete() instead of this.Rows.Remove(row) everything works fine.
My problem is that I will have to manually do this for all my
autogenerated files for all the datasets that I use.

Is there a better way of doing this ?

MB
 
Back
Top