Image data from Sql server to asp.net

Discussion in 'ASP .Net Web Controls' started by seeni, Dec 1, 2003.

  1. seeni

    seeni Guest

    I have tried retrieving the image data from NorthWind database and print in
    the asp page using Repeater control. But instead of showing picture, the
    browser is showing "System.Byte[]"

    My code is
    In codebehind file.

    private void Page_Load(object sender, System.EventArgs e)
    {

    if(!Page.IsPostBack)

    {

    SqlConnection myConnection = new
    SqlConnection("server=test2;uid=sa;pwd=;database=NorthWind");

    // Connect to the SQL database using a SQL SELECT query to get

    // all the data from the "Titles" table.

    SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM" +

    " Categories", myConnection);

    // Create and fill a DataSet.

    DataSet ds = new DataSet();

    myCommand.Fill(ds);


    Repeater1.DataSource = ds;

    Repeater1.DataBind();

    }

    }

    In aspx file

    <asp:repeater id="Repeater1" runat="server">
    <HeaderTemplate>
    <font color="#cc3333" face="verdana" size="3"><b>DataList Control
    Header</b> </font>
    </HeaderTemplate>
    <AlternatingItemTemplate>
    <font face="verdana" size="2" color="green">
    <br>
    <b>Category ID: </b>
    <%# DataBinder.Eval(Container.DataItem, "CategoryID") %>
    <br>
    <b>Category Name: </b>
    <%# DataBinder.Eval(Container.DataItem, "CategoryName")%>
    <br>
    <b>Description: </b>
    <%# DataBinder.Eval(Container.DataItem, "Description") %>
    <br>
    <b>Picture: </b>
    <%# DataBinder.Eval(Container.DataItem, "Picture") %>
    <p>
    </div> </font>
    </AlternatingItemTemplate>
    <ItemTemplate>
    <font face="verdana" size="2">
    <br>
    <b>Category ID: </b>
    <%# DataBinder.Eval(Container.DataItem, "CategoryID") %>
    <br>
    <b>Category Name: </b>
    <%# DataBinder.Eval(Container.DataItem, "CategoryName")%>
    <br>
    <b>Description: </b>
    <%# DataBinder.Eval(Container.DataItem, "Description") %>
    <br>
    <b>Picture: </b>
    <%# DataBinder.Eval(Container.DataItem, "Picture") %>
    <p>
    </div> </font>
    </ItemTemplate>
    <FooterTemplate>
    <font color="#996600" face="verdana" size="1">DataList Control footer
    </font>
    </FooterTemplate>
    </asp:repeater>

    The output is

    Category ID: 3
    Category Name: Confections
    Description: Desserts, candies, and sweet breads
    Picture: System.Byte[]


    Category ID: 4
    Category Name: Dairy Products
    Description: Milk and stuff
    Picture: System.Byte[]


    Category ID: 5
    Category Name: Grains/Cereals
    Description: Breads, crackers, pasta, and cereal
    Picture: System.Byte[]


    Category ID: 6
    Category Name: Meat/Poultry
    Description: Prepared meats
    Picture: System.Byte[]


    Category ID: 7
    Category Name: Produce
    Description: Dried fruit and bean curd
    Picture: System.Byte[]


    Thanks,
    -Seeni
    seeni, Dec 1, 2003
    #1
    1. Advertising

  2. seeni

    Hans Kesting Guest

    "seeni" <> wrote in message
    news:%...
    > I have tried retrieving the image data from NorthWind database and print

    in
    > the asp page using Repeater control. But instead of showing picture, the
    > browser is showing "System.Byte[]"
    >


    You can't send image data along with the html code. Image data is retrieved
    by the
    browser in a separate request when it encounters an <img> tag.

    What you need to do is this:
    * generate an <img src=...> tag, with a URL that points to a special aspx
    file (with some parameters)
    * in that aspx, read the image from the database (based on the parameters)
    * set an appropriate ContentType ("image/gif", "image/jpeg") and use
    Response.BinaryWrite
    to write the data.

    Hans Kesting
    Hans Kesting, Dec 1, 2003
    #2
    1. Advertising

  3. "Hans Kesting" <> wrote in message
    news:OrIpNL$...
    >
    > "seeni" <> wrote in message
    > news:%...
    > > I have tried retrieving the image data from NorthWind database and print

    > in
    > > the asp page using Repeater control. But instead of showing picture, the
    > > browser is showing "System.Byte[]"
    > >

    >
    > You can't send image data along with the html code. Image data is

    retrieved
    > by the
    > browser in a separate request when it encounters an <img> tag.
    >
    > What you need to do is this:
    > * generate an <img src=...> tag, with a URL that points to a special aspx
    > file (with some parameters)
    > * in that aspx, read the image from the database (based on the parameters)
    > * set an appropriate ContentType ("image/gif", "image/jpeg") and use
    > Response.BinaryWrite
    > to write the data.


    Just as an FYI, according to the demos and previews I've seen, there will be
    a "DynamicImage" control in the next version of ASP.NET which can be bound
    to a binary column in a database. Not all that useful for now, but it's
    something to think about in the long run.
    Dave Rothgery, Dec 1, 2003
    #3
  4. seeni

    Bob Brunton Guest

    Well if microsoft put out a Dynamic Image control they are wasting
    their time because it already exists.

    I have used this control for 6 months now and I don't know anything
    about HTTP Handlers or sending stuff other than HTML down the HTTP
    response.

    Go to the Microsoft ASP.NET Control Gallery.

    http://www.asp.net/ControlGallery/ControlDetail.aspx?Control=773&tabindex=2

    Regards Bob


    "Dave Rothgery" <> wrote in message news:<#>...
    > "Hans Kesting" <> wrote in message
    > news:OrIpNL$...
    > >
    > > "seeni" <> wrote in message
    > > news:%...
    > > > I have tried retrieving the image data from NorthWind database and print

    > in
    > > > the asp page using Repeater control. But instead of showing picture, the
    > > > browser is showing "System.Byte[]"
    > > >

    > >
    > > You can't send image data along with the html code. Image data is

    > retrieved
    > > by the
    > > browser in a separate request when it encounters an <img> tag.
    > >
    > > What you need to do is this:
    > > * generate an <img src=...> tag, with a URL that points to a special aspx
    > > file (with some parameters)
    > > * in that aspx, read the image from the database (based on the parameters)
    > > * set an appropriate ContentType ("image/gif", "image/jpeg") and use
    > > Response.BinaryWrite
    > > to write the data.

    >
    > Just as an FYI, according to the demos and previews I've seen, there will be
    > a "DynamicImage" control in the next version of ASP.NET which can be bound
    > to a binary column in a database. Not all that useful for now, but it's
    > something to think about in the long run.
    Bob Brunton, Dec 24, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. seeni
    Replies:
    3
    Views:
    1,443
    Bob Brunton
    Dec 24, 2003
  2. =?Utf-8?B?SlA=?=
    Replies:
    1
    Views:
    7,294
    Steve C. Orr [MVP, MCSD]
    Aug 10, 2005
  3. Bhavesh
    Replies:
    0
    Views:
    423
    Bhavesh
    Jul 16, 2007
  4. Bhavesh
    Replies:
    5
    Views:
    631
    Bhavesh
    Jul 18, 2007
  5. seeni

    Image data from Sql server to asp.net

    seeni, Dec 1, 2003, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    212
    Bob Brunton
    Dec 24, 2003
Loading...

Share This Page