1
Reply

retireve multiple images from database and show in view

Jagdish Suryawanshi

Jagdish Suryawanshi

Jul 15 2017 6:17 AM
192
i am trying to show an image in view. i am using mvc 5 now, below  is my view code and server side code. so please help me how i show a single image or multiple images from database,
view code,
 
@model NewAuthentication.ViewModel.PhotoViewModel
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>myprof</title>
</head>
<body>
<div>
<table>
<tbody>
<tr>
<td>
<img src="@Model.photo" />
</td>   </tr>
</tbody>
</table>
</div></body>
</html>
 
and server side code, 
 
i create a viewmodel which is shown below,
 
public class PhotoViewModel
{
public int id { get; set; }
public string photo { get; set; }
public string firstname { get; set; }
public string Gender { get; set; }
 
and action method  code is below,
 
public ActionResult myprof()
{
SqlConnection con = new SqlConnection("Data Source=JAGDISH;Database=Hmarasmaj;Integrated Security=SSPI");
SqlDataAdapter da = new SqlDataAdapter("Select photo from ParentImages where pid="+1+"",con);
DataTable dt = new DataTable();
da.Fill(dt);
int c = dt.Rows.Count;
PhotoViewModel p = new PhotoViewModel();
byte[] img =(byte[]) dt.Rows[0]["photo"];
p.photo = "data:image/png;base64," + Convert.ToBase64String(img);
return View(p);
}

Answers (1)