0

This is my database in SQL Server, I saved the image in an image column:

CREATE TABLE registro
(
    id int IDENTITY(1, 1),
    Nombre varchar(100),
    appaterno varchar(100),
    apmaterno varchar(100),
    lineainvestigacion varchar(100),
    correo varchar(100),
    correoi varchar(100),
    telefonop int,
    extension int,
    telefonoof int,
    usuario varchar(100) PRIMARY KEY ,
    clave varchar(100),
    imagen image,
    idrol varchar(100),
    ciudadr varchar(100)
);

Where looking at the data through a select, it shows that I have my image saved:

image saved using image attribute

This is my class:

public class Registro1
{
    public int id { get; set; }
    public string nombre { get; set; }
    public string appaterno { get; set; }
    public string apmaterno { get; set; }
    public string correo { get; set; }
    public string usuario { get; set; }
    public string clave { get; set; }

    public string Imagen { get; set; }

    public string image
    {
        get { return Imagen.Replace(" ", string.Empty) + ".jpg"; }
    }

    public string idrol { get; set; }
    
    public string lineainvestigacion { get; set; }
    public string correoi { get; set; }
    public int telefonoof { get; set; }
    public double telefonop { get; set; }
    public int extension { get; set; }
    public string ciudadr { get; set; }
}

And this is my controller:

public ActionResult VistaPerfil(Registro1 rg)
{
    connectionString();

    con.Open();
    com.Connection = con;
    com.CommandText =
        "select Nombre,lineainvestigacion,correo,correoi,telefonop,extension,telefonoof,ciudadr,clave,imagen 
         from registro where usuario='" + rg.usuario + "'";
      
    SqlDataReader dr;

    try
    {
        dr = com.ExecuteReader();
  
        while (dr.Read() == true)
        {                   
            rg.nombre = dr["Nombre"].ToString();
            rg.lineainvestigacion = dr["lineainvestigacion"].ToString();
            rg.correo = dr["correo"].ToString();
            rg.correoi = dr["correoi"].ToString();
            rg.telefonop = Convert.ToInt32(dr["telefonop"]);
            rg.extension = Convert.ToInt32(dr["extension"]);
            rg.telefonoof = Convert.ToInt32(dr["telefonoof"]);
            rg.ciudadr = dr["ciudadr"].ToString();
            rg.clave = dr["clave"].ToString();
            rg.Imagen= dr["imagen"].ToString();

            ViewData["Nombre"] = rg.nombre;
            ViewData["lineainvestigacion"] = rg.lineainvestigacion;
            ViewData["correo"] = rg.correo;//correo personal
            ViewData["correoi"] = rg.correoi;//correo institucional
            ViewData["telefonop"] = rg.telefonop;
            ViewData["extension"] = rg.extension;
            ViewData["telefonoof"] = rg.telefonoof;
            ViewData["ciudadr"] = rg.ciudadr;
            ViewData["clave"] = rg.clave;
            ViewData["imagen"] =rg.Imagen;
                 
            con.Close();

            return View("../Home/Perfil");
        }
    }
    catch (Exception es)
    {    
        con.Close();
        Response.Write("<script>alert('Perfil no visto =(')</script>");
        return View("../Home/Perfil");

        throw es;
    }
}

Through this function I try to load the image so that it is shown in my view:

<form action="VistaPerfil" method="post">
    <div class="row">
        <div class="col-md-6">
            <label>Imagen:</label>
        </div>
        <div class="col-md-6">
            <p>
                @ViewData["imagen"]
            </p>
        </div>
    </div>
    <button type="submit" class="btn btn-primary btn-block"> Ver datos</button>
</form>

The error I get is that it does not show me the image as shown in the screenshot:

System.Byte[]

I would like to know how to convert from string to some attribute to show the image or some other solution.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Does this answer your question? [ASP.Net MVC: How to display a byte array image from model](https://stackoverflow.com/questions/17952514/asp-net-mvc-how-to-display-a-byte-array-image-from-model) – Yong Shun Apr 30 '21 at 02:43
  • 1
    Would like to give some kindly suggestions: 1. Use [Parameterized Query](https://www.c-sharpcorner.com/UploadFile/a20beb/why-should-always-use-the-parameterized-query-to-avoid-sql-i/), but not `string.concat` value in query for preventing [SQL Injection](https://www.w3schools.com/sql/sql_injection.asp). – Yong Shun Apr 30 '21 at 03:16
  • 2. Apply [Using block/declaration](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement) for `SqlConnection`, `SqlCommand` and `SqlDataReader` to automatically dispose resources when the process ends. – Yong Shun Apr 30 '21 at 03:18
  • 2
    `image` data type will be removed in a future version of SQL Server. Avoid using this data type in new development work, and plan to modify applications that currently use it. Use `varbinary(max)` instead. [See details here](http://msdn.microsoft.com/en-us/library/ms187993.aspx) – marc_s Apr 30 '21 at 03:53

0 Answers0