How to use Syste,.data.common.DbConnection variable ?
-
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.
-
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.
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
-
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.
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
-
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.
-
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
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
-
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
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
-
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
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