Well, just like say in VB6 or even access VBA, or vbnet and asp.net web pages?
You can simple add a module to the project, and dump in your helper and handy dandy code routines.
Only ONE BIG new issue?
In those code modules, we often say had variables at the base module (class) level. That you SHOULD NOT do at all, unless those values are to be common to all web users.
In c#, we don't have this common module, but dropping in (adding) a static class to the project behaves very simular to that module in vb.net.
But, once again, do NOT share and have class wide variables, since all users are hitting and running the SAME web site?
Well, for a page load or any other class you use - a NEW instance is created. But for a static public class, a NEW instance is NOT re-created, and any class wide variables ARE IN FACT shared among all users!!!
But, I have a boatload of general code routines (to get connection strings, to get say a data table etc.
So, in vb.net, I might have a grid on a form, and my load code will will call/consume/use code from that global module of code routines that all web pages need and want and can freely use.
So, my markup might be say this GridView
And my code to load can say be this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim rstData As DataTable = MyRst("SELECT * FROM Fighters")
GridView1.DataSource = rstData
GridView1.DataBind()
End If
End Sub
And the results are this:
So, the routine MyRst? That is a global function, placed in my global module that I added to the project (called module 2).
So that code in module2 is this:
Module Module2
Public Function MyRst(strSQL As String, ByVal Optional strCon As String = "") As DataTable
If strCon = "" Then
strCon = My.Settings.TEST4
End If
Dim rstData As New DataTable
Using conn As New SqlConnection(strCon)
Using cmdSQL As New SqlCommand(strSQL, conn)
conn.Open()
rstData.Load(cmdSQL.ExecuteReader)
rstData.TableName = strSQL
End Using
End Using
Return rstData
End Function
etc. etc. etc.
Now, as noted, in c# we don't have just "modules" we can add to the project.
So, above in c# would be this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable rstData;
rstData = General.MyRst("SELECT * FROM Fighters");
GridView1.DataSource = rstData;
GridView1.DataBind();
}
}
Note how I did prefix the routine MyRst with "general". So, I add a static class "General" to the project.
The code would look like this - all static methods/members in that class I called general:
public static class General
{
public static DataTable MyRst(string strSQL)
{
DataTable rstData = new DataTable();
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
{
cmdSQL.Connection.Open();
rstData.Load(cmdSQL.ExecuteReader());
}
}
return rstData;
}
etc. etc. etc. (more functions )
So, the above gives a rundown for c# and vb.net
However, ONE BIG new rule:
Do not create nor use nor have variables that are public/global to the static class - since as noted, they WILL and are actualy shared between all web users. In fact, some attempt to persit and share data/variables between all web users this way - but it is a bad idea, since such values can (and will) often go out of scope.
So, the above quite much gives a run down as to how you can have a fmailer "code library" of routines.
Regards,
Albert D. Kallal (Access MVP 2003-2017)
Edmonton, Alberta Canada