Jump to content

Carregar 5 registros mais recentes do BD em LISTA


Leonardo_Augusto
 Share

Recommended Posts

Olá!

Estou a criar um sistema que mostre na página inicial do site os 5 registros mais recentes do BD em uma LISTA

.

Basicamente a tabela do Banco de dados é assim:

ID

Artigo

URL

Eu preciso carregar os ultimos cinco (5) dados em uma lista e que seja possivel clicar nestes links e redirecionar para a url.

Eu tenho a consulta SELECT TOP [ ] .... FROM [TABELA]

mas não tenho ideia de como criar uma lista do tipo : ÚLTIMAS POSTAGENS.

Edited by Leonardo_Augusto
Link to comment
Share on other sites

Basicamente quero preencher o componente abaixo com os 5 ultimos dados do banco de dados e que tenha Hyperlink

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

<div>

<asp:BulletedList ID="BulletedList1" runat="server">

<asp:ListItem>Dados do BD</asp:ListItem>

<asp:ListItem>Dados do BD</asp:ListItem>

<asp:ListItem>Dados do BD</asp:ListItem>

<asp:ListItem>Dados do BD</asp:ListItem>

<asp:ListItem>Dados do BD</asp:ListItem>

</asp:BulletedList>

</div>

</form>

Edited by Leonardo_Augusto
Link to comment
Share on other sites

tens que primeiro criar o teu dataSource, por exemplo

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=XXX;Initial Catalog=XXX;Persist Security Info=True;User ID=XXX;Password=XXX"
 ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [tabela] order by ID LIMIT 5">
</asp:SqlDataSource>

depois

<form id="form1" runat="server">
<div>
<asp:BulletedList ID="BulletedList1" runat="server">
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<asp:ListItem><a href="<%# DataBinder.Eval(Container.DataItem, "URL") %>"><%# DataBinder.Eval(Container.DataItem, "ID") %><%# DataBinder.Eval(Container.DataItem, "Artigo") %></a></asp:ListItem>
</asp:Repeater>
</asp:BulletedList>
</div>

nao testei , mas a ideia está ai

Edited by apocsantos
geshi
Link to comment
Share on other sites

Estou a tentar assim :

Private Function GetData() As DataSet
 Dim query As String = "SELECT top 5 * FROM Articles"
 Dim cmd As New SqlCommand(query)
 Using sda As New SqlDataAdapter()
	 cmd.Connection = Conexao.Conectar
	 sda.SelectCommand = cmd
	 Using ds As New DataSet()
		 sda.Fill(ds)
		 Return ds
	 End Using
 End Using
End Function

If Not IsPostBack Then
	 ListView1.DataSource = Me.GetData()
	 ListView1.DataBind()
 End If

<asp:ListView ID="ListView1" runat="server" GroupItemCount="3" GroupPlaceholderID="groupPlaceHolder1"
 ItemPlaceholderID="itemPlaceHolder1">
 <LayoutTemplate>
	 <table>
		 <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
	 </table>
 </LayoutTemplate>
 <GroupTemplate>
	 <tr>
		 <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
	 </tr>
 </GroupTemplate>
 <ItemTemplate>
	 <td>
		 <table border="1" style="width: 200px; height: 100px;
			 border: dashed 2px #04AFEF; background-color: #B0E2F5">

			 <tr>
				 <td>
					 <b>Post: </b><span class="Post">
						 <%# Eval("Article")%></span><br />
					 <b>Descrição: </b><span class="Article">
						 <%# Eval("Descricao")%></span><br />
					 <b>Leia Mais: </b><span class="Descricao">
					 <%# DataBinder.Eval(Container.DataItem, "Url")%>"</span><br />
				 </td>
			 </tr>
		 </table>
	 </td>
 </ItemTemplate>
</asp:ListView>

Mas estou recebendo o erro:

Referência de objeto não definida para uma instância de um objeto.

Edited by Leonardo_Augusto
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.