Solved

App_Code and Need Help

Posted on 2007-12-06
9
974 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

11 Experts available now in Live!

Get 1:1 Help Now