Solved

App_Code and Need Help

Posted on 2007-12-06
9
981 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 27

Expert Comment

by:Sammy
ID: 20422384
Yes you can declare as many classes as you want within the namespace
0
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

696 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