Last post Aug 06, 2014 04:57 PM by Siva Krishna Macha
Aug 06, 2014 02:57 PM|borisre|LINK
I'm starting a new website and I built a class for DB connections in an external cs file.
how can I call/refer the class from my aspx.cs file so it won't be a must for me in every page to write the DB connection strings etc.
Aug 06, 2014 03:03 PM|MetalAsp.Net|LINK
Usually connectionstring go in web.config. but certainly you can create a class for data access. That's a good idea. But what problem are you facing with that?
Aug 06, 2014 03:08 PM|borisre|LINK
thanks for replying...
I'm not facing a real problem or some errors...
I'm just quite new to programming with C#...
so I just would like to know how can I use my Class that is in cs file into my aspx.cs?
Aug 06, 2014 04:03 PM|wim sturkenboom|LINK
I have a class in the App_Code directory for DB stuff. Just two methods for demo purpose
public class db_highlevel
public DataTable getSoftwarebuilds(int iDecoderModelID, ref string errmsg)
public int addSoftwarebuild(int iDecoderModelID, string strBuildNumber, int iProjectID, ref string errmsg)
To use them in your webpages, you can create an instance of the above class
App_Code.db_highlevel db = new App_Code.db_highlevel();
and next access the methods
To add a software build
int affectedrows = db.addSoftwarebuild(decodermodelid, buildnumber, projectid, ref errmsg);
To retrieve them
DataTable tblSoftwarebuilds = db.getSoftwarebuilds(decodermodelid, ref errmsg);
Aug 06, 2014 04:05 PM|MetalAsp.Net|LINK
If it's a static class, you just call the methods directly, like: Class1.WriteToDatabase("hello");
If it's not a static class, you'll need to instantiate an object first: Class1 cls = new Class1(); and then call method: cls.WriteToDatabase("Hello"),
You might benefit from a C# book, or tutorials...
Aug 06, 2014 04:57 PM|Siva Krishna Macha|LINK
In case if you have written (or reusing) the class in an external C# Class library project, then you would need to add reference and then use it with corresponding namespace in your application. But hopefully you have your class in just App_Code folder in
which case the above code example should suffice.