Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object reference not set to an instance of an object

Status
Not open for further replies.

RamHardikar

Programmer
Feb 16, 2001
109
GB
Hi,

Below is a function that i call to get the rowcount. When it is called for the first time it returns me the rowcount. But when i call this for the 2nd time, it fails at ExecuteScalar() line of code and throws me an error (Object reference not set to an instance of an object). Could any one tell me why it fails?

Code:
private int getRowCount(string strSQL)
{
 OleDbConnection cn;

 MyOleDBConnection OleDB = new MyOleDBConnection ("Provider=MSDAORA.1;User ID=test;Data   Source=mydb;Password=test");

 cn = OleDB.getMyOleDBConnection();
 OleDbCommand cmd = new OleDbCommand();
 cmd.CommandText = strSQL;   
 cmd.Connection = cn;

 int iRowCount = Int32.Parse(cmd.ExecuteScalar().ToString());
 OleDB.closeDbConnection(cn);
 return iRowCount;
}
 
In the 2nd call to the function, the SQL string that i was passing was wrong. I missed out the "count(*)" part and had given just "*" in the SQL. So that was causing all the trouble.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top