Solved

Alter Oracle Sequence in C#

Posted on 2013-05-22
2
1,055 Views
Last Modified: 2013-05-22
I need to alter an Oracle Sequence in my code (C#) and I keep receiving an Invalid Cast error.  I've tried casting to INT64, INT32 and just INT and still get the same error.  Here is my code:

        public Boolean resetSEQ(OracleConnection connectionObj, String owner, String seqname)
        {
            Int64 currVal = 0;
            Boolean success = true;
            OracleCommand command = connectionObj.CreateCommand();
            try
            {
                // Reset Sequence
                ErrMsg = "";

                // Check that the database has been opened
                if (connectionObj == null || connectionObj.State != ConnectionState.Open)
                {
                    ErrMsg = "<br />Database is not opened.";
                    return false;
                }

                // Create Command object
                command.Connection = connectionObj;

                try
                {
                    // Get Current Sequence Value
                    command.CommandText =
                        "SELECT " + owner + "." + seqname + ".nextval FROM dual";

                    currVal = (int)command.ExecuteScalar();
                    ErrMsg = ErrMsg + currVal.ToString();
                }
                catch (Exception ex)
                {
                    ErrMsg = ErrMsg + "<br />Error Retrieving Next Sequence for " + seqname + ": " + ex.Message;
                    success = false;
                }
                //try
                //{
                //    // Get Current Sequence Value
                //    command.CommandText =
                //        "SELECT " + owner + "." + seqname + ".currval FROM dual";

                //    currVal = (Int64)command.ExecuteScalar();
                //    ErrMsg = ErrMsg + currVal.ToString();
                //}
                //catch (Exception ex)
                //{
                //    ErrMsg = ErrMsg + "<br />Error Retrieving Current Sequence for " + seqname + ": " + ex.Message;
                //    success = false;
                //}
            }
            catch (Exception ex)
            {
                ErrMsg = ErrMsg + "<br />ERROR resetSEQ: " + ex.Message;
                success = false;
            }
            finally
            {
                command.Dispose();
            }
            return success;
        }

Open in new window


The code only shows trying to get the current value of the sequence.  If I can get this working I can get the rest working.

Any help or suggestions would be greatly appreciated!
0
Comment
Question by:dyarosh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39187163
Have you tried double or decimal? The numeric types coming back from Oracle, IMO, are not very intuitive. You might also try putting a breakpoint on the ExecuteScalar line and executing the following in the Immediate Window to check the type:

?command.ExecuteScalar().GetType()

Open in new window

0
 

Author Closing Comment

by:dyarosh
ID: 39187345
Casting as type Decimal worked.  Thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question