Solved

App_Code and Need Help

Posted on 2007-12-06
9
975 Views
Last Modified: 2013-12-16
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();
        }
    }
}
0
Comment
Question by:neonlights
  • 4
  • 3
  • 2
9 Comments
 
LVL 27

Expert Comment

by:Sammy
ID: 20421710
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

}
0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
ID: 20421758
In the declaration of your page, change System.Web.UI.Page to DSClassNameSpace.DSClass.

In the code:

OleDbDataReader reader = this.Select_BookIDs();

However, you will notice that the reader that you get from the method is rather useless. The database connection has to be open while you read from the data reader.

I suggest that you put an Open and Close method in the class and store the database connection and reader as member variables. Something like:
namespace DSClassNameSpace {
 

    public class DSClass : System.Web.UI.Page {
 

        private OleDbConnection DBConnection;

        private OleDbDataReader DBReader;
 

        public DSClass() {

        }
 

        public void DbOpen() {

            DBConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["AM"].ConnectionString); ;

            DBConnection.Open(); // connection opened

        }
 

        public void DbClose() {

            DBReader.Close();

            DBConnection.Close();

        }
 

        public OleDbDataReader Select_BookIDs()

        {

            string SQLString; // to store the sql statements

            SQLString = "SELECT DISTINCT PayYear FROM TblPayPeriods";

            OleDbCommand DBCommand = new OleDbCommand(SQLString, DBConnection);

            DBReader = DBCommand.ExecuteReader();

            return DBReader;

        }

    }

}
 

Usage:
 

this.DbOpen();

OleDbDataReader reader = this.Select_BookIDs();

// use the reader here...

this.DbClose();

Open in new window

0
 

Author Comment

by:neonlights
ID: 20422321
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.
0
 
LVL 27

Expert Comment

by:Sammy
ID: 20422384
Yes you can declare as many classes as you want within the namespace
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:neonlights
ID: 20422700
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
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20422760
> 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.
0
 

Author Comment

by:neonlights
ID: 20423146
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.
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20423187
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();
0
 

Author Comment

by:neonlights
ID: 20423392
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now