Show records on a WebTable

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Show records on a WebTable

herberth guzman
Regards, I need help, something I am doing wrong.

I need to display the records of a Table from a DB in a WebTable
The following code does not work for me in a WebForm and WebTable


Public Sub WebTable1_Data(Row As Integer, Column As Integer, Data As
WebTableData)

  Dim i As Integer
  cx = M_DB.Connect()
  rs = cx.Exec("SELECT * FROM tbcategory")

  For i = 0 To rs.count - 1
    Data.Text = rs!Name
    rs.MoveNext
  Next

End

But in a WebComboBox1 if it works the way I need it


Public Sub WebButton8_Click()

  Dim i As Integer
  cx = M_DB.Connect()
  rs = cx.Exec("SELECT * FROM tbcategory")

  For i = 0 To rs.count - 1
    WebComboBox1.Add(rs!Name)
    rs.MoveNext
  Next

End

Someone who can help me solve my problem please.

Nothing works for me, the problem is only with WebTable.

WebTable1.Refresh()
WebTable1.Clear()
WebTable1.Reset()


Regards

Herberth
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|

Re: Show records on a WebTable

Tobias Boege-2
On Fri, 17 Mar 2017, herberth guzman wrote:

> Regards, I need help, something I am doing wrong.
>
> I need to display the records of a Table from a DB in a WebTable
> The following code does not work for me in a WebForm and WebTable
>
>
> Public Sub WebTable1_Data(Row As Integer, Column As Integer, Data As
> WebTableData)
>
>   Dim i As Integer
>   cx = M_DB.Connect()
>   rs = cx.Exec("SELECT * FROM tbcategory")
>
>   For i = 0 To rs.count - 1
>     Data.Text = rs!Name
>     rs.MoveNext
>   Next
>
> End
>
> But in a WebComboBox1 if it works the way I need it
>
>
> Public Sub WebButton8_Click()
>
>   Dim i As Integer
>   cx = M_DB.Connect()
>   rs = cx.Exec("SELECT * FROM tbcategory")
>
>   For i = 0 To rs.count - 1
>     WebComboBox1.Add(rs!Name)
>     rs.MoveNext
>   Next
>
> End
>

The WebTable works differently from a WebComboBox. The difference is much
like gb.gui.base's GridView vs. ComboBox -- but not quite. I think comparing
gb.web.form's controls with the graphical components' helps to understand
and memorise the "new" gb.web.form interface, so I'll talk first about
GridView and ComboBox in the graphical components. If you just want an
answer to your question, skip to the TL;DR.

The ComboBox stores all its elements in its memory, thus you can use
ComboBox.Add() to add /all/ its items beforehand and call it a day.
The GridView allows this "store all" mode of operation, too, by using
the array accessors GridView[Row, Column] to fill it.

But, GridView also supports an "on-the-fly" mode, where it stores only the
data that is currently displayed (I don't know if that is technically
correct (it may not store anything but immediately forward your input to
the display or something) but you can think about it this way). When the
set of rows/columns to be displayed changes (e.g. the user scrolls in the
GridView), it raises its Data event for each cell which is now to be
displayed. The Data event handler receives Row and Column parameters to
tell you which cell content it needs and you provide that data in the
GridView.Data property.

This on-the-fly mode takes a little to get accustomed to and may appear
more difficult in the beginning. It has an advantage over the "store all"
mode though, for *huge* tables, because the "on-the-fly" mode always stores
a constant, and very tiny, amount of data. Especially if you want to display
big databases, you want to avoid having /all/ the data in the database *and*
your Gambas process (which might not even be possible, as the size of the DB
might very well exceed your RAM).

Now, this applies in almost the same way in gb.web.form, but the WebTable
*only* supports the "on-the-fly" mode -- it doesn't have the array accessors
of GridView. And instead of a WebTable.Data property to store the result of
the Data event, you get a Data As WebTableData variable passed to the
WebTable_Data event.

              |  store all  |  on-the-fly
  ------------+-------------+--------------
    ComboBox  |      y      |      n
    GridView  |      y      |      y (GridView.Data property)
    WebTable  |      n      |      y (Data event handler argument)

TL;DR: So, to finally answer your question. What you did wrong was

  For i = 0 To rs.count - 1
    Data.Text = rs!Name
    rs.MoveNext
  Next

Here you iterate through all your result records and fill the Data variable
with everything you encounter. You are supposed to only search the (Row, Column)
cell in your result set and put this single data value into the Data object.
I didn't test it, but the following is approximately how it goes:

  Public Sub WebTable1_Data(Row As Integer, Column As Integer, Data As WebTableData)
    Dim i As Integer
    Dim f As ResultField

    cx = M_DB.Connect()
    rs = cx.Exec("SELECT * FROM tbcategory")

    ' Go to the Row-th record in your result
    For i = 0 To Row - 1
      rs.MoveNext
    Next

    ' I'll assume that the Column-th column is just the Column-th one
    ' in rs.Fields, i.e. you have no special ordering on the columns
    i = 0
    For Each f In rs.Fields
      If i = Column Then Break
      Inc i
    Next

    Data.Text = rs[f.Name]
  End

You will be better off not doing the "M_DB.Connect()" and "cx.Exec()" bits
in every invocation of the Data event. Do it once at the beginning.
How did you set WebTable1.Count without a Result in the first place?
If you did that, a further improvement would be to convert the rs.Fields
collection into an array beforehand, too, so that you can just index this
array with the Column parameter you receive in the Data event to avoid the
For Each loop I did above in order to find the Column-th field name.

Regards,
Tobi

--
"There's an old saying: Don't change anything... ever!" -- Mr. Monk

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|

Re: Show records on a WebTable

Benoît Minisini
Le 17/03/2017 à 20:18, Tobias Boege a écrit :

> I didn't test it, but the following is approximately how it goes:
>
>   Public Sub WebTable1_Data(Row As Integer, Column As Integer, Data As WebTableData)
>     Dim i As Integer
>     Dim f As ResultField
>
>     cx = M_DB.Connect()
>     rs = cx.Exec("SELECT * FROM tbcategory")
>
>     ' Go to the Row-th record in your result
>     For i = 0 To Row - 1
>       rs.MoveNext
>     Next

--> Please use rs.MoveTo(I) instead. :-)

--
Benoît Minisini

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|

Re: Show records on a WebTable

herberth guzman
In reply to this post by herberth guzman
>Le 17/03/2017 à 20:18, Tobias Boege a écrit :
> I didn't test it, but the following is approximately how it goes:
>
>   Public Sub WebTable1_Data(Row As Integer, Column As Integer, Data As WebTableData)
>     Dim i As Integer
>     Dim f As ResultField
>
>     cx = M_DB.Connect()
>     rs = cx.Exec("SELECT * FROM tbcategory")
>
>     ' Go to the Row-th record in your result
>     For i = 0 To Row - 1
>       rs.MoveNext
>     Next

>--> Please use rs.MoveTo(I) instead. :-)

>--
>Benoît Minisini


Tobias - Benoit thanks for your help, it has been useful.
My webtable works fine.


Public Sub WebTable1_Data(Row As Integer, Column As Integer, Data As
WebTableData)

  Dim i As Integer
  Dim f As Resultfield

  cx = M_DB.Connect()
  rs = cx.Exec("SELECT Title FROM tbapps where tbapps.Title like  '%'")

  For i = 0 To Row - 1
    rs.MoveTo(i)
  Next

  If rs.Available Then
    i = rs.Fields.Count
    For Each f In rs.Fields
      If i = Column Then Break
      Inc i
    Next
    Data.Text = rs[f.Name]
  Endif

End

Herberth
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|

Re: Show records on a WebTable

Tobias Boege-2
On Tue, 21 Mar 2017, herberth guzman wrote:

> >Le 17/03/2017 à 20:18, Tobias Boege a écrit :
> > I didn't test it, but the following is approximately how it goes:
> >
> >   Public Sub WebTable1_Data(Row As Integer, Column As Integer, Data As WebTableData)
> >     Dim i As Integer
> >     Dim f As ResultField
> >
> >     cx = M_DB.Connect()
> >     rs = cx.Exec("SELECT * FROM tbcategory")
> >
> >     ' Go to the Row-th record in your result
> >     For i = 0 To Row - 1
> >       rs.MoveNext
> >     Next
>
> >--> Please use rs.MoveTo(I) instead. :-)
>
> >--
> >Benoît Minisini
>
>
> Tobias - Benoit thanks for your help, it has been useful.
> My webtable works fine.
>
>
> Public Sub WebTable1_Data(Row As Integer, Column As Integer, Data As
> WebTableData)
>
>   Dim i As Integer
>   Dim f As Resultfield
>
>   cx = M_DB.Connect()
>   rs = cx.Exec("SELECT Title FROM tbapps where tbapps.Title like  '%'")
>
>   For i = 0 To Row - 1
>     rs.MoveTo(i)
>   Next
>

What Benoit meant was to replace the whole loop above by a single call to

  rs.MoveTo(Row)

>   If rs.Available Then
>     i = rs.Fields.Count
>     For Each f In rs.Fields
>       If i = Column Then Break
>       Inc i
>     Next
>     Data.Text = rs[f.Name]
>   Endif
>
> End
>

--
"There's an old saying: Don't change anything... ever!" -- Mr. Monk

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|

Re: Show records on a WebTable

Fabien Bodard-4
Herberth :

Private $rs as Result
Private $Fields as New String[]

'or somewhere else
Public sub _New()
dim f as ResultField
dim i as integer


 M_DB.Connect()

'DB is the last connected db
  $rs = DB.Exec("SELECT Title FROM tbapps where tbapps.Title like  '%'")


If rs.Available Then
'  Setup Columns count
  TableView1.Columns.Count = rs.Fiels.Count
  'Setup tableHeader
  For each f in rs.Field
     TableView1.Columns[i].Text = f.name
     inc i
  Next
  'Launch table filling
  TableView1.Rows.Count = rs.Count
EndIf

end


Public Sub WebTable1_Data(Row As Integer, Column As Integer, Data As
WebTableData)

  $rs.MoveTo(Row)
'Use the Column Text to get the Field name
Data.Text = $rs[Last.Columns[Column].Text]

End

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user