Results 1 to 9 of 9

Thread: help in calling a function in ASP.Net Project

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    3

    help in calling a function in ASP.Net Project

    I have a function call declared in .aspx file that I call from the page_load event and it works fine. But when I move the function call to a Module file, it is not able to find it? The error message that I am getting is function name not declared. How do I call that function from my Page_load event. Please help.

  2. #2
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    I am new to ASP.NET so you will have to bear with me.

    Is your function private or public.
    I presume is is not a class module.


    Things I do when I am bored: DotNetable

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    3
    I have created a ASP.net web application project and added a module file in the project. The function is public.

  4. #4
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    I was thinking today and was wondering if you need an imports statement at the beginning of you aspx code.

    imports modulename

    This is a bit of a stab in the dark.

    I understand about classes and can add them so they work, and they are meant to be more difficult than straight modules.

    Ah well.


    Things I do when I am bored: DotNetable

  5. #5
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    If it is a Class file (.vb) then you can just do this
    VB Code:
    1. 'Functions is the name of your file (Functions.vb)
    2. Dim f As New Functions
    3. Dim strName As String
    4.  
    5. strName = f.<functionName>(strName)
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    3
    Still It does not work. Here is the code I am using.

    In the Page_load event of the .aspx file I am using the following code:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here



    ' And return the datatable wrapped in a dataview

    lbExams.DataSource = DataLoad()
    lbExams.DataTextField = "ExamName"
    DataBind()

    End Sub


    DataLoad is a Public function in Exam.vb module file. Here is the code in the Exam.vb file.



    Module Exam
    Public Function DataLoad() As System.Collections.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 Module

  7. #7
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    try something like this
    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.    Dim dl As clsDataLoad
    3.  
    4.    lbExams.DataSource = dl.DataLoad()
    5.    lbExams.DataTextField = "ExamName"
    6.    lbExams.DataBind()
    7.  
    8. End Sub
    9.  
    10. Public Class clsDataLoad
    11.  
    12.    'Place function code here
    13.  
    14. End Class
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  8. #8
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    I don't understand your problem. Why not just put this at the top of your web page's .vb file..
    VB Code:
    1. Imports NameOfSomeModuleYouMade

    And stick this anywhere within that .vb file you need to call it.

    VB Code:
    1. Dim myModule As NameOfSomeModuleYouMade
    2. myModule.CallMyFunction

    If you need to call it from the front side (Aspx), make a Protected function inside your web page's .vb file that instantiates the class and calls the function.
    VB Code:
    1. Protected Sub GoCallMyFunction
    2. Dim myModule As NameOfSomeModuleYouMade
    3. myModule.CallMyFunction
    4. End Sub

  9. #9
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    I am lost for suggestions now.

    I created a new web application.
    Added a module.
    In the module I added a simple function so the module looked like this.
    VB Code:
    1. Module Module1
    2.  
    3.     Public Function giveit() As String
    4.         Return "something"
    5.     End Function
    6.  
    7. End Module
    I added a textbox to the webform and added this code in the page_load event.
    VB Code:
    1. TextBox1.Text = giveit()
    When I run the application it worked fine.

    Sometimes it helps to go back to basics. But I am not sure in your case.

    Did you add a new module to the web application or did you add an existing module you had previously written, say in VB6?
    I think as modules go they will be different. A module in a webapp has
    VB Code:
    1. Module Module1
    2. ...
    3. ...
    4. End Module
    This is really all I can think of.


    Things I do when I am bored: DotNetable

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width