1

Hi everyone. I am selecting a column from a table which returns a field with a number. I wish to return this number to an integer in c# so that I can increment the integer by 1 after getting it. I am using visual studio 2010,C# and oracle as my database

This is my code: **Hi guys. I tried as you told me but it did not work. It is crashing--+ $exception {"Unable to cast object of type 'Oracle.DataAccess.Client.OracleDataReader' to type 'System.IConvertible'."} System.Exception {System.InvalidCastException}

**

public static int GetRunNumber(string date)
    {
        int result;

        DatabaseAdapter dba = DatabaseAdapter.GetInstance();
        string sqlQuery = "SELECT RUN FROM LOAD_CONTROL " +
                          "WHERE START_DATE = (SELECT MAX(START_DATE) " +
                          "FROM LOAD_CONTROL " +
                          "WHERE LOAD_DATE = to_date('" + date + "', 'dd/mm/yyyy')) " +
                          "AND LOAD_DATE = to_date('" + date + "', 'dd/mm/yyyy') ";

        result = Convert.ToInt32(dba.QueryDatabase(sqlQuery));
        return result;
    } 
5
  • 4
    BTW: don't use sql-queries with string-concatenation of input data (search for SQL-injection) Commented Aug 22, 2011 at 9:32
  • 2
    Just as usual process of converting to integer, for ex. as already mentioned Convert.ToInt32(...) Commented Aug 22, 2011 at 9:34
  • @CKoenig and Incognito--I tried to do as you told me but it gave me the above error as shown in my edited code.thanks Commented Aug 22, 2011 at 9:47
  • 1
    I changed my answer - this should work Commented Aug 22, 2011 at 9:54
  • You have a duplicated entry in your where clause 'LOAD_DATE ='. Commented Aug 22, 2011 at 10:17

2 Answers 2

2

your comment suggests that the result will be a OracleDataReader - in this case use



var reader = dba.QueryDatabase(sqlQuery);
if(reader.Read())
{
  return reader.GetInt32(0);
}
// else error
throw new Exception("no result found");
Sign up to request clarification or add additional context in comments.

3 Comments

CKoenig--I am not understanding what you are telling me exactly. Can you please edit my code to what you are trying to tell me please. Thanks for your patience. Mike
you can remove the while since it returns the first row and leaves method body anyway.
sure - why not; @mikespiteri: this is almost a edit of your code - just replace everything from "result = ... " with it (or set result = reader.GetInt32(0); instead of "return reader.GetIn32(0) - but then you have to change the error-handling)
2

You can use Parse or TryParse methods of Int32 Structure.

Edit : I found this solution on Oracle's documents. It is a different approach but might work for you.

// Connection string for your app
string constr = "User Id=scott;Password=tiger;Data Source=oracle"; 

// Creates new connection object.
OracleConnection con = new OracleConnection(constr);
con.Open();

// Change below with your query.
string cmdstr = "SELECT * FROM EMPINFO";
OracleConnection connection = new OracleConnection(constr);
OracleCommand cmd = new OracleCommand(cmdstr, con);
OracleDataReader reader = cmd.ExecuteReader();

// Returns the first column of the first row returned from your query and 
// converts it to Int32. You should replace '0' with the column no you desire.
return reader.GetInt32(0);

For more info you can check the document I mentioned.

Comments

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.