|
-
Apr 17th, 2007, 08:16 AM
#1
Thread Starter
Just Married
[RESOLVED] [2.0] OledbDataAdapter
Hi all
If you have a function that return the dataset
you are using the oleddataadapter and the oledbconnection in it.
all code in the try catch block
In finally statement I am closing the connection, but oledbdataadapter.Dispose is necessary or not?
c# Code:
public DataSet ListItems(long cartId, long userId)
{
this._cartId = cartId;
this._userId = userId;
con.Open();
try
{
OleDbDataAdapter adapter = new OleDbDataAdapter("Select * from Cart where nCartID='" + cartId + "' And nUserID='" + userId + "'", con);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
-
Apr 17th, 2007, 06:09 PM
#2
Re: [2.0] OledbDataAdapter
The rule is that you should call the Dispose method of any disposable objects you create once you're finished with them. If you create an object that has a Dispose method then you should dispose it. The easiest way to do that with local variables is with a 'using' block.
-
Apr 17th, 2007, 11:33 PM
#3
Thread Starter
Just Married
Re: [2.0] OledbDataAdapter
Thanks sir
Is it necessary to close before dispose the oledbbDataAdapte?
-
Apr 18th, 2007, 12:00 AM
#4
Re: [2.0] OledbDataAdapter
DataAdapters aren't ever "open" so closing them is meaningless. The connection is an independent object so it has no effect on the adapter in that regard. The adapter doesn't know, or care, whether a connection is open or closed until it tries to use it.
-
Apr 18th, 2007, 12:51 AM
#5
Thread Starter
Just Married
Re: [2.0] OledbDataAdapter
But the Text box has also the dispose Method so Dispose it also ?? 
-
Apr 18th, 2007, 01:22 AM
#6
Re: [2.0] OledbDataAdapter
When a form is disposed it will dispose all the controls it contains, which is how disposal is supposed to work. That is true even if you created a control at run time. If it's on the form when the form is disposed then it will be disposed. If you remove a control from a form at run time then you should dispose the control because if the control isn't on the form then the form won't do it for you.
-
Apr 18th, 2007, 01:27 AM
#7
Thread Starter
Just Married
Re: [2.0] OledbDataAdapter
Great Answer
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|