Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Connections,Transactions and DataReader
Message
From
15/12/2003 12:39:44
 
General information
Forum:
ASP.NET
Category:
ADO.NET
Miscellaneous
Thread ID:
00859176
Message ID:
00859182
Views:
16
Hi Marcus,

I don't know for sure, but I'm betting that the SqlDataReader closes the connection once it's done with it (I've not used DataReaders). Your best bet is to open the connection in your UpdateData() method and close it there when you're done with it, rather than opening the connection in the SetupConnection() method.

~~Bonnie

>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();
>                 }
>
>
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform