1
Reply

image not show in view after retrieve from database

Jagdish Suryawanshi

Jagdish Suryawanshi

Jul 15 2017 7:58 AM
192
i retrieve image from database but after vconverting base64 not show in view below is the server side code
  1.   public ActionResult myprof()
    {
    SqlConnection con = new SqlConnection("Data Source=JAGDISH;Database=Hmarasmaj;Integrated Security=SSPI");
    SqlDataAdapter da = new SqlDataAdapter("Select top(1) photo from ParentImages where pid="+1+"",con);
    DataTable dt = new DataTable();
    da.Fill(dt);
    int c = dt.Rows.Count;
    DataModel dm = new DataModel();
    byte[] img =(byte[]) dt.Rows[0]["photo"];
    dm.Photo = img;
    return View(dm);

    }
and below is the view code,
  1.   @model NewAuthentication.Models.DataModel
    @{
    Layout = null;
    }
    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width" />
    <title>myprof</title>
    </head>
    <body>
    <div>
    @{
    var base64 = Convert.ToBase64String(Model.Photo);
    var imgsrc = string.Format("data:image/jpeg;base64,{0}", base64);
    }
    <img src="@imgsrc" width="100" />
    </div>
    </body>
    </html>
 
public ActionResult myprof()
{
SqlConnection con = new SqlConnection("Data Source=JAGDISH;Database=Hmarasmaj;Integrated Security=SSPI");
SqlDataAdapter da = new SqlDataAdapter("Select top(1) photo from ParentImages where pid="+1+"",con);
DataTable dt = new DataTable();
da.Fill(dt);
int c = dt.Rows.Count;
DataModel dm = new DataModel();
byte[] img =(byte[]) dt.Rows[0]["photo"];
dm.Photo = img;
return View(dm);

}

Answers (1)