Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Connections,Transactions and DataReader
Message
 
To
All
General information
Forum:
ASP.NET
Category:
ADO.NET
Title:
Connections,Transactions and DataReader
Miscellaneous
Thread ID:
00859176
Message ID:
00859176
Views:
53
Hi,

Just for test purpose I wrote a few methods, see below.
Now I run into a strange behavior:
If I call con=SetupConnection() and then UpdateData(con) everything works like expected.
If I run ShowData(con) before I run UpdateData(con) I get an error:
"Begin transaction needs an open and available connection; connection status = open"

Can anyone explain this to me? Does the command.ExecuteReader() do anything to the connection and how can I avoid this?

Marcus
public SqlConnection SetupConnection()
		{
			SqlConnection con = new SqlConnection();
			con.ConnectionString = @".....";
			con.Open();
                        return con;
                }

public void ShowData(SqlConnection con)
		{
		SqlCommand command = new SqlCommand("SELECT * FROM Categories WHERE CategoryId = 8",con);

		SqlDataReader dr = command.ExecuteReader();
		dr.Read();
		
		MessageBox.Show(dr.GetValue(2).ToString());
		
		}

public void UpdateData(SqlConnection con)
		{
		
			SqlCommand command = new SqlCommand("UPDATE Categories SET Description = 'Seaweed and fish **New**' WHERE CategoryId = 8",con);

			SqlTransaction transaction = con.BeginTransaction();
			command.Transaction = transaction;
			command.ExecuteNonQuery();
			transaction.Commit();
                 }
Marcus Alt
TRIA IT-Consulting GmbH
Working hard | for your success
Next
Reply
Map
View

Click here to load this message in the networking platform