Results 1 to 7 of 7

Thread: [RESOLVED] [2.0] OledbDataAdapter

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Resolved [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:
    1. public DataSet ListItems(long cartId, long userId)
    2.         {
    3.             this._cartId = cartId;
    4.             this._userId = userId;
    5.             con.Open();
    6.            
    7.             try
    8.             {
    9.                 OleDbDataAdapter adapter = new OleDbDataAdapter("Select * from Cart where nCartID='" + cartId + "' And nUserID='" + userId + "'", con);
    10.                 DataSet ds = new DataSet();
    11.                 adapter.Fill(ds);
    12.                 return ds;
    13.                
    14.             }
    15.             catch (Exception ex)
    16.             {
    17.                 throw ex;
    18.             }
    19.             finally
    20.             {
    21.                 con.Close();
    22.              
    23.             }
    24.  
    25.         }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2.0] OledbDataAdapter

    Thanks sir
    Is it necessary to close before dispose the oledbbDataAdapte?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2.0] OledbDataAdapter

    But the Text box has also the dispose Method so Dispose it also ??

    c# Code:
    1. this.TextBox1.Dispose()

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Thumbs up 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
  •  



Click Here to Expand Forum to Full Width