Resolved create a class that read data from db with Repeater Control?

DARK__FOXX

Member
Joined
Sep 2, 2020
Messages
16
Programming Experience
Beginner
Hi everyone,

I created this class to read data from db:

VB.NET:
Public Class Class_LECTURE_TABLE_01
    Dim connection_DB As New Class_CN_DB_XXXXXX("XXXXXXXXXX")
    Dim cn As New SqlConnection(connection_DB.Connessione)
    Dim MyParm As SqlParameter
    Dim cmd As New SqlCommand
    Dim dataAdapter As New SqlDataAdapter(cmd)
    Dim dataTbl As New DataTable
    Private Repeater1 As Repeater

    Public Sub LECTURE_TABLE_01()


        cmd.CommandType = CommandType.StoredProcedure
        cmd.Connection = cn
        cmd.CommandText = "LST_LECTURE_TABLE_01"
        Try
            cn.Open()
            dataAdapter.Fill(dataTbl)
            Repeater1.DataSource = dataTbl
            Repeater1.DataBind()


        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            cn.Close()
        End Try

    End Sub

and I have added inside Page Load on main page , but doesn't show the table that I have created inside file aspx and I don't want to put this method inside main page.

This is as how to call class inside Page Load()


VB.NET:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not Me.IsPostBack Then
Dim lecture_value_TABLE As New Class_LECTURE_TABLE_01()
lecture_value_TABLE.LECTURE_TABLE_01()


End If


End Sub

Thanks for help!!!!
 
At what point do you assign anything to the Repeater1 field inside that class? Nowhere that I can see. It's not going to just magically know to use some control from the page that it knows nothing about. You should get rid of that field and, instead, have the Repeater passed in to a parameter of the method. That way, you could use the class anywhere and pass in the appropriate control when you call it. There's quite a lot wrong with that class and it should ideally be changed a lot but, based on what you are specifically trying to achieve, using a method parameter is the simplest course of action from here.
 
Nevermind I understand how to pass parameter and now works!!
I had to add to the reading method between the brackets (by val) and call after in the method I added in the page load.
Thanks
 

Latest posts

Back
Top