Search code examples
c#db2ibm-midrange

idb2 connection not allowed with c#


i have a little problem with my connection with the AS400.I am using c#. When i want to do an insert sql statement on a table, it pops this message

SystemInvalidOperationException : This operation cannot be successful because the connection is not allowed at IBM.Data.DB2.iSeries.iDB2Command.verifyConnection(); at IBM.Data.DB2.iSeries.iDB2Command.ExecuteNonQuery();

here is my definition of the connection string

    public static string userID;
    public static string passwd;
    public static string system;
    public string query;
    public iDB2Connection conn = new iDB2Connection("DataSource=" + system + ";UserID=" + userID + ";Password=" + passwd + ";DataCompression=true;");

and the code that contains the insert statement

       public  void insert(Programs prog, int nbfiche)
        {
        //conn.Open();
        try
        {


            string sqlQuery = "INSERT INTO DIIAB.FICDET(MTPRO,MTFICH,MTPGM,MTNSRC,MTLSRC,MTTYP,MTOBJT) VALUES('" + Progiciel + "','" + nbfiche + "','" + prog.program_name +
                "','" + prog.source_program + "','" + LIB + "','" + prog.element_type + "','" + prog.program_type + "')";


            iDB2Command iDB2Command = conn.CreateCommand();
            iDB2Command.CommandText = sqlQuery;
            iDB2Command.ExecuteNonQuery();
            sqlQuery = "select MTFICH from DIIAB.FICDET where MTFICH='" + nbfiche + "'";
            iDB2Command command = conn.CreateCommand();
            command.CommandText = sqlQuery;
            iDB2DataReader reader = command.ExecuteReader();
            while (reader.Read())
            {

                if (reader[0].ToString().Contains(nbfiche.ToString()))
                {
                    System.Windows.MessageBox.Show("Un programme à été rajouté à la fiche.");
                }



            }

            System.Windows.MessageBox.Show("Les programmes ont été rajouté à la fiche", "Information");


        }
        catch (Exception e)
        {
            System.Windows.MessageBox.Show(e.ToString());
        }

    }

and the code that call the method insert with the parameters

         edit.userID = userID;
                            edit.passwd = passwd;
                            edit.system = system;
                            edit editeur = new edit();

                            editeur.nbfiche = Convert.ToInt32(daoficnbr.fICNBR.nb_fiche);
                            editeur.fiche_status = Statuss.Text;
                            editeur.Progiciel = PRO.Text;
                            editeur.getpgm(arcad.lib,daoficnbr.fICNBR.nb_fiche);
                            foreach (Programs p in editeur.content)
                            {
                                editeur.insert(p, editeur.nbfiche);
                            }

Could help me please it's been already 2 days i am stuck on this one


Solution

  • Solution was to ensure that the connection string was terminated by a semi-colon and that the conn.Open() completed successfully before running commands or queries.