Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to use Syste,.data.common.DbConnection variable ?

How to use Syste,.data.common.DbConnection variable ?

Scheduled Pinned Locked Moved C#
tutorialquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nicolus
    wrote on last edited by
    #1

    I am trying to use the DbConnection and i used the following code. public DbConnection mConnection = null; mConnection.ConnectionString =value; //value has the connection string but at the runtime its giving me "Object reference not set to an instance of an object." Exception. Can any body pls tell me how to use DbConnection. Thanks.

    T N N 3 Replies Last reply
    0
    • N nicolus

      I am trying to use the DbConnection and i used the following code. public DbConnection mConnection = null; mConnection.ConnectionString =value; //value has the connection string but at the runtime its giving me "Object reference not set to an instance of an object." Exception. Can any body pls tell me how to use DbConnection. Thanks.

      T Offline
      T Offline
      Talal Sultan
      wrote on last edited by
      #2

      The problem is that your object mConnection has not been instantiated. You need to instantiate a class to be able to use it. Instantiating means creating an instance, exactly what the exception message is telling you :) You do this by calling the constructor of the class.

      // Create the class mConnection. Now the class has a place in memory.
      public Dbconnection mConnection = new DbConnection();
      mConnection.ConnectionString = value; // this should work now as the class exists.

      Voila :) Talal

      -- If this is a post that has been helpful to you, please vote for it. Thank you! "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook

      C 1 Reply Last reply
      0
      • N nicolus

        I am trying to use the DbConnection and i used the following code. public DbConnection mConnection = null; mConnection.ConnectionString =value; //value has the connection string but at the runtime its giving me "Object reference not set to an instance of an object." Exception. Can any body pls tell me how to use DbConnection. Thanks.

        N Offline
        N Offline
        Nissim Salomon
        wrote on last edited by
        #3

        Hi It seems like you are working with the wrong DB Connection class. In order to connect SqlServer Database use the SqlConnection under the System.Data.SqlClient namespace otherwise you can use the OleDbConnection under the System.Data.OleDb namespace

        1 Reply Last reply
        0
        • N nicolus

          I am trying to use the DbConnection and i used the following code. public DbConnection mConnection = null; mConnection.ConnectionString =value; //value has the connection string but at the runtime its giving me "Object reference not set to an instance of an object." Exception. Can any body pls tell me how to use DbConnection. Thanks.

          N Offline
          N Offline
          nicolus
          wrote on last edited by
          #4

          I got the Solution. Actually that is a very generic database connection class which can be instantiated with any type of connection like oledb,sql etc. i solved that problem by public DbConnection mConnection =new OleDbConnection(); :)

          1 Reply Last reply
          0
          • T Talal Sultan

            The problem is that your object mConnection has not been instantiated. You need to instantiate a class to be able to use it. Instantiating means creating an instance, exactly what the exception message is telling you :) You do this by calling the constructor of the class.

            // Create the class mConnection. Now the class has a place in memory.
            public Dbconnection mConnection = new DbConnection();
            mConnection.ConnectionString = value; // this should work now as the class exists.

            Voila :) Talal

            -- If this is a post that has been helpful to you, please vote for it. Thank you! "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            Talal Sultan wrote:

            public Dbconnection mConnection = new DbConnection(); mConnection.ConnectionString = value; // this should work now as the class exists.

            Except it won't work becuase DbConnection[^] is an abstract class and cannot be instantiated. You need to use a concrete class derived from DbConnection (e.g. SqlConnection)


            -- Always write code as if the maintenance programmer were an axe murderer who knows where you live. Upcoming FREE developer events: * Glasgow: Agile in the Enterprise Vs. ISVs, Mock Objects, SQL Server CLR Integration, Reporting Services, db4o ... * Reading: SQL Bits My website

            T 1 Reply Last reply
            0
            • C Colin Angus Mackay

              Talal Sultan wrote:

              public Dbconnection mConnection = new DbConnection(); mConnection.ConnectionString = value; // this should work now as the class exists.

              Except it won't work becuase DbConnection[^] is an abstract class and cannot be instantiated. You need to use a concrete class derived from DbConnection (e.g. SqlConnection)


              -- Always write code as if the maintenance programmer were an axe murderer who knows where you live. Upcoming FREE developer events: * Glasgow: Agile in the Enterprise Vs. ISVs, Mock Objects, SQL Server CLR Integration, Reporting Services, db4o ... * Reading: SQL Bits My website

              T Offline
              T Offline
              Talal Sultan
              wrote on last edited by
              #6

              oops, right about that. Sorry. I knew something was wrong with that class, I just didn't take the time to investigate more. Sorry about that. My mistake.

              -- If this is a post that has been helpful to you, please vote for it. Thank you! "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook

              V 1 Reply Last reply
              0
              • T Talal Sultan

                oops, right about that. Sorry. I knew something was wrong with that class, I just didn't take the time to investigate more. Sorry about that. My mistake.

                -- If this is a post that has been helpful to you, please vote for it. Thank you! "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook

                V Offline
                V Offline
                Vasudevan Deepak Kumar
                wrote on last edited by
                #7

                I was thinking he was messing out on something on a DAL factory front and not properly using the IDBConnection.

                Vasudevan Deepak Kumar Personal Homepage Tech Gossips

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups