Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to display an image from database?
    primarykey
    data
    text
    <p>i know too that this (kind of) question has been asked and answered many times. I myself have asked this once before. But I havnt been successful yet. how do I display the image in an image control for a particular employee ID... Heres my code:</p> <p>` bind() GridView1.Visible = "True"</p> <pre><code> Dim strQuery As String = "SELECT Image FROM EmployeeTable WHERE EmployeeID =@EmployeeID" Dim cmd As SqlCommand = New SqlCommand(strQuery) cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value() = Convert.ToInt32(Request.QueryString("ImageID")) Dim dt As DataTable = GetData(cmd) If dt IsNot Nothing Then **Dim bytes() As Byte = CType(dt.Rows(1)("Image"), Byte())** Response.Buffer = True Response.Charset = "" Response.Cache.SetCacheability(HttpCacheability.NoCache) Response.ContentType = dt.Rows(0)("ContentType").ToString() Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows(1)("Name").ToString()) Response.BinaryWrite(bytes) Response.Flush() Response.End() End If` </code></pre> <p>Getting error at the line in bold. "There is no row at position 1"... Actually I want to fetch (display) the image for a particular employee id. How does it concern a row position? I dont know why i have written that line of code. Anyway, I know there are guys who can help me with this and so I thank them in advance...</p> <p>OK, Heres my code for adding the image to the database. This is working fine.</p> <pre><code> Dim imageData As Byte() = New Byte(FileUpload1.FileContent.Length) {} FileUpload1.FileContent.Read(imageData, 0, Convert.ToInt32(FileUpload1.FileContent.Length)) Dim con As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString").ToString) con.Open() If EmployeeIDTextBox.Text = "" Then MsgBox("Please Enter EmployeeID to Add Photo") Else Dim com As New SqlCommand("UPDATE EmployeeTable SET IMAGE = @IM where EmployeeID='" &amp; EmployeeIDTextBox.Text &amp; "'", con) Dim filePath As String = Server.MapPath(FileUpload1.FileName) 'Dim imageData As Byte() = File.ReadAllBytes(filePath) com.Parameters.AddWithValue("@IM", imageData) com.ExecuteNonQuery() MsgBox("File Saved Successfully!") End If End Sub </code></pre> <p>And now this is my code for retrieving &amp; displaying the same image in an image control...</p> <pre><code> bind() GridView1.Visible = "True" 'If Request.QueryString("ImageID") IsNot Nothing Then Dim strQuery As String = "SELECT Image FROM EmployeeTable WHERE EmployeeID =@EmployeeID" Dim cmd As SqlCommand = New SqlCommand(strQuery) cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value() = Convert.ToInt32(Request.QueryString("Image")) Dim dt As DataTable = GetData(cmd) If dt IsNot Nothing Then Dim bytes() As Byte = CType(dt.Rows(0)("Image"), Byte()) Response.Buffer = True Response.Charset = "" Response.Cache.SetCacheability(HttpCacheability.NoCache) Response.ContentType = dt.Rows(0)("ContentType").ToString() Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows(1)("Name").ToString()) Response.BinaryWrite(bytes) Response.Flush() Response.End() End IF </code></pre> <p>This is not working...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload