简体   繁体   中英

Invalid operation given the current state of the object in ExecuteNonQuery()

I'm trying to insert and in which case there is a duplicate key update. Everything is from Oracle and using a procedure but I get this error and I do not know why

public static int GrabarJugador(Jugador_E jugador)
    {
        int respuesta = 0;

        try
        {
            conexion = bd.LeerDeBaseDeDatos();

            orden = new OracleCommand();
            orden.CommandText = "CONSULTAS.grabar_jugador";
            orden.CommandType = CommandType.StoredProcedure;
            orden.Parameters.Add("v_nombre", OracleDbType.Varchar2, 60).Value = jugador.Nombre;
            orden.Parameters.Add("v_equipo", OracleDbType.Varchar2, 50).Value = jugador.EquipoJugador;
            orden.Parameters.Add("v_direccion", OracleDbType.Varchar2, 150).Value = jugador.Direccion;
            orden.Parameters.Add("v_puesto", OracleDbType.Varchar2, 2).Value = jugador.PuestoHab;
            orden.Parameters.Add("v_fec_na", OracleDbType.Date).Value = jugador.Fecha;
            orden.Parameters.Add("v_foto", OracleDbType.Blob).Value = jugador.Foto;

            respuesta = orden.ExecuteNonQuery();

            orden.Dispose();

            bd.CerrarConexion();
        }
        catch (Exception e)
        {
            Console.WriteLine("Error " + e.ToString());
            Console.ReadLine();
        }

            return respuesta;
    }

The code procedure in DB Oracle is this:

PROCEDURE GRABAR_JUGADOR (v_nombre         VARCHAR,
                          v_equipo         VARCHAR2,
                          v_direccion      VARCHAR2,
                          v_puesto_h       VARCHAR2,
                          v_fec_na         DATE,
                          v_foto        IN BLOB)
IS
BEGIN
    INSERT INTO JUGADOR (NOMBRE,
                         DIRECCION,
                         PUESTO_HAB,
                         FECHA_NAC,
                         EQUIPO_JUGADOR,
                         FOTO_JUGADOR)
         VALUES (v_nombre,
                 v_direccion,
                 v_puesto_h,
                 v_fec_na,
                 v_equipo,
                 v_foto);
EXCEPTION
    WHEN DUP_VAL_ON_INDEX
    THEN
        UPDATE jugador
           SET equipo_jugador = v_equipo,
               direccion = v_direccion,
               puesto_hab = v_puesto_h,
               fecha_nac = v_fec_na,
               foto_jugador = v_foto
         WHERE nombre = v_nombre;

  WHEN OTHERS
    THEN
        raise_application_error (
            -20100,
            'Error inexperado: '|| SQLERRM);
END GRABAR_JUGADOR;

I tried if the error is for Blob or Date but isn't this. The error comes out just in Orden.ExecuteNonQuery();

EDIT 1: I repair the error, was the conection in command, but now when i insert/update the picture player (blob) when I charge the photo the program give me 1 error: The parameter is not valid.

EDIT 2: I have seen that the error is when I save the blob in the Oracle DB

if (jugador.Foto != null)
        {
            MemoryStream ms = new MemoryStream();
            ms.Write(jugador.Foto, 0, Convert.ToInt32(jugador.Foto.Length));
            Bitmap bm = new Bitmap(ms, false); //this line break error
            ms.Dispose();
            pictureBox1.Image = bm;
        }

Thanks for help me.

Since you state that you've solved your first issue, I'll just address the bottom one (you should probably edit your question to just be that, since the first part is irrelevant now).

The issue you're seeing is because you write to the end of the stream and then try to read the image with the end of the stream as the starting point . You need to move back to the start of the stream before you can read the image. Your code therefore should be:

if (jugador.Foto != null)
{
    using(MemoryStream ms = new MemoryStream())
    {
        ms.Write(jugador.Foto, 0, Convert.ToInt32(jugador.Foto.Length));
        ms.Seek(0, SeekOrigin.Begin); // seek to the beginning of the stream
        Bitmap bm = new Bitmap(ms, false);
        pictureBox1.Image = bm;
    }
}

I've also moved the MemoryStream into a using block.

Tangential things to note:

  • Microsoft's implementation of MemoryStream means that this isn't a problem, but if you were writing to a file, or a network socket, etc. you may have to call .Flush() to ensure that the data has been sent/saved and isn't still just stuck in a temporary memory store.
  • Not all Streams are seekable, and thus have a CanSeek property (in addition to CanRead and CanWrite ) to indicate if you can seek.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM