How to insert Image 3 Layers C #?

2

Good evening, I would like you to help me. I'm doing an image insert with SQL SERVER and C #. These are the fields of my table:

CREATE TABLE [dbo].[alumno](

    [Alu_IMAGEN] [VARCHAR](250) NULL
) ON [PRIMARY]

This is my stored procedure called Registrar_Alumno

ALTER PROCEDURE [dbo].[Registrar_Alumno]

@aluimagen VARCHAR(200)
@opc int
AS
BEGIN
IF @opc=1
begin
    INSERT INTO dbo.alumno
            (
            alu_Imagen

            )
    VALUES  (
            @aluimagen  

            )
            SELECT '1'
end
END



As I said I have done in 3 layers I am new to this Methodology and I would like you to help me as I can not insert AN IMAGE to my BD. I am using an open.file dialog but when I select an image in my field, the location of the image is not registered and I think that is the problem.

Les Agradesco.

This Data Layer:

 public class Cls_D_Alumno
    {
        private static string conexion = ConfigurationManager.ConnectionStrings["CnxLogin"].ConnectionString;



        public static DataTable RegistrarAlumno(Cls_E_Alumno objE)
        {
            return SqlHelper.ExecuteDataTable(conexion, "Registrar_Alumno", objE.Imagen,objE.Opc);
        }
    }

This is the Entity Layer:

    public class Cls_E_Alumno
    {

private string imagen;

        public string imagen
        {
            get { return imagen; }
            set { imagen= value; }
        }

        private int opc;

        public int Opc
        {
            get { return opc; }
            set { opc = value; }
        }


    }

This is the Presentation Layer:

private void btnRegistrar_Click(object sender, EventArgs e)
        {
            try
            {


                    objE.Imagen=txtImagen.Text;
                    objE.Opc = 1;

                    string resp = Cls_N_Alumno.RegistrarAlumno(objE).Rows[0][0].ToString();
                    if(resp=="1")
                    {
                        Limpiar();
                        MessageBox.Show("Registro Correctamente", "Aviso del Sistema");
                    }
                    else
                    {
                        MessageBox.Show("Error al Registrar", "Aviso del Sistema");
                    }




            }
            catch(Exception ex)
            {

            }
        }

This is the Image of my Presentation:

In that Button of ... IT IS WHERE they are going to select the image for it I am using this method of the open File.

private void btnImagen_Click(object sender, EventArgs e)
        {
            try
            {
                this.openFileDialog1.ShowDialog();
                if(this.openFileDialog1.FileName.Equals("")==false)
                {
                    pictureBox1.Load(this.openFileDialog1.FileName);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("No se Pudo Cargar la Imagen", "Aviso del Sistema");
            }
        }

    
asked by PieroDev 18.03.2017 в 05:09
source

1 answer

3

Well I can mark some points that I think could solve the problem, the first thing I notice is that nowhere in the code are you assigning the path of the selected image to txtImagen , when you select the image with the openFileDialog1 you assign this to pictureBox1 , but then I see that you use to define the entity:

objE.Imagen=txtImagen.Text;

but we have not assigned the path of the image to the textbox (or at least you did not show that part of the code), that's why nothing comes to the other layers.

It is also not recommended if you use entities that the RegisterAlumn () method returns a datatable, you use the SqlHelper, but you should supply the response it generates and return an entity or a value if.

Something that is very useful series that you put a breakpoint in the code and go analyzing step by step what values have the variables in this way to know if you are assigning them.

In the event method btnRegistrar_Click , remove the catch vacion, NEVER define a try..catch but you will do something with it, like being at least log the problem in a file, because despeus there are problems and never you find out what's going on If you're not going to do anything with the error then do not define the try..catch

Analyze this article

[WinForms] Editing Employees

there I explain how to work in layers using entities

    
answered by 20.03.2017 / 11:51
source