Transaction in strongly-typed dataset

  • Thread starter Thread starter Ziemowit
  • Start date Start date
Z

Ziemowit

Hi all

I have problem with strongly-typed dataset.
I have two datasets generated by VisualStudio tools from database. DataSetA
consist of three tables, DataSetB of one.

Now I have to update both of them in one transaction.

If I try:

SQlConnection conn = new SqlConnection(connString);
conn.Open();
SqlTransaction trans = conn.BeginTransaction();
try {
//here is update DataSetA
//something to do
//update DataSetB
trans.Commit();
}
catch
{
trans.Rollback();
}
finally
{
conn.Close();
}

I have message tat command need transaction. I'm using namespace with
DataAdapters that is generated by VisualStudio and there is no way to
specify transaction. What I can do to solve this?

Thanks for any help

Ziemowit S.
 
Hi,

You have to assing transaction instance to each command partecipating in
transaction (adapter.InsertCommand, adapter.UpdateCommand,
adapter.DeleteCommand).
 
Dnia Mon, 6 Jun 2005 20:19:44 +0200, Miha Markic [MVP C#] napisa³(a):
You have to assing transaction instance to each command partecipating in
transaction (adapter.InsertCommand, adapter.UpdateCommand,
adapter.DeleteCommand).

Yes, I know, but how to do this. DataAdapter generated by VisualStudio
doesn't have public access to these commands.

Ziemowit
 
Hi,

Yes, it has.
Just do something like:
adapter.InsertCommand.Transaction = yourTransaction:

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info


--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

Ziemowit said:
Dnia Mon, 6 Jun 2005 20:19:44 +0200, Miha Markic [MVP C#] napisa³(a):
You have to assing transaction instance to each command partecipating in
transaction (adapter.InsertCommand, adapter.UpdateCommand,
adapter.DeleteCommand).

Yes, I know, but how to do this. DataAdapter generated by VisualStudio
doesn't have public access to these commands.

Ziemowit
 
:( No, it hasn't

Maybe I have to uses some specific settings while generating DataSet.
Ah, I'm using VS2005, maybe problem is here.

To be clear:
I have dataset generated MyDataSet. With this dataset I got
MyDataSetTableAdapters namespace. In dataset I add Insert query and
name method AddMyData()

in code I have:
....
using MyDataSetTableAdapters
....
MyDataSet test = new MyDataSet();
MyTableAdapter testAdapter = new MyTableAdapter();
//here I'm opening the connection and start the new transaction
testAdapter.InsertCommand.Transaction = trans; // !!! compiler error
'MyDataSetTableAdapters.testAdapter' does not contain a definition for
'InsertCommand'
testAdapter.AddMyData("something");

So what I have to do to fix this?

Ziemowit
 
You can inherit from the adapter and you will have access to the connection
in inherited class. Methods called during Update() are virtual.

Peter
 
Back
Top