List:MySQL ODBC« Previous MessageNext Message »
From:jbonnett Date:October 8 2003 12:46am
Subject:RE: Using select count(*) in c# app
View as plain text  
Do you know exactly which line is throwing the exception? Are con and
myCommand valid references?

I presume you have seen the VB example doing this same thing.

Dim SQLStatement As String = "SELECT Count(CustomerID) FROM Customers"
Dim RecordCount As Integer
Dim SelectCommand As New SqlClient.SqlCommand(SQLStatement, SqlConnection1)
Try
    SqlConnection1.Open()
    RecordCount = CInt(SelectCommand.ExecuteScalar())
Catch ex As Exception
    MessageBox.Show("Failed to execute command")
Finally
    SqlConnection1.Close()
End Try
MessageBox.Show("Customers table contains " & RecordCount & " records")

John Bonnett

-----Original Message-----
From: Firdaus Aryana [mailto:vamanose@stripped]
Sent: Tuesday, 7 October 2003 3:18 AM
To: myodbc@stripped
Subject: Using select count(*) in c# app


Hi,
I am trying to run the following query in a C# app,
but with no success.

Select count(*) from tblUser

Here is the code snippet:

string myQuery=tbQuery.Text;
OleDbConnection con=new
OleDbConnection("Provider=MySQLProv;User
ID=root;Password=;Data Source=test;");

OleDbCommand myCommand=new OleDbCommand(myQuery, con);
try {
				myCommand.Connection.Open();
	if (con.State==ConnectionState.Open)
	   Int32 res=(Int32)myCommand.ExecuteScalar();

	con.Close();
     }
     catch(Exception ex) {
	MessageBox.Show(ex.Message);
     }

The exception thrown is:
"Object reference not set to an instance of an
object."

If I replace the line where the query is executed:

Int32 res=(Int32)myCommand.ExecuteScalar();

with

Object Count = myCommand.ExecuteScalar();

"Count" remains undefined (null).

Any insight will be helpful.

Thanks,

=====


__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com
Thread
Using select count(*) in c# appFirdaus Aryana6 Oct
RE: Using select count(*) in c# appjbonnett8 Oct