CodeCharge Studio
search Register Login  

Visual Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> .NET

 how to call a custom function from a different page

Print topic Send  topic

Author Message
AndyGB4

Posts: 122
Posted: 01/09/2013, 1:00 PM

Hi I'm pretty new to using .NET (C#),
and I wanted to know how I could call a function that I wrote myself from a separate page.

What I'm trying to do is run a certain function whenever a form is Inserted, Update, etc. And the function I want to run will be on a separate .cs file because I will be using it on multiple pages and don't want to rewrite it each time, or go through each page if I make a slight change.

So basically, what does C# use to "import" (?) a page, and how would I call the function from that page?

Thanks everyone!
View profile  Send private message
cvboucher

Posts: 191
Posted: 01/10/2013, 12:41 PM

I sometimes create function libraries in Visual Studio and drop the .dll in the bin folder of the website. Then I can call these functions from any page.

HTH,
Craig
View profile  Send private message
AndyGB4

Posts: 122
Posted: 01/16/2013, 8:47 AM

Thanks, I'll try this!
View profile  Send private message
AndyGB4

Posts: 122
Posted: 01/16/2013, 9:32 AM

Hmm, ok I'm still pretty confused. Maybe I can give you a bit of on example and you can help me with the steps to make it happen?

So let's say my .dll file is called "testFunctions.dll" and it contains exactly this:
  
namespace testFunctions  
{  
    public class TestAddClass  
    {  
        public static long addition(long x, long y)  
        {  
            //simple function to add 2 numbers  
            return (x + y);  
        }  
    }  
}  
Does this work so far? Is that what I should have in my .dll?

Then where do I save this file in my CodeCharge project folder? You said the bin folder, but I don't see a bin folder anywhere.

And then let's say I have a page on CodeCharge with a Record (we'll call it "RecordPage"), and whenever I Insert a record, I'd like to call that function I mentioned previously. First of all, which page should I be working on? RecordPage.aspx.cs page? And what do I do to be able to link to this page? Something like "using testFunctions;" ?

Sorry for all the questions, I'm just so confused and don't know where to start!

Thanks for any help guys.
View profile  Send private message
cvboucher

Posts: 191
Posted: 01/16/2013, 10:58 AM

You are on the right track. If the bin folders don't exist then create them under the project folder and under the website folder. I usually create a small .bat that copies the .dll to both locations and manually run the .bat file after I build the .dll in Visual Studio. I create this .bat file in the same directory where Visual Studio creates the .dll

Then in CodeCharge, add a Code event where you want to call your functions. This could be After Insert or After Update on a Record form. I'm a VB'er so in VB.Net the code might look something like:

Dim MyResult As Long = testFunctions.TestAddClass.addition(5, 10)

I can't tell if you are using the InMotion templates or the old CCS ASP.Net templates. I prefer the InMotion templates and in CCS5 I don't think you can create a new project with the old templates.

HTH,
Craig
View profile  Send private message
AndyGB4

Posts: 122
Posted: 01/16/2013, 12:07 PM

I'm using CCS5 and the ASP.NET C# option under Project Setting > General, so I'm not using the InMotion Templates.

So you don't need to somehow link to the dll file before you're able to use it's functions?
In other words, you don't need anything at the top of the page like "using" or anything like that?

And also, the code snippet I added before, is that really all I would need in my .dll file or am I missing anything. (obviously the function itself will be more complex than that though)

Thanks for all the help!

View profile  Send private message
cvboucher

Posts: 191
Posted: 01/16/2013, 1:21 PM

No, you don't need to link the .dll. Just copy it to the bin folder. IIS will see it there and pre-compile it. You can add a using for the namespace so you don't have to type the whole namespace when using your class.
C#:
using testFunctions;
VB.Net:
Imports testFunctions
Then in the code event.
Dim MyResult As Long = TestAddClass.addition(5, 10)
Craig
View profile  Send private message
AndyGB4

Posts: 122
Posted: 01/16/2013, 2:16 PM

I tried that, but it didn't work out. It doesn't seem to find or recognize the file, the error message i got now is this:

Compiler Error Message: CS0246: The type or namespace name 'testFunctions' could not be found (are you missing a using directive or an assembly reference?)

The .dll file is in both the Project folder > bin and also the website's folder > bin.
View profile  Send private message
cvboucher

Posts: 191
Posted: 01/17/2013, 12:33 PM

Try prefixing it with your .dll filename but without the .dll. So if your .dll filename is MyUtilLib.dll then your using would be:

using MyUtilLib.testFunctions;

Or in your case, since your .dll is named testFunctions and then you add a testFunctions namespace, your using statement might be:

using testFunctions.testFunctions

There is a Root Namespace proerty in the project properties that can affect your final namespace name.

Craig
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Internet Database

Visually create Web enabled database applications in minutes.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright 2003-2004 by UltraApps.com  and YesSoftware, Inc.