Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 441
  • Last Modified:

Would like to dispose the con in class

Please chekc the below code its giving me an error:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Collections;
using System.IO;

namespace ConsoleApplication4
{
   
    class GC_Finalize_Dispose_methods : IDisposable
    {
        SqlConnection Con;
       


        //implementing the dispose method IDisposable  should be called be called by client

        public  void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(Con);
        }

        // creating constructor
        public GC_Finalize_Dispose_methods()
        {
            SqlConnection Con = new SqlConnection("User Id=sa; Password=abc; Server=LT-ECMITPCONFAH; Database=expenses");
            Con.Open();
            Console.WriteLine(Con);
            Console.ReadLine();
                    }

        // Calling the finalize method or destructor

        ~GC_Finalize_Dispose_methods()
        {
            Dispose(false);
        }

        public virtual void Dispose(bool disposingcon)

        {
            if (disposingcon)
            {
                Console.WriteLine(Con);
                Con.Close();

                Con = null;
            }
            Con.Dispose();
        }
    }
   
   
   

}


Client which is calling the dispose:

GC_Finalize_Dispose_methods gn = new GC_Finalize_Dispose_methods();
            gn.Dispose();

Error:

dispose method is throwing null exception as value is not being passed
0
aspnetDottech
Asked:
aspnetDottech
  • 5
  • 2
1 Solution
 
DhaestCommented:
If you debug it, on which line does the error occur ?
0
 
aspnetDottechAuthor Commented:
Hi Dhaest,

getting the error in below line:
 public virtual void Dispose(bool disposingcon)

        {
            if (disposingcon)
            {
                Console.WriteLine(Con);
                Con.Close();

                Con = null;
            }
            Con.Dispose();
        }
0
 
DhaestCommented:
On which line in that function ?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
aspnetDottechAuthor Commented:
if (disposingcon)
            {
                Console.WriteLine(Con);
                Con.Close();  // error is here

                Con = null;
            }
            Con.Dispose();
        }
0
 
Alfred A.Commented:
Since your are calling Con.Dispose();, you don't need to call Con.Close() anymore.   Calling Con.Dispose() does the following:

if(Con.State == ConnectionState.Open)
{
    Con.Close();
}
Con = null;

So, in your code, you can just do this:

if (disposingcon)
            {
                Console.WriteLine(Con);
            }
            Con.Dispose();
        }

Also, referring to the note in the excerpt in the link below,  always call Dispose before you release your last reference to the Component. Otherwise, the resources it is using will not be freed until the garbage collector calls the Component object's Finalize method.

http://msdn.microsoft.com/en-us/library/3cc9y48w.aspx
0
 
aspnetDottechAuthor Commented:
I got the solution as i was calling the object the function that was the error.
0
 
aspnetDottechAuthor Commented:
I got the solution as i was calling the object within the function that was the error.
0
 
aspnetDottechAuthor Commented:
Because i could see the first user who was asking clarifications were irrelevant
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now