How to call an image from C #, stored in SQL Server. Using Stored Procedure and Entity Framework

1

I have this code made so far where I apply the SP "InsertEmployee", it adds records to the BD:

var Insertar_NuevoEmpleado = _empleados.InsertarEmpleado(txtcedula.Text, txtnombres.Text, txtapellidos.Text, txtcargo.Text, txtdepartamento.Text, Convert.ToDecimal(txtsalario.Text), txtUrl.Text);

What I try to do is a search with another SP where I bring the records corresponding to the parameter sent. It happens that, the form has a PictureBox, by means of which an image is stored in the database ( itself the route where the image is stored is stored ) and this is the search code:

 private void btnBuscarEmpleado_Click(object sender, EventArgs e)
    {
        btnBuscarEmpleado.Image = imageList1.Images[0];

        if (string.IsNullOrEmpty(txtBuscarCedula.Text))
        {
            MessageBox.Show("Ingrese un número de cédula valido", "Buscar Empleado", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return;
        }

        empresaEntities _empleados = new empresaEntities();
        {
            nomina _trabajador = new nomina();

            var BuscarEmpleado = _empleados.BuscarEmpleados(txtBuscarCedula.Text).ToList();

            if (BuscarEmpleado.Count() > 0)
            {
                MessageBox.Show("El empleado existe en la base de datos.", "¡ATENCIÓN!", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                txtcedula.Text = BuscarEmpleado.FirstOrDefault().cedula.ToString("N0");
                txtnombres.Text = BuscarEmpleado.FirstOrDefault().nombres;
                txtapellidos.Text = BuscarEmpleado.FirstOrDefault().apellidos;
                txtcargo.Text = BuscarEmpleado.FirstOrDefault().cargo;
                txtdepartamento.Text = BuscarEmpleado.FirstOrDefault().departamento;
                txtsalario.Text = BuscarEmpleado.FirstOrDefault().salario.ToString("N2");
                txtUrl.Text = BuscarEmpleado.FirstOrDefault().UrlFoto;
                pictureBox1.Image = Image.FromFile(BuscarImagen.FileName); **<- AQUI me marca el ERROR**
            }

            else
            {
                MessageBox.Show("El número de cédula no existe en la base de datos. ¿Desea ingresar uno nuevo?", "Confirmar", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            }

        }
    }

It is evident that the pictureBox has no code. The question is, how do I make the corresponding call for this object, where it shows me the image to which the parameter sent by the SP is associated.

I comment:

The SP obviously works for me without the Image parameter, but the idea is to bring the records and in turn to show the image too.

For more info:

I search for the image using the following code and store the image path:

 private void btnBuscarFoto_Click(object sender, EventArgs e)
    {
        OpenFileDialog BuscarImagen = new OpenFileDialog();
        BuscarImagen.Filter = "Archivo PNG|*.png";

        if (BuscarImagen.ShowDialog() == DialogResult.OK)
        {
            pictureBox1.Image = Image.FromFile(BuscarImagen.FileName);
        }
            txtUrl.Text = BuscarImagen.FileName;
    }

In addition to this, when I do the Debugg of the application, I mark error in this line when trying to do the search.

 pictureBox1.Image = Image.FromFile(BuscarImagen.FileName);

Indicating the following: Severity Code Description Project File Line Error CS1061 'List' does not contain a definition for 'FileName' and no extension method 'FileName' accepting a first argument of type 'List' could be found ( Are you missing a using directive or an assembly reference?)

If you have opinions or answers about it, examples in EF.

Thank you!

    
asked by Dave 26.02.2018 в 19:27
source

2 answers

1

I have solved the error I wrote in the last comment.

I had to delete the model and reload it, so that I could take the changes I had made. So that's how the application worked for me, bringing me the image and the data associated with it. I leave the code of the "search" button for all those who need an idea similar to mine:

 private void btnBuscarEmpleado_Click(object sender, EventArgs e)
    {
        btnBuscarEmpleado.Image = imageList1.Images[0];

        if (string.IsNullOrEmpty(txtBuscarCedula.Text))
        {
            MessageBox.Show("Ingrese un número de cédula valido", "Buscar Empleado", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return;
        }

       using(empresaEntities1 _empleados = new empresaEntities1())
        {
            nomina _trabajador = new nomina();

            var BuscarEmpleado = _empleados.BuscarEmpleados(txtBuscarCedula.Text).ToList();

            if (BuscarEmpleado.Count() > 0)
            {
                OpenFileDialog BuscarImagen = new OpenFileDialog();

                MessageBox.Show("El empleado existe en la base de datos.", "¡ATENCIÓN!", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                txtcedula.Text = BuscarEmpleado.FirstOrDefault().cedula;
                txtnombres.Text = BuscarEmpleado.FirstOrDefault().nombres;
                txtapellidos.Text = BuscarEmpleado.FirstOrDefault().apellidos;
                txtcargo.Text = BuscarEmpleado.FirstOrDefault().cargo;
                txtdepartamento.Text = BuscarEmpleado.FirstOrDefault().departamento;
                txtsalario.Text = BuscarEmpleado.FirstOrDefault().salario.ToString("N2");
                txtUrl.Text = BuscarEmpleado.FirstOrDefault().UrlFoto;
                pictureBox1.Image = Image.FromFile(txtUrl.Text);
            }

            else
            {
                MessageBox.Show("El número de cédula no existe en la base de datos. ¿Desea ingresar uno nuevo?", "Confirmar", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            }

        }
    }

Take into account that this code is designed with Entity Framework.

    
answered by 27.02.2018 / 17:10
source
1

This is an example to obtain images stored in SQLServer:

    connection.Open();
    SqlCommand command1 = new SqlCommand("select imgfile from myimages where [email protected]", connection);
    SqlParameter myparam = command1.Parameters.Add("@param", SqlDbType.NVarChar, 30);
    myparam.Value = txtimgname.Text;
    byte[] img = (byte[])command1.ExecuteScalar();
    MemoryStream str = new MemoryStream();
    str.Write(img, 0, img.Length);
    Bitmap bit = new Bitmap(str);
    connection.Close();
    
answered by 27.02.2018 в 14:59