B
Ben Dewey
Hey everyone,
First off I am a newbie at using System.Transactions. I am trying to do
what would seem to be a simple task.
I am using a C#.Net in a Windows Form to extract information from a
powerpoint document into a Sql Server 2005 Database.
On the Database side:
- I have two tables for TALKS and SLIDES
- I also have two corresponding sp_save_talk and sp_save_slide that return
the @@IDENTITY of the newly inserted row.
On the WinForm side:
- I have a strongly typed dataset for talks and slides which implement the
StoredProcs as TableAdapter methods and return the identity.
My Questions are:
- Why is the return value of @@IDENTITY is coming back null, which doesn't
help, because I need it to insert the slides? It works and I get a return
value if I comment out the using TransactionScope line.
- Am I using the right transaction type?
- Does this even make sense?
- I was going to just scrape the idea and use conn.begin transaction, but I
cant find anywhere in the Strongy typed DataSet to use a transaction. Does
one exist?
I am trying to use the following code:
using (System.Transactions.TransactionScope tx = new
System.Transactions.TransactionScope())
{
using (SqlConnection conn = new
SqlConnection(MySettings.EndocannabinoidConnectionString))
{
try
{
conn.Open();
int? talkId = 0;
using (talksTableAdapter talkAdapter = new talksTableAdapter())
{
talkAdapter.Connection = conn;
talkId = (int?)talkAdapter.Save(0, Path.GetFileName(this.destination.Text),
this.title.Text, (int?)this.topics.SelectedValue);
}
using (slidesTableAdapter slideAdapter = new slidesTableAdapter())
{
slideAdapter.Connection = conn;
foreach (Slide s in _pres.Slides)
{
// Insert Slides
int? slideId = (int?)slideAdapter.Save(0,
talkId,
(int?)s.SlideIndex,
Path.GetFileName(this.destination.Text) + "-slides/" +
Path.GetFileName(s.ThumbnailImageFilename),
Path.GetFileName(this.destination.Text) + "-slides/" +
Path.GetFileName(s.ImageFilename),
s.Title,
s.Notes,
s.Content,
s.Keywords);
}
}
tx.Complete();
}
catch (Exception ex)
{
MessageBox.Show("Rolled Back because " + ex.Message + (ex.InnerException ==
null ? "" : ", " + ex.InnerException.Message));
}
}
}
First off I am a newbie at using System.Transactions. I am trying to do
what would seem to be a simple task.
I am using a C#.Net in a Windows Form to extract information from a
powerpoint document into a Sql Server 2005 Database.
On the Database side:
- I have two tables for TALKS and SLIDES
- I also have two corresponding sp_save_talk and sp_save_slide that return
the @@IDENTITY of the newly inserted row.
On the WinForm side:
- I have a strongly typed dataset for talks and slides which implement the
StoredProcs as TableAdapter methods and return the identity.
My Questions are:
- Why is the return value of @@IDENTITY is coming back null, which doesn't
help, because I need it to insert the slides? It works and I get a return
value if I comment out the using TransactionScope line.
- Am I using the right transaction type?
- Does this even make sense?
- I was going to just scrape the idea and use conn.begin transaction, but I
cant find anywhere in the Strongy typed DataSet to use a transaction. Does
one exist?
I am trying to use the following code:
using (System.Transactions.TransactionScope tx = new
System.Transactions.TransactionScope())
{
using (SqlConnection conn = new
SqlConnection(MySettings.EndocannabinoidConnectionString))
{
try
{
conn.Open();
int? talkId = 0;
using (talksTableAdapter talkAdapter = new talksTableAdapter())
{
talkAdapter.Connection = conn;
talkId = (int?)talkAdapter.Save(0, Path.GetFileName(this.destination.Text),
this.title.Text, (int?)this.topics.SelectedValue);
}
using (slidesTableAdapter slideAdapter = new slidesTableAdapter())
{
slideAdapter.Connection = conn;
foreach (Slide s in _pres.Slides)
{
// Insert Slides
int? slideId = (int?)slideAdapter.Save(0,
talkId,
(int?)s.SlideIndex,
Path.GetFileName(this.destination.Text) + "-slides/" +
Path.GetFileName(s.ThumbnailImageFilename),
Path.GetFileName(this.destination.Text) + "-slides/" +
Path.GetFileName(s.ImageFilename),
s.Title,
s.Notes,
s.Content,
s.Keywords);
}
}
tx.Complete();
}
catch (Exception ex)
{
MessageBox.Show("Rolled Back because " + ex.Message + (ex.InnerException ==
null ? "" : ", " + ex.InnerException.Message));
}
}
}