Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can I dispose dataset when returning the dataset 2

Status
Not open for further replies.

compu66

Programmer
Dec 19, 2007
71
US
Hi all,

If we dont dispose the objects in finally what happens??
In my opinion if the objects are not disposed properly the application might run slowly because of improper resources.Am i right??

IF i am returning a dataset then in finally block if I dispose the dataset ,in any of the situation is it wrong.

OR suppose if I dont dispose,and there are so many dataset objects in the application.and in all I am retruning the ds.
Will that cause the resources scarcity or the application will become slow.??

I am little confused.Can any one clear my confusion.

It will be of great help.

Thanks.


 
usually it's a good idea to dispose of resources as soon as possible. if you don't the system will eventually clean up after itself.

I have found the 2 most important objects to dispose of are database connections, and streams/file access. I'm sure objects which access unmanaged code would also fall into this category, but I haven't directly accessed unmanaged code in my apps.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
hey I have one more question.I am returning ds in a function and in finally if i dispose what are the effects.Is it good practice or may end up landing me in to errors.
 
Once you have finished with an object then it's good practice to dispose of it. If you are unsure what will happen though, the best advice is to simply try it and see.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
i try to dispose of any objects which implement IDisposable. will that lead to errors. only if you attempt to utilize code after it's been disposed/gone out of scope.

if you rely heavily on drap/drop controls and declarative syntax then i would let the system automanage the objects. if you write a majority of your code by hand, then you should manage the objects according to your needs.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
HI
Thanks for ur good response. hey I have tried it.
In the below case I think because of little knowledge i might be getting confuseed but as far as i remmember once when i did the below it was fine and the other time it gave me an error "u cant reader when the reader is close"

Public Function executeReader(ByVal sSQL As String) As SqlDataReader
Dim errorMsg As String
Dim cnn As SqlConnection = Nothing
Dim cmd As SqlCommand = Nothing
Try
cnn = New SqlConnection(ConnString)
cmd = New SqlCommand(sSQL, cnn)

cnn.Open()
cmd.CommandTimeout = 60

Return cmd.ExecuteReader(CommandBehavior.CloseConnection)
Catch ex As Exception
errorMsg = "Error Executing Reader. SQL=" & sSQL
'--- Throw error to DALException class for

Throw New DALException(errorMsg, ex)

Finally
cnn.Close()------------------------------1
cmd.Dispose()
cnn.dispose()-----------------------1


End Try
End Function
 
the problem is your returning the datareader. this object requires direct access to the database. instead load the values from the datareader into another object and retrun that object. this easist is a datatable.

here's the code is c#.
Code:
public DataTable GetData()
{
   DataTable table = new DataTable();
   using (IDbConnection cnn = new SqlConnection())
   {
      IDbCommand cmd = new SqlCommand("select * from foo", cnn);
      IDataReader reader = cmd.ExecuteReader();
      while(reader.Read())
      {
         table.Rows.Add(reader.ItemArray);
      }
   }
   return table;
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
As Jason said, a DataReader isn't a disconnected object. The object needs to be connected directly to the data, so disposing of the object will result in an error when trying to read the data.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
In the above I am sure from your explaination that if I remove "cnn.close()".I will not get the error.
But one thing i am not clear is except cnn.close(),if i dispose other objects like cmd.dispose() and cnn.dispose() in the above code which I sent, is that good way of coding.Will it cause any errors.



Or simply u want me to delete the finally block completely.if i should not dispose or close in the above case of my code.
 
don't be afriad of breaking code or errors. it happens, that's part of developement.

in some forms of development(strict red/green testing) it's considered short-cutting if a test doesn't fail on the first pass because the function hasn't been implemented.

also, if your unsure about how an object, interface or keyword works; google it.

as for my code above. I don't explicitly call close or dispose, because the using block does this automatically.
the follow to code blocks preform the same functionality.
Code:
public DataTable GetData()
{
   DataTable table = new DataTable();
   using (IDbConnection cnn = new SqlConnection())
   {
      IDbCommand cmd = new SqlCommand("select * from foo", cnn);
      IDataReader reader = cmd.ExecuteReader();
      while(reader.Read())
      {
         table.Rows.Add(reader.ItemArray);
      }
   }
   return table;
}
Code:
public DataTable GetData()
{
   DataTable table = new DataTable();
   IDbConnection cnn = new SqlConnection();
   IDbCommand cmd = new SqlCommand("select * from foo", cnn);
   try
   {
      cnn.Open();
      IDataReader reader = cmd.ExecuteReader();
      while(reader.Read())
      {
         table.Rows.Add(reader.ItemArray);
      }
   }
   finally
   {
      cnn.Close();
      cnn.Dispose();
      cmd.Dispose();

      cnn = null;
      cmd = null;
   }
   return table;
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top