Solved

App_Code and Need Help

Posted on 2007-12-06
9
979 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 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