Avatar of fosiul01
fosiul01
Flag for United Kingdom of Great Britain and Northern Ireland asked on

problem with databinding

Hi , i am following a book to learn about data binding

i wrote this function dataload()  in exam.vb.
now i want to call this function from web form, but its saying, " name data load not declared"

where is the problem ??

Thanks in advance

Imports Microsoft.VisualBasic
Imports System.Data
Module exam
 
 
    Public Class exam
        Public Function DataLoad() As ICollection
            Dim dt As DataTable = New DataTable()
            Dim dv As DataView
            Dim dr As DataRow
 
            ' Add two columns to the DataTable
            dt.Columns.Add(New DataColumn( _
             "ExamNumber", GetType(String)))
            dt.Columns.Add(New DataColumn( _
            "ExamName", GetType(String)))
 
            ' Put some data in
            dr = dt.NewRow()
            dr(0) = "305"
            dr(1) = "Web Applications With VB.NET"
            dt.Rows.Add(dr)
 
            dr = dt.NewRow()
            dr(0) = "306"
            dr(1) = "Windows Applications With VB.NET"
            dt.Rows.Add(dr)
 
            dr = dt.NewRow()
            dr(0) = "310"
            dr(1) = "XML With VB.NET"
            dt.Rows.Add(dr)
 
            dr = dt.NewRow()
            dr(0) = "315"
            dr(1) = "Web Applications With Visual C# .NET"
            dt.Rows.Add(dr)
 
            dr = dt.NewRow()
            dr(0) = "316"
            dr(1) = _
             "Windows Applications With Visual C# .NET"
            dt.Rows.Add(dr)
 
            dr = dt.NewRow()
            dr(0) = "320"
            dr(1) = "XML With Visual C# .NET"
            dt.Rows.Add(dr)
 
            ' And return the datatable
            ' wrapped in a dataview
            dv = New DataView(dt)
            Return dv
 
        End Function
 
    End Class
End Module
 
 
webform :
 
 
Partial Class thankyou
    Inherits System.Web.UI.Page
   
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
 
        lbExams.DataSource = DataLoad()
        lbExams.DataTextField = "ExamName"
        DataBind()
 
 
    End Sub
 
    
End Class

Open in new window

.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
fosiul01

8/22/2022 - Mon
SOLUTION
Todd Gerbert

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
fosiul01

ASKER
hi thanks
I guessed i will have to call that function in webform

but problme is, how i will call ?? can you please write that code for me please ?
what code what i write in webform  to call that dataload function from exam class to web form ?
the_bachelor

First do you need to have a Module? I've never seen one used in ASP.NET. I've seen them on Console Apps but...
Anyways, If I were you, i'd take Module exam and End Module out.
Then add the key word Shared to the definition of the function (Public Shared Dataload)
Then you'd be ready to do: lbExams.DataSource = exam.DataLoad()
fosiul01

ASKER
Omm problem is, i am following the code from book  
MCAD/MCSD.NET Training Guide (Exam 70-305): Developing and Implementing Web Applications with Visual Basic .NET and Visual Studio® .NET

if i take your advise, then it would be like this is not it  ?

 Public Shared Function DataLoad() As ICollection
        Dim dt As DataTable = New DataTable()
        Dim dv As DataView
        Dim dr As DataRow
       ........
       ........

  dv = New DataView(dt)
        Return dv

    End Function

if this is right , still its same problem   --1

But if i want to keep as author said, then what is the solution ?? -2
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Todd Gerbert

the_bachelor is right about the Module/End Module statements (I missed that).

Your web form is the "thankyou" class, and the DataLoad() function is in the "exam" class.  Either the DataLoad() function needs to be in the same class as your web form, you need to create an instance of the "exam" class and call it's DataLoad() method, or declare the the DataLoad() as Shared (which makes more sense).

You can't call functions that belong to other classes without somehow referring to the class that contains them.
fosiul01

ASKER
problme is, how i will create instances ?? here is the problem.

Give me these  solution :

1. i want to call dataload function from exam class to thankyou class, but how i will call ?? Please give me the code.( i realy dont know how to call)

2. I tried to make the function, shared, but still its same ,


the_bachelor

fosiul01,
I'm no author. I dont think tgerbert is either. But I'm studying for Exam 70-536 myself. And I ran inton my share of buggy stuff.
Authors are mere mortals like everybody. Sometimes they copy and paste the wrong stuff. Check out the documentation. I believe they provide you/us the readers with a website with corrected code.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
the_bachelor

you create an instance of the class exam by doing
Dim math_exam as exam = new exam
Then can do somthn like lbExams.DataSource = math_exam.DataLoad.
fosiul01

ASKER
HI the_bachelor ,

I understand i will have to write something like this  in thankyou class

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
      Dim mydataload As myexam  = New exam
  ' ????????????????????????????????????????????? how i will associated dataload function here ??
        lbExams.DataSource = DataLoad()
        lbExams.DataTextField = "ExamName"
        DataBind()
 
 
    End Sub


You will just have to show me , how i will  call dataload here
ASKER CERTIFIED SOLUTION
the_bachelor

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
fosiul01

ASKER
Sorry i didnot see your post.

Ok its done with these code :

  Dim datal As exam = New exam
        lbExams.DataSource = datal.DataLoad()
        lbExams.DataTextField = "ExamName"
        DataBind()

And i had to stay with this syntax :

Public Function DataLoad() As ICollection

***** if  write public shared Function DataLoad() As ICollection ***
it gives error with this syntax :
 Dim datal As exam = New exam

Now last question i want to ask :

suppose if i write as you said "public shared Function DataLoad() As ICollection "

How i would of call the dataload() function in thankyou class ??

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
the_bachelor

if you had Shared then you wouldnt have to have an instance of the exam class before you call the DataLoad function.

You could just go directly:
lbExams.DataSource = exam.Dataload()

fosiul01

ASKER
yap, right . i have learned 2 things today

thanks to stay with me, i will close this question now.