Feeding codebehind list and displaying value in Asp

Asked

Viewed 89 times

1

I am for the first time having deeper contact with an application in VB.NET. In this case, I need to feed a list that I am 'turning into an object' and later, display it on my Asp. I’m usually used to working with C# in MVC and so I’m having serious difficulty here.

In my codebehind I have the methods:

Sub Main()
    Dim Pedidos = GetPedidos()
    DisplayList(Pedidos)
    Console.ReadLine()
End Sub

Sub DisplayList(ByVal Pedidos As IEnumerable(Of InformacoesPedido))
    For Each Pedido As InformacoesPedido In Pedidos
        Console.WriteLine("Nome do Agente: " & Pedido.Agente)
        Console.WriteLine()
    Next
End Sub

Function GetPedidos() As IEnumerable(Of InformacoesPedido)
    Dim MyConnectionCarrossel = New OleDbConnection(CONEXAO)
    MyConnectionCarrossel.Open()
    Dim ComandoCa As New OleDbCommand
    ComandoCa.Connection = MyConnectionCarrossel
    ComandoCa.CommandText = "SELECT * from tabela"
    Dim readerCa As OleDbDataReader = ComandoCa.ExecuteReader()

    Return New List(Of InformacoesPedido) From
        {
         New InformacoesPedido("Teste", "Teste 2", "Teste 3", "Teste 4", "Teste 5", "Teste 6")
        }

End Function

Public Class InformacoesPedido
    Public Property Agente As String
    Public Property Cliente As String
    Public Property Pedido As String
    Public Property Tipo As Integer
    Public Property Data As Integer
    Public Property ValorTotal As Integer

    Public Sub New()

    End Sub

    Public Sub New(ByVal AgenteNome As String,
                   ByVal ClienteNome As String,
                   ByVal PedidoNome As String,
                   ByVal TipoNome As Integer,
                   ByVal DataPedido As Integer,
                   ByVal ValorTotalPedido As Integer)

        Agente = AgenteNome
        Cliente = ClienteNome
        Pedido = PedidoNome
        Tipo = TipoNome
        Data = DataPedido
        ValorTotal = ValorTotalPedido
    End Sub
End Class

My idea was to feed my list as follows:

Return New List(Of InformacoesPedido) From
        {
    While readerCa.Read
         New InformacoesPedido(readerCa.Item("X"), readerCa.Item("Y"), readerCa.Item("Z"), readerCa.Item("A"), readerCa.Item("B"), readerCa.Item("C"))
    End While
        }

But I’ve seen that it’s not possible. So how could I be feeding my list to the basis of my query?

How can I call my Function GetPedidos() in Asp to display the results obtained?


@EDIT

I was able to access my method as follows:

    <%For Each X In GetPedidos()%>
     <h2><%=X.Agente%></h2>
    <%Next%>

My biggest problem right now is how to feed my list with query results

I removed the following method:

Sub DisplayList(ByVal Pedidos As IEnumerable(Of InformacoesPedido))
For Each Pedido As InformacoesPedido In Pedidos
    Console.WriteLine("Nome do Agente: " & Pedido.Agente)
    Console.WriteLine()
Next
End Sub
  • 1

    I’m having a little trouble understanding your code (VB for me is Greek), but as I believe your problem is with the notion of the life cycle of a Webforms application. you have a list declared inside your Page, it is initially loaded in Pageload, some event on your page manipulates this list, but in Callback the List is not available. this would be the problem?

1 answer

0


Solved

Dim Valores = New List(Of ProdutosPedido)
While readerCa.Read
Valores.Add(New ProdutosPedido(readerCa.Item("X"), readerCa.Item("Y"), readerCa.Item("Z"), readerCa.Item("A"), readerCa.Item("B"), readerCa.Item("C")))
End While

Browser other questions tagged

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