I would like you to help me. I'm doing an image push 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 calledRegistrar_Alumno
CREATE 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 was telling you, I have made it in 3 layers, I am new to this methodology and I would like you to help me since I cannot insert an image into my DB.
I am using an open.file dialog but when I select an image in my field the location of the image is not recorded and I think that is the problem.
I thank you
This is the 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 ...
is where they are going to select the image, for this 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");
}
}
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 youopenFileDialog1
assign it topictureBox1
, but then I see what you use to define the entity:but I need to assign the path of the image to the textbox (or at least you didn't show that part of the code), that's why nothing reaches the other layers.
It is also not recommended if you use entities that the RegistrarAlumno() method returns a datatable, I know you use the SqlHelper, but you should store the response it generates and return an entity or a value yes.
Something that is very useful is that you put a breakpoint in the code and analyze step by step what values the variables have in this way to know if you are assigning them.
In the event method
btnRegistrar_Click
, remove the empty catch, NEVER define a try..catch but you are going to do something with it, like at least log the problem in a file, because later there are problems and you never find out what is happening. If you are not going to do anything with the error then don't define the try..catchAnalyze this article
[WinForms] Employee Edition
there I explain how to work in layers using entities
Good morning, Your code is subject to many improvements, but focusing on the question as you ask it, the code for the "btnImagen" button has some errors, such as from there you should fill the txtImagen.Text property, because I see that from there you take the image path. The fix for that code is as follows:
For example, the ShowDialog method returns the user's response. If the user presses OK, it is understood that you are going to perform the operation, but if they cancel either with the ESC key or by pressing Cancel, the operation should not be performed. The following thing I think solves your doubt at once, is that the Text property of the txtImagen control must be filled, since that value is the one you are taking in the Registry button. I hope this code will help you to clarify a little your doubts.