Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reuse A Function

Status
Not open for further replies.

Dimitrie1

Programmer
Jan 5, 2007
138
CA
I have a function that I call from several different pages.

I'd like to write it once and make it accessible to the whole website. How do I do this.

Code:
Private Function HaveSerials(ByVal First As Int64, ByVal Last As Int64, ByVal Type As String, ByVal Category As String, ByVal Owner As Int64) As Boolean
        'This function checks to see if the items requested to transfer are actually in the possession of the 
        'Transfer Vendor. 
        'It counts the number of items returned from the select and the number of items if all were available
        'If they match then the Transfer Vendor has them all.  
        'This means that the user cannot ask for serials 1-10 knowing that 9 is unavailable.  They 
        'must ask for items 1-8, 10-10 in 2 separate transfers.


        Dim connStr As String = ConfigurationManager.ConnectionStrings("buspassconnectionstring").ConnectionString
        Dim conn As New SqlConnection(connStr)
        Dim cmd As New SqlCommand("Select count(*) as num_returned from item where Condition = 'Sellable' and Type = '" & Type & "' and Category ='" & Category & "' and Serial_Number between " & First & " and " & Last & " and Owner = " & Owner, conn)
        Dim rdr As SqlDataReader
        Dim number As Int32

        Try
            conn.Open()
            rdr = cmd.ExecuteReader
            rdr.Read()
            If Not IsDBNull(rdr("num_returned")) Then
                number = rdr("num_returned")
            Else
                number = 0
            End If
            rdr.Close()
            conn.Close()
        Catch
            number = 0
        End Try

        If number = Last - First + 1 Then
            HaveSerials = True
        Else
            HaveSerials = False
        End If

    End Function
 

Check out includes:

<!--#include virtual="/inc/functions.asp" -->

and put your code in the file specified.

This can be added anywhere in the code of your asp pages, and will be replaced with the contents of the file you point to BEFORE the page is parsed and processed - important to remember this when designing the use of include files and the way the code will exist as a part of the whole.





A smile is worth a thousand kind words. So smile, it's easy! :)
 
so create a file called functions.asp
put it my code
put the include statement at the top of the page
and just CALL it?
 
I created a file called functions.aspx
on my page I used the imports command but functions.aspx was not available. Should functions.aspx be a class
 
Are you sure the path to your file is correct?

If your include file is in the same directory as your "main" file, a simple:

Code:
<!--#include file="functions.aspx" -->

will work.

functions.aspx should just be an exact copy of the code you have in your first post.

[monkey][snake] <.
 
okay is this right
Code:
Imports System
Imports System.Data
Imports System.Data.SqlClient
<!--#include file="functions.aspx" -->


Partial Class Distribution_VendorTransfer_Send
    Inherits System.Web.UI.Page
    Private currentUser As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        currentUser = HttpContext.Current.Request.LogonUserIdentity.Name.ToLower
        lblCurrentUser.Text = currentUser
    End Sub
...
end class

then in my code I just reference the function

sorry if this sounds simple but I haven't done much web development in a long time
 
It is ASP.NET, but it should still work the same.

Yes in your code just reference the function.

[monkey][snake] <.
 
sorry - I thought I was in the .NET forum, now I see I clicked the ASP link.

I typed if HaveSerials( and did not get the prompt for the parms - does this mean I did something wrong.

 
.NET doesn't like <!--#include file="functions.aspx" -->
it says Identifier Expected
 
I read where asp.NET does support #include, but you'd be better off asking this in the asp.net forum.

[monkey][snake] <.
 
nah you did - I think you got me on the right track, now I just need to know how to finish it off
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top