[RESOLVED] [2.0] OledbDataAdapter
Hi all :wave:
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();
}
}
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.
Re: [2.0] OledbDataAdapter
Thanks sir
Is it necessary to close before dispose the oledbbDataAdapte?
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.
Re: [2.0] OledbDataAdapter
But the Text box has also the dispose Method so Dispose it also ?? :confused:
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.
Re: [2.0] OledbDataAdapter
Great Answer
Thanks :thumb: