locked
Images and Databases RRS feed

  • Question

  • User1546138644 posted

    This is driving me mad! Using Webmatrix I am uploading images and storing them in a database and then trying to retrieve them from the database and display them.

    First let me say I have been studying mikesdotnetting excellent article here: http://www.mikesdotnetting.com/Article/148/Save-And-Retrieve-Files-From-a-Sql-Server-CE-Database-with-WebMatrix

    Now. I use the exact technique described in this article and my retrieval code, getBandImage.cshtml is:

    @{
        int id = 0;
        if(Request["Id"].IsInt()){
            id = Request["Id"].AsInt();
            var db = Database.OpenFile((string)App.db);
            var sql = "Select * From LSBands Where id = @0";
            var file = db.QuerySingle(sql, Id);
            
            Response.AddHeader("content-disposition", "inline;");
            Response.ContentType = "image/jpg";
            Response.BinaryWrite((byte[])file.imageMain);
        }
    }

     I have tested this code by putting it inside the non-post section of the page where I want to display the image:

            // Display the image
            var db2 = Database.Open((string)App.db);
            var sql = "Select * From LSBands Where id = @0";
            var file = db2.QuerySingle(sql, strBandId);
            //
            Response.AddHeader("content-disposition", "inline; ");
            Response.ContentType = "image/jpg";
            Response.BinaryWrite((byte[])file.imageMain);
    

    This does work but opens the image in a new browser window- but it proves the db connection works plus the variables are correct and the field it retrieves from the database can be displayed as an image properly... 

    However when I use this it fails completely...

        <img src="getBandImage.cshtml?Id=@strBandId" alt="Band Image" />
    

    All I get is the image box with the little cross and the "Band Image" text... I have checked the correct id is being passed to getBandImage.cshtml...

    Any ideas anyone?

     

     

    Wednesday, July 10, 2013 5:08 PM

Answers

  • User-821857111 posted

    Http Status code 500 means Server Error.  Click the getBandImage entry once to select it then click Go to detailed view. Then click the Response body tab. That contains the actual response that was sent by the server, which should be an error message.  Read it to see if it makes any sense to you. If it doesn't, paste it here.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 11, 2013 3:43 PM

All replies

  • User359558855 posted

    Shouldn't it be src="~/getBandImage.cshtml?Id=@strBandId" ?

    Wednesday, July 10, 2013 6:45 PM
  • User1546138644 posted

    No, the target file is in the same folder as the calling file. But just in case, I tried it with an explicit path to the target .cshtml file and it had no effect...

    Wednesday, July 10, 2013 6:57 PM
  • User-821857111 posted

    Hit F12 in IE or Chrome to bring up the developer tools and check the details of the request in the Network tab.

    Thursday, July 11, 2013 2:28 AM
  • User1546138644 posted

    I'm not at the machine at the moment, but I did do this as part of my attempted diagnosis last night and it all looked fine- specifically the id being passed in was one that related to a record containing a valid image in the imageMain column- What else should I be on the lookout for?

     

    [Edit] Oops, just noticed you mentioned the Network tab- I did not check that last night, only the HTML tab. I'll take a look tonight.

    Thursday, July 11, 2013 8:29 AM
  • User-821857111 posted

    just noticed you mentioned the Network tab- I did not check that last night, only the HTML tab. I'll take a look tonight.

    When you do, check the Response tab.

    Thursday, July 11, 2013 10:04 AM
  • User1546138644 posted

    Network summary view gave me:

     

    URL Method Result Type Received Taken Initiator Wait‎‎ Start‎‎ Request‎‎ Response‎‎ Cache read‎‎ Gap

    http://localhost:35717/Bands/updateBand?id=3 GET 200 text/html 3.42 KB 47 ms click 0 0 31 16 0 433

    /Css/Style.css GET 304 text/css 273 B < 1 ms <link rel="stylesheet"> 47 0 0 0 0 433

    /Bands/getBandImage.cshtml?Id=3 GET 500 text/html 15.65 KB 359 ms <img> 47 0 359 0 0 74

    I don't really know what I am looking for here... What is a response 500?

     

    Thursday, July 11, 2013 1:41 PM
  • User-821857111 posted

    Http Status code 500 means Server Error.  Click the getBandImage entry once to select it then click Go to detailed view. Then click the Response body tab. That contains the actual response that was sent by the server, which should be an error message.  Read it to see if it makes any sense to you. If it doesn't, paste it here.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 11, 2013 3:43 PM
  • User1546138644 posted

    Oh wow! I did not know that level of debugging information was possible... There were multiple errors in my source (one of which I actually fixed when I was running it as inline code but had not gone back and fixed in the standalone .cshtml

     

    It's all working now, thanks to you Mike! Frankly I don't know what we'd do without you round here Cool

     

     

    Thursday, July 11, 2013 3:55 PM