SitePoint Sponsor

User Tag List

Results 1 to 22 of 22
  1. #1
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to display image from sql server

    I need to show an image from sql server of type image .but it does not showing me .although there is no error /exception while getting the data from dataset but still.

    secondly please reply me by keeping in mind that i used dataset not reader to display the image.

    If some one need further explanation regarding problem or need code please specify in reply.

  2. #2
    Non-Member
    Join Date
    Jun 2007
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    Give more info about the exception.

    Regards

  3. #3
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    there is no exception in the code but image is not showing in the grid

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2008
    Location
    Dublin, Ireland
    Posts
    461
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  5. #5
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in my this post i m going to show my certain patches of code
    Code:
    <asp:TemplateColumn HeaderText="Picture" HeaderStyle-Font-Bold="True">
    									<ItemTemplate>
    										<asp:Image Width="150" Height="75" ImageUrl='<%# FormatURL(DataBinder.Eval(Container.DataItem, "s_NO")) %>' Runat=server ID="Image1" />
    									</ItemTemplate>
    								</asp:TemplateColumn>
    Code:
    
        Function FormatURL(ByVal strArgument) As String
            'Response.Write("\n" & strArgument)
            'Return ("readrealimage.aspx?id=" & strArgument)
            Return "ReadRealImage.aspx?id=" & strArgument
        End Function
    and following is teh page load event of readrealimages

    Code:
       Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            Dim strImageID As Int32 = Convert.ToInt32(Request.QueryString("id").ToString())
            Dim ds As New DataSet
            Dim cn As New Connection
            ds = cn.GetimageByS_NO(strImageID)
            Try
                Dim bytArrayContent As Byte()
                bytArrayContent = CType(ds.Tables(0).Rows(0)("picture"), Byte())
                'Dim bytArrayContent As Byte() = DirectCast(drc.ExecuteScalar, Byte())
    
                Response.ContentType = "image/jpeg"
                'Response.BinaryWrite(CType(ds.Tables(0).Rows(0)("picture"), Byte()))
                Response.OutputStream.Write(bytArrayContent, 0, bytArrayContent.Length)
            Catch SQLexc As SqlException
            End Try
        End Sub
    and GetimageByS_NO is the function in the user defined class which get image from database .

    if need further gist regarding code i m available!!

  6. #6
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    should i need to physically store the image on certain folder

  7. #7
    SitePoint Evangelist
    Join Date
    Apr 2008
    Location
    Dublin, Ireland
    Posts
    461
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure if you are saying that your code is working or not. I personally prefer to store images in a folder and the name of the image in the database.

  8. #8
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BrianOConnell View Post
    I'm not sure if you are saying that your code is working or not. I personally prefer to store images in a folder and the name of the image in the database.
    I second this. I recently had to deal with a database that had 16gigs woth of images stored, and it was not pleasant!

  9. #9
    I'm a college yuppie now! sbdi's Avatar
    Join Date
    Jul 2000
    Location
    Dublin , Ireland
    Posts
    1,271
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Keeping images in the database is a bad design approach. It creates more load then required on the database. Serving from the filesystem is superior.

    You are only using a SQLException in your catch block. This wont catch all exceptions. When you stick debug statements into the start of the try block what happens? I suspect that something is going amiss when you are trying to send the image down.

    What is the datatype in the database?
    Back Again

  10. #10
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    storing the image in database is requirement.the field name is picture and type is image

  11. #11
    SitePoint Evangelist
    Join Date
    Apr 2008
    Location
    Dublin, Ireland
    Posts
    461
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm still not clear on what error you are getting.

  12. #12
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i m not getting any error while retirveing the image .just the problem is image is not showing in the grid

  13. #13
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://www.beansoftware.com/asp.net-...-database.aspx

    my problem is closely related to above . the only difference is that i m using dataset and grid . so any one can help me in this regard by keeping these two things in mind

  14. #14
    SitePoint Evangelist
    Join Date
    Apr 2008
    Location
    Dublin, Ireland
    Posts
    461
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A couple of things to try. First of all if you are not going to put code in a catch then temporarily remove it until you know what is wrong. Also add a response.end just to make sure the page stops processing once the image has been outputted.
    Code:
    'Try
                Dim bytArrayContent As Byte()
                bytArrayContent = CType(ds.Tables(0).Rows(0)("picture"), Byte())
                'Dim bytArrayContent As Byte() = DirectCast(drc.ExecuteScalar, Byte())
    
                Response.ContentType = "image/jpeg"
                'Response.BinaryWrite(CType(ds.Tables(0).Rows(0)("picture"), Byte()))
                Response.OutputStream.Write(bytArrayContent, 0, bytArrayContent.Length)
                Response.End
            'Catch SQLexc As SqlException
            'End Try

  15. #15
    SitePoint Evangelist
    Join Date
    Apr 2008
    Location
    Dublin, Ireland
    Posts
    461
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also to see the error if there is one you'll need to open readrealimage.aspx?id=anid. Put in a valid id in the querystring and open that link. You will see a proper error if there is one while in the grid you'll just see an x.

  16. #16
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have change my db structure and convert its type from image to varchar.

    but now i have bit different kind of problem .i used following code to display aimage

    Code:
    <asp:Template Column Header Text="Picture" Header Style-Font-Bold="True">
    									<Item Template>
    										<asp:Image ID=MIG  Imagery ='<%#  "Uploaded Images/" + DataBinder.Eval(Container.DataItem,"Picture") %>'></asp:Image>
    									</Item Template>
    								</asp:Template Column>
    but its not showing the image . although dataset is getting all the records .the image are stored here

    http:\\localhost\DATA ENTRY FORM DEVELOPMENT\Uploaded Images

    where DATA ENTRY FORM DEVELOPMENT is the project name.

  17. #17
    SitePoint Evangelist
    Join Date
    Apr 2008
    Location
    Dublin, Ireland
    Posts
    461
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:-

    <asp:Image ID="MIG" ImageURL ='Uploaded Images/<%# DataBinder.Eval(Container.DataItem,"Picture") %>'></asp:Image>

  18. #18
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no still does not showing any image.

  19. #19
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    Code:
    <asp:Image ID="MIG" ImageURL ='<&#37;# DataBinder.Eval(Container.DataItem,"Picture", "~/Uploaded%20Images/{0}") %>' />
    I would avoid having spaces in web folder names in the future.

  20. #20
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no result so far .even the image control on the screen

  21. #21
    SitePoint Evangelist
    Join Date
    Apr 2008
    Location
    Dublin, Ireland
    Posts
    461
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wwb_99 View Post
    Try this:

    Code:
    <asp:Image ID="MIG" ImageURL ='<%# DataBinder.Eval(Container.DataItem,"Picture", "~/Uploaded%20Images/{0}") %>' />
    I would avoid having spaces in web folder names in the future.
    I'm not sure if databinder.eval takes 3 arguements like this. I know eval in version 2.0 takes the field name followed by a string to be formatted as the dataitem is implied but I don't think asp.net 1.1 (which this appears to be) takes any more than the dataitem and the field name as parameters.

    I would stick with

    <asp:Image ID="MIG" ImageURL ='Uploaded%20Images/<%# DataBinder.Eval(Container.DataItem,"Picture") %>'></asp:Image>

    but fixing the space issue as wwb_99 suggested. That is unless I am mistaken on the parameters. Also when you open the page in the browser look at the html source to see what is being rendered.

  22. #22
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Yes, DataBinder.Eval takes three arguments and you can use all the string formatting functions on the third argument.

    The Eval shortcut might not, but I can't claim to have used it too often.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •