IBM.Data.DB2.iSeries idb2Command crash when insert parameter with empty string

461 Views Asked by At
var ibmConnnectionString = "DataSource=10.12.61.12;Naming=SQL;LibraryList=LINSYS;UserID=*****;Password=*****;";

using (iDB2Connection db2Conn = new iDB2Connection(ibmConnnectionString))
            {
                db2Conn.Open();
                //  using (iDB2Transaction trans = db2Conn.BeginTransaction())
                {
                    using (iDB2Command cmd = db2Conn.CreateCommand())
                    {
                        // cmd.Transaction = trans;
                        cmd.CommandText = @"INSERT INTO LINSYS.ALENBN (
                                    POLNUM, FEDRNUM, BENFCD, BNSURN, BNNAME, BNFANM, CSBRDT, BNPART, CSADT)
                                    VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8)";

                        cmd.DeriveParameters();

                        cmd.Parameters["@p0"].Value = 148690; //iDB2Numeric
                        cmd.Parameters["@p1"].Value = 11001; //iDB2Numeric
                        cmd.Parameters["@p2"].Value = 99;    //iDB2Numeric                     
                        cmd.Parameters["@p3"].Value = String.Empty; //iDB2Char Not NULL Default ('')
                        cmd.Parameters["@p4"].Value = String.Empty; //iDB2Char Not NULL Default ('')
                        cmd.Parameters["@p5"].Value = String.Empty; //iDB2Char Not NULL Default ('')
                        cmd.Parameters["@p6"].Value = 0; //iDB2Numeric

                        cmd.Parameters["@p7"].iDB2Value = 100M;//iDB2Numeric
                        cmd.Parameters["@p8"].iDB2Value = String.Empty; //iDB2Char Not NULL Default ('')

                        try
                        {

                            cmd.ExecuteNonQuery();
                        }
                        catch(iDB2SQLErrorException ex)
                        {
                            Console.WriteLine(ex);
                        }


                    }
                }

When executing the command ExecuteNonQuery, console app stops with no error If I omit the iDb2Char Parameters or these parameters contains any single characters (even space) then the command working successfully. The problem is when I pass String.Empty Any idea?

1

There are 1 best solutions below

9
Neil On

My guess is that you are getting a non iDB2SQLErrorException.

Try this:

try
{
    cmd.ExecuteNonQuery();
}
catch(iDB2SQLErrorException ex)
{
    Console.WriteLine(ex);
}
catch(Exception ex)
{
     Console.Writeline(ex);
}

Then if there are ANY exceptions, you will know what the problem was. It could be something like the field doesn't allow 0 length strings and for some reason it throws ArgumentOutOfRangeException.