1

Working in C#, I'm having trouble with a function. Function runs a query and the query is supposed to return one integer value but I'm having trouble returning it. I keep getting errors like:

  • Unable to cast object of type oleDbDataReader to type Int32
  • Specified cast is not valid

Not sure how to do this with C# and OleDbDataReader. My code is below

     public static int FifthQuery()
     {
         int _value = 0;
         OleDbConnection _connectMe = Utilities.OledbConnect();

         OleDbCommand _query1 = new OleDbCommand();
         _query1.Connection = _connectMe;
         _query1.CommandText = "SELECT count(*) FROM GIS.PERSONS where Name_Prefix = 'Dr.'";
         OleDbDataReader _reader = _query1.ExecuteReader();

         _reader.Read();

              //_value = Convert.ToInt32(_reader);
              _value = _reader.GetInt32(0);


         return _value;
     }
3
  • What is the exact problem you are having? Commented May 6, 2014 at 19:17
  • Trying to return an integer but I keep getting the errors about cast and type Commented May 6, 2014 at 19:58
  • Exactly what error? If you want people to help you, tell them what the problem is! Commented May 6, 2014 at 19:58

1 Answer 1

3

Since you using COUNT(*), using ExecuteScalar would be better approach.

Executes the query, and returns the first column of the first row in the result set returned by the query.

int _value = (int)_query1.ExecuteScalar();

Also use using statement to dispose your OleDbConnection and OleDbCommand.

using(OleDbConnection _connectMe = Utilities.OledbConnect())
using(OleDbCommand _query1 = _connectMe.CreateCommand())
{
    _query1.CommandText = "SELECT count(*) FROM GIS.PERSONS where Name_Prefix = 'Dr.'";
    _connectMe.Open();
    int _value = (int)_query1.ExecuteScalar();
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the post. I have tried adjusting my funciton but on the line: int _value = (int)_query1.ExecuteScalar(); I keep getting an error saying that the specified cast is not valid. I'm not sure if I'm missing some library or why this is happening. I also placed "Return _value" before the last closing bracket of the second 'Using' statement. Not sure if that poses any issues as well.
Are you sure you get this error or int _value = (int)_query1.ExecuteScalar(); line? I don't think so because since your query returns COUNT(*) it will be a valid integer in my opinion.
There was some kind of type error. I can't remember what it was exaclty. But I did end up changing th type to an object rather than an integer ----> object _myObj = _query1.ExecuteScalar(); I then added another line to conver the object to an integer and this seemed to work out fine ---> int _value = Convert.ToInt32(_myObj); Not the prettiest way but it works. I still am not sure what could have been the problem before with the specified cast not being valid. I do appreciate your feedback and concern. THanks for everything.
The value is 23. But I agree, your line should have worked but I was getting the error: "Specified cast is not valid". This came from my exception error message when I put the code in a Try-Catch block. Again, it may be some wierd libraries or references or config files that aren't set up correctly, or something is missing I'm just not sure. But regardless, things are working right now. Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.