1
Answer

retrieving image from database

harikesh sharma

harikesh sharma

16y
2.6k
1

hi all,

i have written the following code for retrieving image from database(using vb.net2003 and sql server 2000) and displaying it in datagrid,but image is not displaying.if anyone has the solution ,pls tell me.

//////readrealimage.aspx
<script runat="server">
     Public Sub Page_Load(sender As Object, e As EventArgs)
            Dim strImageID as String = Request.QueryString("lid")
            Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("siteconnection"))
            Dim myCommand As New SqlCommand("Select photo from blogReg_ems Where lid=" & strImageID, myConnection)
     Try
        myConnection.Open()
        Dim myDataReader as SqlDataReader
        myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
           Do While (myDataReader.Read())
          'Response.ContentType =myDataReader.Item("PersonImageType")
           Response.BinaryWrite(myDataReader.Item("photo"))
      Loop                                               
       myConnection.Close()
        Catch SQLexc As SqlException
       End Try
    End Sub   
      </script>
   
////test.aspx
<script runat="server">
      Sub Page_Load(sender As Object, e As EventArgs)
          If Not Page.IsPostBack Then
                   BindGrid()
                 End If   
       End Sub
             Private Sub BindGrid()
      Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("siteconnection"))
      Dim myCommand As SqlCommand = New SqlCommand("blogimgprc", myConnection)
      myCommand.CommandType = CommandType.StoredProcedure
   Try
     myConnection.Open()
     DG_Persons.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
     DG_Persons.DataBind()
 Catch SQLexc As SqlException
        Response.Write("Error occured while Generating Data. Error is " & SQLexc.ToString())
   End Try
  End Sub
Function FormatURL(strArgument) as String
    Return ("readrealimage.aspx?lid=" & strArgument)
   End Function
                       
</script>
 </HEAD>
<body style="FONT: 10pt verdana">
 <h3 align="center">Retrieving images from SqlServer and Displaying in the DataGrid.</h3>
 <form id="Form1" method="post" runat="server">
 <asp:datagrid id="DG_Persons" AutoGenerateColumns="False" Runat="server" BorderColor="#000000"
ItemStyle-Font-Size="13px" ItemStyle-Font-Name="verdana" ItemStyle-BackColor="Beige" HeaderStyle-ForeColor="#ffffff"
 HeaderStyle-Font-Size="13px" HeaderStyle-Font-Name="Verdana" HeaderStyle-Font-Bold="True"
 HeaderStyle-BackColor="#ff0000" Width="99%">
<Columns>
 <asp:TemplateColumn HeaderText="Image">
 <ItemTemplate>
<asp:Image Width="200" Height="200" ImageUrl='<%# FormatURL(DataBinder.Eval(Container.DataItem, "lid")) %>' Runat=server ID="Image1"/>
 </ItemTemplate>
 </asp:TemplateColumn>
</Columns>
</asp:datagrid></form>
 </body>
</HTML>

Answers (1)