Asp.net and C# problem image gallery to display the photo

Asked

Viewed 457 times

1

I’m having trouble displaying the photo in the browser the Repeater brings all the database information except the photo. I entered the data manually in the database.

//TABLE

CREATE TABLE [dbo].[Imoveis](
    [CodImovel] [int] IDENTITY(1,1) NOT NULL,
    [Descricao] [varchar](750) NOT NULL,
    [QtdDorms] [int] NOT NULL,
    [TipoImovel] [varchar](7) NOT NULL,
    [Localizacao] [varchar](6) NOT NULL,
    [Valor] [money] NOT NULL,
    [Operacao] [varchar](7) NULL,
    [Cidade] [varchar](75) NULL,
    [Foto] [varchar](150) NULL
    PRIMARY KEY CLUSTERED 
(
[CodImovel] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

//TRANSFERENCE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Entities
{
    public class Imovel
    {
        public int CodImovel { get; set; }
        public string Descricao { get; set; }
        public int QtdDorms { get; set; }
        public string TipoImovel { get; set; }
        public string Localizacao { get; set; }
        public double Valor { get; set; }
        public string Operacao { get; set; }
        public string Cidade { get; set; }
        public string Foto { get; set; }
    }
}

//DAL: //Connection to the bank

namespace DAL
{
    public class Conexao
    {
        public static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["EDGImoveisConnectionString"].ConnectionString;
        public static SqlConnection connection = new SqlConnection(connectionString);

        public static void Conectar()
        {
            if (connection.State == System.Data.ConnectionState.Closed)
            {
                connection.Open();
            }
        }

        public void Desconectar()
        {
            if (connection.State == System.Data.ConnectionState.Open)
            {
                connection.Close();
            }
        }
    }
}

namespace DAL
{
    public class ImovelDao
    {
        public List<Imovel> ObterTodasCasas()
        {
            try
            {
                var command = new SqlCommand();
                command.Connection = Conexao.connection;
                command.CommandText = "SELECT * FROM Imoveis";

                Conexao.Conectar();

                var reader = command.ExecuteReader();
                var imoveis = new List<Imovel>();

                while (reader.Read())
                {
                    var imovel = new Imovel();

                    imovel.CodImovel = Convert.ToInt32(reader["CodImovel"]);
                    imovel.Descricao = reader["Descricao"].ToString();
                    imovel.QtdDorms = Convert.ToInt32(reader["QtdDorms"]);
                    imovel.TipoImovel = reader["TipoImovel"].ToString();
                    imovel.Localizacao = reader["Localizacao"].ToString();
                    imovel.Valor = Convert.ToDouble(reader["Valor"]);
                    imovel.Foto = reader["Foto"].ToString();

                    imoveis.Add(imovel); 
                }

                return imoveis;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}

//BLL

namespace BLL
{
    public class ImovelBo
    {
        private ImovelDao _ImovelDao;

        public List<Imovel> ObterTodasCasas()
        {
            _ImovelDao = new ImovelDao();
            return _ImovelDao.ObterTodasCasas();
        }
    }
}

//.CS

namespace EDGOficial.Painel
{
    public partial class Imoveis : System.Web.UI.Page
    {
        private ImovelBo _imovelBo;

        protected void Page_Load(object sender, EventArgs e)
        {
            CarregarImoveisNoRepeater();
        }

        private void CarregarImoveisNoRepeater()
        {
            _imovelBo = new ImovelBo();
            RepeaterCasas.DataSource = _imovelBo.ObterTodasCasas();
            RepeaterCasas.DataBind();
        }
    }
}

//.ASPX

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div id="paginas">
        <asp:Repeater ID="RepeaterCasas" runat="server">
            <ItemTemplate>
                <div class="capaCasa">
                    <img src="../Content/ImagensCasas/" <%#DataBinder.Eval(Container.DataItem,"Foto") %> "/>
                </div>
                <div class="NomeCasa">
                    <%#DataBinder.Eval(Container.DataItem,"Descricao") %>
                </div>
                </div>
            </ItemTemplate>
        </asp:Repeater>
    </div>
</asp:Content>

If anyone there can help, thank you very much!! D Esse é o erro que dá

1 answer

3


Right-click on the image and see in the html of the page if the image path is correct. As your field public string Foto { get; set; } is a string so I suppose the image path is stored in the database.

So instead of doing;

 aspx <img src="../Content/ImagensCasas/" <%#DataBinder.Eval(Container.DataItem,"Foto") %> "/>

change its class to;

imovel.Foto = "../Content/ImagensCasas/" + reader["Foto"].ToString(); 

and in aspx

<img src="<%#DataBinder.Eval(Container.DataItem,"Foto") %> "/>

Browser other questions tagged

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