Link to home
Start Free TrialLog in
Avatar of neonlights
neonlights

asked on

App_Code and Need Help

Hi,

I am new to ASP.NET.

I have created App_Code folder and placed a class file. Then, I am trying to call it from my site.master.cs.. How do I do that?
my class file called - DSClass.css

namespace DSClassNameSpace
{
    public class DSClass : System.Web.UI.Page
    {
        public DSClass()
        {
        }
        public OleDbDataReader Select_BookIDs()
        {
            OleDbConnection DBConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["AM"].ConnectionString); ;
            OleDbDataReader DBReader; // read the output
            string SQLString; // to store the sql statements
            DBConnection.Open(); // connection opened
            SQLString = "SELECT DISTINCT PayYear FROM TblPayPeriods";
            OleDbCommand DBCommand = new OleDbCommand(SQLString, DBConnection);
            DBReader = DBCommand.ExecuteReader();
            return DBReader;
            DBReader.Close();
            DBConnection.Close();
        }
    }
}
Avatar of Sammy
Sammy
Flag of Canada image

you have to use the namespace
using DSClassNameSpace; should be placed after all using statements

then anywhere in the page cs file you can do
private OleDbDataReader mydbReader = Select_BookIDs();

if(mydbReader!=null)
{
//use reader here

}
ASKER CERTIFIED SOLUTION
Avatar of Göran Andersson
Göran Andersson
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of neonlights
neonlights

ASKER

Thanks Sammy and GreenGhost.

Just one question: From Sammy's method - I can declare many different classess.. right?

because, if I use In the declaration of your page, change System.Web.UI.Page to DSClassNameSpace.DSClass, I can only call one class..?

Thanks again.
Yes you can declare as many classes as you want within the namespace
Hi GreenGhost.. Sammy,

one bug:

I am using GreenGhost method -

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="site.master.cs" Inherits="Mine.site" %>
getting error message saying that
'Mine.site' is not allowed here because it does not extend class 'System.Web.UI.MasterPage'.

My aspx.cs has:

namespace Mine
{
    public partial class site : DSClassNameSpace.DSClass
> Just one question: From Sammy's method - I can declare many different classess.. right?

Well, Sammys method doesn't work at all. You can't call a method in a class without an instance of the class.


You have inherited the System.Web.UI.Page class in your class. That is done when you use the class as a base for your aspx page class. A page class can only inherit one base class.

If you don't want to use the class as a base class for your page, you shouldn't inherit the System.Web.UI.Page class. Then you also have to create an instance of the class to use it:

DSClass ds = new DSClass();
OleDbDataReader reader = ds.Select_BookIDs();


A namespace can contain as many classes you like, and each class can contain as many methods you like.
Thanks GreenGhost.. that was very useful.

Do you see Select_BookIDs - this is for a combo box. to fill my combo box.

This is what I have:

in my form_load:

                            ds.DbOpen();
                            OleDbDataReader reader = ds.Select_PayYears();
                            DropDownList1.DataSource = reader;
                            ds.DbClose();

and class code same as before.. how come it is not working.. my dropdown is empty.
Settings the data source does not read the data from it. Call the DataBind method:

ds.DbOpen();
OleDbDataReader reader = ds.Select_PayYears();
DropDownList1.DataSource = reader;
DropDownList1.DataBind();
ds.DbClose();
Thank you GreenGhost.. nice name...

It is working finally.. there was a one mistake in my code..

forgot to add before binding:                            

DropDownList1.DataTextField = "PayYear";
DropDownList1.DataValueField = "PayYear";

Thank you very much for your help and liked the idea of having DbOpen() and DbClose() separate...

Thanks again