Question

I don't know why the following query doesn't executed with my expected parameters !!

cmdTxt.Append("UPDATE sd32depart SET currentcredit = currentcredit + ? WHERE year = ? AND main_code = ? ");
paramList.Add("currentcredit", value.ToString().TrimEnd());
paramList.Add("year", year.ToString().TrimEnd());
paramList.Add("main_code", main_code.ToString().TrimEnd());
res = ConnectionObj.Execute_NonQueryWithTransaction(cmdTxt.ToString(), CommandType.Text, paramList);

I get

res = 1and although currentcredit = 180 as a parameter

when i check my table i found currentcredit NULL !!


public int Execute_NonQueryWithTransaction(string cmdText)
            {
                string return_msg = "";
                int return_val = -1;
                //check if connection closed then return -1;
                if (connectionstate == ConnectionState.Closed)
                    return -1;
                command.CommandText = cmdText;
                command.CommandType = CommandType.Text;
                command.Transaction = current_trans;
                try
                {
                    return_val = command.ExecuteNonQuery();
                }
                catch (IfxException ifxEx)// Handle IBM.data.informix : mostly catched
                {
                    return_val = ifxEx.Errors[0].NativeError;
                    return_msg = return_val.ToString();
                }
                catch (Exception ex)// Handle all other exceptions.
                {
                    return_msg = ex.Message;
                }
                finally
                {
                    if (!string.IsNullOrEmpty(return_msg))//catch error
                    {
                        //rollback
                        current_trans.Rollback();
                        Close_Connection();
                    }

                }
                return return_val;
            }
Was it helpful?

Solution

From the comments:

currentcredit is null before the update, what should i do

Ah, that's the problem then. In SQL, null is sticky. null + anything is: null. If this was TSQL (i.e. SQL Server), the solution would be ISNULL:

UPDATE sd32depart SET currentcredit = ISNULL(currentcredit,0) + ?

where the result of ISNULL(x, y) is x if x is non-null, otherwise y. In C# terms, it is the equivalent of x ?? y (and indeed, ISNULL(x, y) is identical to COALESCE(x, y), except that COALESCE is varadic).

So: find the informix equivalent of ISNULL or COALESCE, and use that.

From a brief search, it seems that in informix the NVL function does this, so try:

UPDATE sd32depart SET currentcredit = NVL(currentcredit,0) + ?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top