473,378 Members | 1,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Classes and SQL Database in ASP.net

I would like to first start off saying that this is pretty much my first week
of heavily getting into ASP.net. I previously have programmed in ASP Classic,
and now understanding the benefits of ASP.net I would like to cross over.

Anyways onto my question. I am curious how i can go about setting up a class
with the connection to my database in it, which any page can use to connect
to that database. I was never heavily in classes or functions during my ASP
years, so please bare with me.

Thank you so much for all your help...
Also if you know of any good example sites with source i can look at and
read that might further my education on this subject, since that is the best
way i can learn..

~Devin
Nov 18 '05 #1
4 1553
Hi Devin,

Good choice migrating to ASP.Net. Now, get ready to start thinking
differently!
Anyways onto my question. I am curious how i can go about setting up a class with the connection to my database in it, which any page can use to connect to that database. I was never heavily in classes or functions during my ASP years, so please bare with me.
ADO.Net uses Connection Pooling. This is a marked contrast to ADO, where it
was often a good idea to keep a Connection opened, due to the overhead of
creating a new one. With ADO.Net, it is actually a best practice to open and
close Connections as quickly as possible. When they are closed, they are
released into a Pool, where they can be re-used inexpensively, and of course
the advantage is that you don't need to maintain opened (and unused)
Connections. In some cases, it is imperative that you close them. For
example, 2 DataReaders cannot use the same Connection at the same time.
Also if you know of any good example sites with source i can look at and
read that might further my education on this subject, since that is the best way i can learn..
The .Net SDK is a free download, and is some of the best software
documentation I've ever read:

http://www.microsoft.com/downloads/d...displaylang=en

And check out Microsoft's ASP.Net site: http://www.asp.net

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"DevinG" <de***@corsican.com> wrote in message
news:26**********************************@microsof t.com... I would like to first start off saying that this is pretty much my first week of heavily getting into ASP.net. I previously have programmed in ASP Classic, and now understanding the benefits of ASP.net I would like to cross over.

Anyways onto my question. I am curious how i can go about setting up a class with the connection to my database in it, which any page can use to connect to that database. I was never heavily in classes or functions during my ASP years, so please bare with me.

Thank you so much for all your help...
Also if you know of any good example sites with source i can look at and
read that might further my education on this subject, since that is the best way i can learn..

~Devin

Nov 18 '05 #2
TJS
find tutorials and exampes here

www.asp.net
"DevinG" <de***@corsican.com> wrote in message
news:26**********************************@microsof t.com...
I would like to first start off saying that this is pretty much my first
week
of heavily getting into ASP.net. I previously have programmed in ASP
Classic,
and now understanding the benefits of ASP.net I would like to cross over.

Anyways onto my question. I am curious how i can go about setting up a
class
with the connection to my database in it, which any page can use to
connect
to that database. I was never heavily in classes or functions during my
ASP
years, so please bare with me.

Thank you so much for all your help...
Also if you know of any good example sites with source i can look at and
read that might further my education on this subject, since that is the
best
way i can learn..

~Devin

Nov 18 '05 #3
Well you could always do as follows. And since your a new to.Net I'll try and outline things out for you:

1. Create two project in a solution. The first project is aclass library the second is a asp.net web application.
2. In the asp.net web application go to references, click on theprojects tab under the add reference option, and select theclass library project.
3. time to write some code.
4. in the class library setup a subroutine that return a dataset.For example:

//Begin Class Example Here

using System;
using System.Data;
using System.Data.SqlClient;

namespace Sample1
{
public class Sample
{
public Sample() {}

public string ConnectionString;
public string Param1;

public DataSet ViewItems(string val)
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();

SqlConnection Cn = new SqlConnection(ConnectionString);
SqlCommand Cmd = new SqlCommand([STORED PROCEDURE NAME HERE],Cn);
Cmd.CommandType = CommandType.StoredProcedure;

SqlParameter pValue1 = Cmd.Parameters.Add("@Param1",SqlDbType.VarChar);
pValue1.Value = Param1;

Cn.Open();
da.SelectCommand = Cmd;
da.Fill(ds, "VALUES");
}

}
}

//End Class Example

5. Now in the ASP.Net web application double click on the form togo to the code behind page. Now you can create a new Sampleobject and run the dataset. For example:

//Begin Webform1 Example Here

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using Sample1;

namespace SampleProject
{
public class Webform1 : System.Web.UI.Page
{

private void Page_Load(object sender, System.EventArgs e)
{
BindData();
}

public void BindData()
{

DataSet ds = new Dataset();

Sample s = new Sample();
s.ConnectionString =ConfigurationSettings.AppSettings["ConnectionString"];
s.Param1 = "Item1";
ds = s.View();

Repeater1.DataSource = ds;
Repeater1.DataBind();

}

}
}

// End Webform1 Example

Okay, so what did I do here. First I just create a class filethat would get information from a database and set it to adataset. Then on the webform I reference that class andsubroutine and execute it. The I bind the information to arepeater control on the web form.

Hopefully this example will give you so ideas as to how toproceed. Not sure if the example will work word for word -- I'mnot exactly testing it -- but it should be close :)

One thing to note incase it's not entirely evident, the"ConfigurationSettings.AppSettings["ConnectionString"]" is avalue from the web.config file. Just an exmaple of where tostore the connection string.

Alan Washington
http://www.aewnet.com
I would like to first start off saying that this is pretty muchmy first week
of heavily getting into ASP.net. I previously have programmedin ASP Classic,
and now understanding the benefits of ASP.net I would like tocross over.

Anyways onto my question. I am curious how i can go aboutsetting up a class
with the connection to my database in it, which any page canuse to connect
to that database. I was never heavily in classes or functionsduring my ASP
years, so please bare with me.

Thank you so much for all your help...
Also if you know of any good example sites with source i canlook at and
read that might further my education on this subject, sincethat is the best
way i can learn..

~Devin

User submitted from AEWNET (http://www.aewnet.com/)
Nov 18 '05 #4
Since you are new to ASP.NET and interested in how to create a database
driven application I would like to give you an advise to also look into
Microsofts Data Access Block which exposes a help class you can use for db
operations
http://msdn.microsoft.com/library/de...ml/daab-rm.asp

Regards
Thomas
www.infopatrol.com

"DevinG" wrote:
I would like to first start off saying that this is pretty much my first week
of heavily getting into ASP.net. I previously have programmed in ASP Classic,
and now understanding the benefits of ASP.net I would like to cross over.

Anyways onto my question. I am curious how i can go about setting up a class
with the connection to my database in it, which any page can use to connect
to that database. I was never heavily in classes or functions during my ASP
years, so please bare with me.

Thank you so much for all your help...
Also if you know of any good example sites with source i can look at and
read that might further my education on this subject, since that is the best
way i can learn..

~Devin

Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Daedalus.OS | last post by:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of you. If the only answer you can provide is "get a book about OOP" then don't loose your time and mine cause it's already...
2
by: cm500 | last post by:
I'm very new to databases so bear with me. What I need is a way to track the training for the employees at my firm. I have 40 classes that I will be teaching on various subjects and various...
5
by: Sam DeRT | last post by:
Hi. I was wondering if there was anyone that could take a look at the specs for this database and help me through this? http://www.myelasticmind.com/databasespecs.pdf I don't know much...
4
by: JohnR | last post by:
Assume you have a grocery store application. An order consists of an order header with the "customer" info, and multiple "item" records (one for each thing they ordered). Now, in a regular DB...
10
by: Noozer | last post by:
I'm writing an ASP application and have a noob question... I have a class that access an MS SQL database. I have another class also accesses an MS SQL database and this second class uses objects...
35
by: Terry Jolly | last post by:
Web Solution Goal: Have a global database connection Why: (There will be 30+ tables, represented by 30+ classes) I only want to reference the database connection once. I put the connection...
0
by: Nitin Kalia | last post by:
I am developing an project as a part of my MIT Degree Study. I am creating some classes in .net and then I will use these classes in the other application to make its userinterface. I want that the...
1
by: TerryStone | last post by:
I am writing a Windows application, with an SQL Server (Express) database. The application is single user. The Windows application is the only application that accesses the database. I have...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
9
by: Peter Duniho | last post by:
Is there a straightfoward API in .NET that allows for inspection of a database? That is, to look at the structure of the database, without knowing anything in advance about it? For example,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.