locked
display image from sql database RRS feed

  • Question

  • I have read several posts online and can't seem to figure out how to retrieve a picture that is stored in a sql table as an image.  I am using visual studio 2005 and have read about this picurebox control but don't know where it is?  All I am seeing is an image control.  When I try and use it I get a cast error.
    Mike
    • Moved by Karel ZikmundMicrosoft employee Wednesday, February 17, 2010 5:57 PM PictureBox is from WinForms (From:Building Development and Diagnostic Tools for .Net)
    Monday, February 15, 2010 8:22 PM

Answers

All replies

    • Marked as answer by SamAgain Tuesday, February 16, 2010 2:25 AM
    • Unmarked as answer by mr4100 Friday, February 26, 2010 4:53 PM
    Monday, February 15, 2010 9:02 PM
  • Hi, mr4100:
         I hope these refs could be helpful.
        1. Storing and Retrieving Images from SQL Server using Microsoft .NET
            http://www.codeproject.com/KB/database/ImageSaveInDataBase.aspx
        2. Getting and Displaying Images from SQL Server with C#
            http://www.informit.com/articles/article.aspx?p=377078

    Please mark the right answer at right time.
    Thanks,
    Sam
    Tuesday, February 16, 2010 2:27 AM
  • I guess I should have been more specific.  I am using vb.net and these examples are using c#. 


    Mike
    Wednesday, February 17, 2010 2:41 PM
  • The 2 languages are very similar (especially in small code snippets like these), read it carefuly and you will see all important parts. The language itself is not important.
    If you still struggle, check out online C# <-> VB.NET code translators (e.g. http://www.carlosag.net/Tools/CodeTranslator/)

    -Karel

    Wednesday, February 17, 2010 5:46 PM
  • Here is the code I am using to store an image but when I try and retrieve the image it doesn't show up, just a image with an x in it.  Am I storing the image correctly?


    Dim tmpExt As String = System.IO.Path.GetExtension(Me.FileUpload1.PostedFile.FileName)
    Dim Length As Integer = CInt(FileUpload1.PostedFile.InputStream.Length)
    Dim ContentType As String = FileUpload1.PostedFile.ContentType
    Dim Content As Byte()
    ReDim Content(Length)

    Dim File = FileUpload1.PostedFile.InputStream.Read(Content, 0, Length)
                   
    If FileUpload1.HasFile And DrpDwnsecretquestion.SelectedValue <> "" And Txtanswer.Text <> "" Then
               
     If tmpExt.ToLower = ".jpg" Or tmpExt.ToLower = ".gif" Then
      Try
        FileUpload1.SaveAs("C:\Upload_Test\" & FileUpload1.FileName)
        Label3.Text = "The image you uploaded was (" & FileUpload1.FileName & ")
        myConnection = New SqlConnection("server=MYSERVER;uid=user;pwd=*****;database=MYDATABASE")
        myConnection.Open()
        myCommand = New SqlCommand("UPDATE MYDATABASE SET Date_Updated = getdate(),Ip_Address ='" & h.AddressList.GetValue(0).ToString & "',Question ='" & DrpDwnsecretquestion.Text & "',Answer ='" & Txtanswer.Text & "',Image_Name ='" & FileUpload1.FileName & "',Image ='" & File & "'WHERE User_Name='" & Txtname_num.Text & "'", myConnection)
        myCommand.ExecuteNonQuery()
        myConnection.Close()
      Catch ex As Exception
        Label1.Text = "ERROR: " & ex.Message.ToString()
      End Try
     Else
        Label3.Text = "You can only upload .jpg or .gif files!"
     End If

    Else
      Label3.Text = "blah blah blah"
          
    End If


    Mike
    Friday, February 19, 2010 7:48 PM
  • I also tried using Response.BinaryWrite() and I just see the size and not the actual image.  I'm totally confused here.
    Mike
    Friday, February 26, 2010 4:55 PM
  • ok, I have changed my code to show the image in a datagrid.  Can someone tell me why the image is not showing? 

     

    function:

    Dim myCommand As SqlCommand = New SqlCommand("SELECT [Image] FROM Form WHERE User_Name='" & Txtusername.Text & "'", myConnection)

     

    Dim ada As New SqlDataAdapter(myCommand)

     

    Dim dset As New DataSet

    ada.Fill(dset)

    DG_Image.DataSource = dset

    DG_Image.DataBind()

     

    datagrid:

    <

     

    asp:DataGrid

     

    ID="DG_Image"

     

    AutoGenerateColumns="False"

     

    ItemStyle-BackColor="Beige"

     

    ItemStyle-Font-Name="verdana"

     

    ItemStyle-Font-Size="13px"

     

    BorderColor="Black"

     

    Runat="server" CssClass="style4" Visible="False" Width = "85px"

     

    Height ="85px" ShowHeader="False">

    <

     

    ItemStyle BackColor="white" Font-Size="13px"></ItemStyle>

     

    <Columns>

     

    <asp:TemplateColumn>

     

    <ItemTemplate>

     

    <asp:Image ID="Image1" Width="85px" Height="85px"

     

    ImageUrl='<%# Eval("[Image]") %>'

     

    Runat="server" AlternateText="Your Uploaded Image"/>

     

    </ItemTemplate>

     

    </asp:TemplateColumn>

     

    </Columns>

     

    </asp:DataGrid>


    Mike
    Friday, March 19, 2010 4:20 PM
  • I guess no one can help?  I still can't figure this out.
    Mike
    Monday, April 12, 2010 6:04 PM
  • If you want to display an image from a database onto a web form, you will need a handler to do that.

    This article comes up with a Google search.  It has both C# and VB code.

    http://www.dotnetcurry.com/ShowArticle.aspx?ID=129

     


    jon.stromer.galley
    • Marked as answer by mr4100 Wednesday, April 14, 2010 3:24 PM
    Monday, April 12, 2010 8:09 PM
  • thanks this helped out.
    Mike
    Wednesday, April 14, 2010 3:23 PM