This tutorial will show you how to create your own class in ASP.NET and C#
In this tutorial, we will look at how to create a Custom Class that will represent an object. When working with databases in Web Applications, it is often useful to have an object representing that data, rather than using the built-in types such as a DataTable. By creating your own class, you can gain access to the Properties instead of referencing row and column numbers. An example would be if you're working with data from a database that consisted of a group of people. The columns could be name, age, and telephone. If you used a datatable to retrieve and interact with this data, it could get messy very quickly. But if you created a class to represent the data, then you would be able to programmatically reference the properties (name, age, and telephone) directly.
In this example, we will use a SQL Database and create a class to represent the data. We will keep it simple and use the following columns in the table: ID, Name, Age, Telephone.
Let's begin by starting a new Web Application in Visual Studio. Right-click on the App_Data folder in Solution Explorer, and choose Add New Item.. SQL Server Database. Once it opens up in the Server Explorer window, right-click the Tables folder and choose to Add New Table. Add the following columns and data types:
ID bigint
Name varchar(50)
Age smallint
Telephone varchar(20)
We also want to make the ID column the Primary Key, and in the properties, set Identity Specification to Yes. Then Save the table, and add the Connection String to the Web.config:
| <connectionStrings> <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" /> </connectionStrings> |
If you're unsure about the Connection String, see ConnectionStrings.com
Now let us begin writing our Class. If you don't have an App_Code folder in your Solution Explorer, right-click your Project and choose to Add ASP.NET Folder > App_Code. Then right-click the App_Code folder and choose to Add New Item.. Class. Give it a name. You should then be presented with something like this:
| using System; using System.Collections.Generic; using System.Linq; using System.Web; /// /// Summary description for Person /// public class Person { public Person() } { // }// TODO: Add constructor logic here // |
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
The first thing we should do is add the Properties. We want the Properties to reflect the columns of the table in the database. We will need to set the data types as well:
| #region properties public Int32 ID { get }{ return _ID; }set { _ID = value; }private Int32 _ID = 0; public String Name { get }{ return _Name; }set { _Name = value; }private String _Name = ""; public int Age { get }{ return _Age; }set { _Age = value; }private int _Age = 0; public String Telephone { get }{ return _Telephone; }set { _Telephone = value; }private String _Telephone = "";
Subscribe to:
Post Comments (Atom)
|
No comments:
Post a Comment