Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I'm using ASP.NET3.5 version. I dont know how to use seperate class files in program. Can anyone please help me to create and use class in asp.net 3.5 and use it for sql connection.

Thank You
Posted
Updated 12-Jul-12 20:20pm
v2

Refer a good article on CP which demonstrate how to insert and read data from a SQL Server:
Beginners guide to accessing SQL Server through C#[^]

A class is a construct that enables you to create your own custom types by grouping together variables of other types, methods and events.
It defines the data and behavior of a type.
Refer more details on following MSDN article:
Classes (C# Programming Guide)[^]

This is how you can create a class file and define connection string which can be accessed in you solution all over by creating object of this class file.
C#
public class DBConnection
    {      
        public static string cnnStr;
        public SqlConnection cnTransact;
       
        public string GetConnectionString
        {
            get
            {
                return "ConnectionString"; //Connection strings name in Web.config file
            }
        }

        public DBConnection()
        {
            cnTransact = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        }
    }

Web.config file:
HTML
<connectionstrings>
    <add name="ConnectionString" connectionstring="Server=SQLExpress;uid=abc;pwd=123;database=DBName" providername="System.Data.SqlClient" />
</connectionstrings>

Creating object of class file:
C#
DBConnection DB = new DBConnection();

By this DB object you can access all the methods and properties in that class file in you solution. (Note that, DBConnection is class file name)

To open connection:
C#
DB.cnTransact.Open();

To close connection:
C#
DB.cnTransact.Close();


Refer more about classes:
C# Class[^]
Creating a Simple Class in C#[^]
Creating a Class Factory with C# and .NET[^]
 
Share this answer
 
This is a very huge question to answer. You need to google for this question. Go through videos in YouTube, tutorials of ASP.Net3.5, beginning to learn ASP.Net3.5. There are many resources available if you try to put efforts in searching your requirements in google.
For starters:
http://www.w3schools.com/aspnet/default.asp[^]
how to include my own class file[^]
How to add c sharp class file to my project RSS
[^]
how to convert class(.cs) file to DLL using ASP.NET
[^]
Creating_Class_File_Asp.Net[^]
 
Share this answer
 
Comments
Zukiari 13-Jul-12 2:36am    
Ok, Thank you.
I dont know how to use seperate class files in program. Can anyone please help me to create and use class in asp.net 3.5
For ASP.NET, add classes in App_Code folder. They will directly get compiled and referenced.

All you need to do is:
1. right click your project and select option to 'Add ASP.NET Folder' -> 'App_Code', followed by
2. right click to project again and selecting 'Add new Item' -> myNewClass.cs

A class will be added in your web application. You can use it as per your need.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900