locked
Increment for SQL value RRS feed

  • Question

  • Hi,
        
    This might be a rather noob question but i'm still very new to the language.

    This is the situation...I have connected to my Oracle database and grabbed an INT number i wanted and display into my form textbox.No prob thus far...But i want it to +1 or increment and at lost of how to do so.

    localSQL = "SELECT MAX(ITEM_ID) FROM SYS_ITEM";  
                        OracleCommand cmdOracle = new OracleCommand(localSQL, localConnection);  
                        cmdOracle.CommandType = CommandType.Text;  
                        OracleDataReader drOracle = cmdOracle.ExecuteReader();  
                            if (drOracle.Read())  
                            {addlocaidBox.Text = drOracle.GetOracleValue(0).ToString();} 

    my code look like this i've tried addlocaidBox.Text = drOracle.GetOracleValue(0).ToString()+1; which only adds a 1 after the number...
    Tuesday, February 3, 2009 7:13 AM

Answers

All replies

  • Never used the Oracle way of doing things as I tend to use SQLClient but what you're doing there is concatenating a string togather. If you want to simply add 1 to the number then you must convert it into an int like:

    System.Windows.Forms.MessageBox.Show(Convert.ToInt32(drOracle.GetOracleValue(0) + 1).ToString());

    Replace the messagebox with your textbox control. What were doing here is converting the value to an int32 and then incrementing it by 1 followed by stringing it as it will cause an error saying unable to convert int to string. This will be shown depending on how strict your compiler settings are.
    Tuesday, February 3, 2009 9:53 AM
  •  Like you guessed OnamC bumped into errors...

    addlocaidBox.Text =(Convert.ToInt32(drOracle.GetOracleValue(0) + 1).ToString()); 
    It prompts...
    System.InvalidCastException: Unable to cast object of type 'Oracle.DataAccess.Types.OracleDecimal' to type 'System.|Convertible'.
    at System.Convert.ToInt32(Oject value)

    addlocaidBox.Text = (drOracle.GetOracleValue(0) + 1).ToString(); 

    The underlined section got a compilation error of
    Operator '+' cannot be applied to operands of type 'object' and 'int'

    addlocaidBox.Text = (drOracle.GetOracleValue(0) )+1.ToString(); 

    But resulted in the number with a 1 behind it...

    Wednesday, February 4, 2009 8:00 AM
  • I've got it working by using this...
    addlocaidBox.Text = OracleDecimal.Add(drOracle.GetOracleDecimal(0),1).ToString();

    Thanks OnamC for u're suggestions.
    Wednesday, February 4, 2009 10:33 AM