using-statement

How to determine whether a .NET exception is being handled?

We're investigating a coding pattern in C# in which we'd like to use a "using" clause with a special class, whose Dispose() method does different things depending on whether the "using" body was exited normally or with an exception. To the best of my understanding, the CLR keeps track of the current exception being handled until it's be...

Error with the Using Statement and Lazy Initialized Property

The code below will throw an InvalidOperationException: The ConnectionString property has not been initialized. The exception is thrown at the line calling Connection.Open() in the Load method. If I use the try-finally statement instead of the using statement, everything works correctly. Can anyone explain why the exception occurs with t...

Calling Dispose() vs when an object goes out scope/method finishes

Hi, I have a method, which has a try/catch/finall block inside. Within the try block, I declare SqlDataReader as follows: SqlDataReader aReader = null; aReader = aCommand.ExecuteReader(); In the finally block, the objects which are manually disposed of are those which are set at the class leve...

Disposing datacontext causes Invalid attempt to call Read when reader is closed

I'm building an MVC 2 app and using linq to sql with stored procs. I created a data access layer that has an internal datacontext class and a public class that I expose applications. In my public class I expose methods which access the datacontext class and convert the data to my own object model classes using linq. In my public class...

C#: "Using" Statements with HttpWebRequests/HttpWebResponses

Jon Skeet made a comment (via Twitter) on my SOApiDotNet code (a .NET library for the pre-alpha Stack Overflow API): @maximz2005 One thing I've noticed just from browsing the source quickly: you don't disposed (sic) of WebResponses. "using" statements FTW. He indicates that I need to wrap these Web sessions in "using" stateme...

Error using Using operand and XmlElement

my code is like this: Using StateProv As XmlElement = CType(hotelSearch.SelectSingleNode("/ota:OTA_HotelSearchRQ/ota:Criteria/ota:Criterion/ota:Address/ota:StateProv", nsmgr), XmlElement) 'i am getting error in this line.... StateProv.SetAttribute("StateCode", BLLHotel_Search.StateCode) StateProv.ChildNodes(0).In...

What does using(object obj = new Object()) mean?

What does this statement means in C#? using (object obj = new object()) { //random stuff } ...

What does Using(.....){...} mean

Possible Duplicates: Using the using statment in c# What is the C# Using block and why should I use it? Just wondering what this means? I've seen lots of tutorials online that have the syntax: using (SqlCeCommand cmd2 = new SqlCeCommand("SELECT city FROM cities JOIN states ON states.id=cities.state WHERE states.state='" + r...

c# question - is there a tool to identify where I should/can use a "using" statement to ensure resources are closed?

c# question - is there a tool to identify where I should/can use a "using" statement to ensure resources are closed? (to avoid memory leaks etc) Including both the cases that: a) there are resources not been closed and b) syntax is using a try-catch-finally and identies this could be changed to a using Thanks ...

using and web service call

What does the using statement do? Is it actually needed? using (MyWebservice x = new MyWebservice()) { //random code } ...

What happens when 'return' is called from within a 'using' block?

If I have a method with a using block like this... public IEnumerable<Person> GetPersons() { using (var context = new linqAssignmentsDataContext()) { return context.Persons.Where(p => p.LastName.Contans("dahl")); } } ...that returns the value from within the using block, does the IDispos...

Are all disposable objects instantiated within a using block disposed?

This is a question I have asked myself many times in the past as I nested using statements 5 deep. Reading the docs and finding no mention either way regarding other disposables instantiated within the block I decided it was a good Q for SO archives. Consider this: using (var conn = new SqlConnection()) { var conn2 = new SqlConne...

Does a using block create and maintain a reference for the GC?

This is mostly for curiosity's sake, as there are better ways of implementing almost any use case I can think of for this construct (in C# and other languages I use regularly, at least), but I recently saw on here a scoped mutex which was a cool concept. My question is, does the using statement maintain a reference (ie: prevent the GC f...

SQLCommand not clearing commandtext in a loop

I ran into something odd, and I'm not precisely sure why it is behaving this way. In a for each loop I am adding rows to a table for a cross reference. Using the following code: For Each cp In pCheckPoints If cp <> String.Empty Then Dim insertSQL As New StringBuilder With insertSQL .Append("INSERT INTO [C...

C# Using keyword- nested in single line

Hi, Usually I was doing something like that (just a example): using (Stream xmlStream = client.OpenRead(xmlUrl)) { using (XmlTextReader xmlReader = new XmlTextReader(xmlStream)) { } } Isn't better to do just: using (XmlTextReader xmlReader = new XmlTextReader(client.OpenRead(xmlUrl))) { } But I'm not sure if in this sh...

Difference in declaring IDisposable member in using block or at using block declaration?

Hi, I have the code below: using (SqlCommand command = new SqlCommand()) { command.CommandType = System.Data.CommandType.StoredProcedure; command.Connection = new SqlConnection(); command.CommandText = ""; command.Parameters.Add(new SqlParameter("@ExperienceLevel", 3).Direction = System.Data.Pa...

Declare 2 types inside using statement gives compile error?

I want to use this line of code: using (ADataContext _dc = new ADataContext(ConnectionString), BDataContext _dc2 = new BrDataContext(ConnectionString)){ // ...} This gives a compile error: Cannot use more than one type in a for, using, fixed or declartion statement. I thought this was possible? MSDN says it is: http://msdn.m...

.NET using block and return; keyword

When I say this using (Entities db = new Entities()) { return db.TableName.AsQueryable().ToList(); } Do I by-pass the functionality of using block since I return something, and the method exits before exiting the using block, so I think the using block will not serve to its purpose and dispose the resource. Is this correct? ...

Using statement question

I have two questions. 1) Should you always use a using statement on a connection? So, I would use it on the connection and then another one on a reader within the connection? So I would be using two using statements. 2) Lets say you use the using statement on the connection and also a reader being returned on the connection. So you ha...

.NET/C# - Disposing an object with the 'using' statement

Hello, Suppose I have a method like so: public byte[] GetThoseBytes() { using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { ms.WriteByte(1); ms.WriteByte(2); return ms.ToArray(); } } Would this still dispose the 'ms' object? I'm having doubts, maybe because something is returned befo...