Error in Update query-Visual Studio 2010- Oracle 11g Express edition

半世苍凉 提交于 2019-12-06 07:44:59

From ORA-01722

The attempted conversion of a character string to a number failed because the character string was not a valid numeric literal. Only numeric fields or character fields containing numeric data may be used in arithmetic functions or expressions. Only numeric fields may be added to or subtracted from dates.

This error may cause probably you try to define your STUDENT_ID as a OracleDbType.Varchar2.

I'm not sure how long your STUDENT_ID but you can use OracleDbType.Int32 instead of.

Like;

updatestud[0] = query.Parameters.Add("STUDENT_ID", OracleDbType.Int32 , STUDENT_ID, ParameterDirection.Input);

Check out OracleType enumeration for more details.

Specifies the data type of a field or property for use in an OracleParameter.

I am thinking STUDENT_ID may be integer. you are using it as Varchar2, which seems to be the cause of error.

ORA-01722: invalid number Cause: The attempted conversion of a character string to a number failed because the character string was not a valid numeric literal. Only numeric fields or character fields containing numeric data may be used in arithmetic functions or expressions. Only numeric fields may be added to or subtracted from dates.

Hi people :) I found the answer myself. Just thought of sharing with you all.

There are 2 methods to accomplish this.

Method 1:

 OracleParameter[] updatestud = new OracleParameter[3];
        updatestud[0] = query.Parameters.Add(":STUDENT_NAME", OracleDbType.Varchar2, STUDENT_NAME, ParameterDirection.Input);
        updatestud[1] = query.Parameters.Add(":STUDENT_ADDRESS", OracleDbType.Varchar2, STUDENT_ADDRESS, ParameterDirection.Input);
        updatestud[2] = query.Parameters.Add("STUDENT_ID", OracleDbType.Int32, STUDENT_ID, ParameterDirection.Input);

Method 2:

            String sql = "UPDATE STUDENT SET STUDENT_NAME = :STUDENT_NAME, STUDENT_ADDRESS= :STUDENT_ADDRESS WHERE STUDENT_ID= :STUDENT_ID";
        OracleCommand query = new OracleCommand(sql, con);

        OracleParameter p_studid = new OracleParameter();
        OracleParameter p_studname = new OracleParameter();
        OracleParameter p_studaddr = new OracleParameter();

        p_studname.OracleDbType = OracleDbType.Varchar2;
        p_studname.Value = TxtName.Text;
        query.Parameters.Add(p_studname);

        p_studaddr.OracleDbType = OracleDbType.Varchar2;
        p_studaddr.Value = TxtAddress.Text;
        query.Parameters.Add(p_studaddr);

        p_studid.OracleDbType = OracleDbType.Int32;
        p_studid.Value = TxtId.Text;
        query.Parameters.Add(p_studid);

You may use any of these methods just remember one thing... Parameters should always be added in the order of the sql. And of course, one must also take care of the OracleDBType.

Also refer to dis page if in case you have doubts-http://docs.oracle.com/cd/E17781_01/appdev.112/e18751/building_odp.htm

Thank you all for yur suggestions! :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!