Recover image in SQL SERVER and put in a REPEATER list

Asked

Viewed 600 times

0

Today I recover information from a database and I list in a REPEATER, until then all blz. But I also need to take an image from the database and show on this REPEATER.

Follow below as I show the table.

inserir a descrição da imagem aqui

So I want to put a column with the image.

Below code of class retrieving data from database:

    public SqlDataReader ListarClientes()
    {
        String query_string = "select codcliente,razaosocial,cidade,imagem from cliente";
        SqlConnection conexao = new SqlConnection();
        conexao.ConnectionString = this.string_conexao;
        conexao.Open();

        SqlCommand comando = new SqlCommand();
        comando.CommandText = query_string;
        comando.Connection = conexao;

        SqlDataReader dr = comando.ExecuteReader();

        return dr;
    }

Aspnet code

 <h2>Lista de clientes</h2>

     <form id="form1" runat="server">



    <div class ="container"
        <div class =" row col-md-6 col-md-offset-2 custyle">



        </div>
    </div>
    <br />
    <br />




    <div class="container">
    <div class="row col-md-6 col-md-offset-2 custyle">


    <asp:Repeater ID="rptCliente" runat="server">

 <HeaderTemplate>

    <table class="table table-striped custab">
      <a href="InsereCliente.aspx" class="btn btn-primary btn-xs pull-right"><b>+</b> Novo</a>
    <tr >
    <td>
        <font ><b>Código</b></font>
    </td>

     <td>
        <font ><b>Nome</b></font>
    </td>

    <td>
        <font ><b>cidade</b></font>
    </td>



    <td align="center">
            <font ><b>Ação</b></font></td>
   </tr>
</HeaderTemplate>



    <ItemTemplate>


     <tr >
           <td>
                 <%# DataBinder.Eval(Container.DataItem, "codcliente") %>   
           </td>
           <td>
                 <%# DataBinder.Eval(Container.DataItem, "razaosocial") %>   
           </td>
           <td>
           <%# DataBinder.Eval(Container.DataItem, "cidade") %>
       </td>


       <td class="text-center">
           <a class='btn btn-info btn-xs' href="#"><span class="glyphicon glyphicon-edit"></span> Editar</a> 
           <a href="#" class="btn btn-danger btn-xs"><span class="glyphicon glyphicon-remove"></span> Excluir</a></td>

       </td>
   </tr>
</ItemTemplate>

    </asp:Repeater>
        </div>
        </div>

    </form>

    </body>
</body>

Code of the aspnet C screen#

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SITE_SERVICO.cliente
{
    public partial class ListaCliente : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            BancoDados.BancoDados bd = new BancoDados.BancoDados();

            SqlDataReader dtCliente = bd.ListarClientes();



            rptCliente.DataSource = dtCliente;
            rptCliente.DataBind();




        }
    }
}

Can someone help me?

2 answers

0


You guys, I figured it out.

I used bytes[] to solve.

the result of the database search was placed in a Sqldatareader drCartaoProfissional and then I did it as follows:

byte[] bytes = (byte[])drCartaoProfissional["image"]; string base64String = Convert.Tobase64string(bytes, 0, bytes.Length); string url = "data:image/png;Base64," + base64String;

Then in an html IMG, I set the src property with the URL.

0

If you have saved in your bank the image path, just add the component Image in your Repeater:

<td>
    <asp:Image ID="Image1" height="32" width="32" runat="server" ImageUrl='<%# Eval("ImageUrl") %>' />
</td>

If you only have the image name ex: "usuario_26.jpg" there you will have to put the path inside the Repeater or in a Itemdatabound.

Thus:

<td>
    <asp:Image ID="Image1" height="32" width="32" runat="server" ImageUrl="../imagens/user/'<%# Eval("ImageUrl") %>'" />
</td>

Show images in Repeater control has a question related to yours that can help you if you still need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.