2

I dont know why but I'm getting an error trying to get the maximum integer value from a table in the oracle database: Here is the code:

string oradb = "Data Source=";
            oradb = oradb + Login.db + ";";
            oradb = oradb + "User Id=" + Login.user;
            oradb = oradb + ";Password=" + Login.pass + ";";
            OracleConnection conn = new OracleConnection(oradb);
            conn.Open();
            string term = GetTerminal(terminal);
            string sql = "SELECT tallynumber from "+frmSchemas.schema + ".tallies" ;
             MessageBox.Show(sql);
            OracleCommand cmd = new OracleCommand(sql, conn);
            cmd.CommandType = CommandType.Text;
            OracleDataReader dr = cmd.ExecuteReader();
            MessageBox.Show("1");
            Int64 TallyNo = dr.GetInt32(0);  



      // lblTallyNo.Text = (TallyNo).ToString()

the returned value should be: 72332 if that is any relevance

1
  • 2
    So what does the error message say and on what line does it occurr? Commented Jul 18, 2011 at 10:06

3 Answers 3

3

You can try with the Following

        string sql = "SELECT max(tallynumber) from "+frmSchemas.schema + ".tallies" ;
         MessageBox.Show(sql);
        OracleCommand cmd = new OracleCommand(sql, conn);
        cmd.CommandType = CommandType.Text;
        OracleDataReader dr = cmd.ExecuteReader();
        MessageBox.Show("1");
        if (dr.Read())
        {
        Int64 TallyNo = dr.GetInt32(0); 
        }

EDIT:

if(dr.Read())
{
Int64 TallyNo = Convert.ToInt64(dr["tallynumber"].ToString());
}
Sign up to request clarification or add additional context in comments.

3 Comments

ExecuteScalar is not being recognised by the C#, it underlines it
int TallyNo = Convert.ToInt32(dr[0].ToString());
Ok, You can try with the Code I have given also.
1

You are missing a dr.Read(); before accessing the value:

OracleDataReader dr = cmd.ExecuteReader();
dr.Read();
Int64 TallyNo = dr.GetInt32(0);

Comments

1

If you want to find the max value from a table, the query should be

SELECT max(tallynumber) from ....

If you want to get the max, min etc values you should be calling ExecuteScalar() instead of ExecuteReader().

So you code could become

   string sql = "SELECT Max(tallynumber) from "+frmSchemas.schema + ".tallies";              MessageBox.Show(sql);             
    OracleCommand cmd = new OracleCommand(sql, conn);             
    cmd.CommandType = CommandType.Text;             
    object val = cmd.ExecuteScalar();
int res = int.MinValue;             
    //MessageBox.Show("1");
  if(int.TryParse(val.ToString(), out res)) 
    Int64 TallyNo = res;

1 Comment

Try it now, changed if(int.TryParse(val, out res)) To if(int.TryParse(val.ToString(), out res))

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.