Use C # to create a Web application

Source: Internet
Author: User

Microsoft's. NET technology and C # language can be used to quickly build Web applications. Their security and scalability are much better than ordinary ASP applications. In this article, we will use. NET and C # To build an application step by step.

System Requirements:

Internet Explorer 5.5

Windows 2000 Professional, Server or Advanced Server

ASP +/Microsoft. NET (available in http://msdn.microsoft.com/net)

SQL Server 7.0 + SP1 or later

The Advanced Object Model of Microsoft. NET allows you to quickly create secure, easy-to-upgrade Web applications.

Microsoft's. NET is an ideal technology for developing commercial solutions .. NET technology combined with the elasticity and high performance of C #, it is easier to develop commercial programs than to use ASP only in the past.

In this article, we will use. NET and C # To build a simple commercial application-a program that allows customers to browse your product catalog.

This program contains the most basic. NET and C # technologies. In this article, we will discuss how to build and compile the commercial application layer components of C. We will also talk about how to use the ASPX page to access components and bind data. Using these technologies together, you can spend less time building Web applications than the traditional ASP method.

Solid. NET applications still rely on solid database plans and well-written stored procedures.

Applications can call stored procedures that have been written through components. Most of the multi-layer methods do not recommend calling data directly from the program, which reduces the program speed and is not conducive to debugging.

This article mainly discusses C # and ASP +, but mostly about database creation and stored procedures.

Component development
The c # component to be compiled serves both the logic layer and the data access layer.

If you plan to migrate the program to another database, such as Oracle, you need to put the logic layer and data access layer in two components respectively. This article only uses SQL Server, so you do not need to separate them.

This application requires two pages: Default. aspx and ProductList. aspx. Default. aspx is the first page you can see when accessing the Web. It lists all product types. After you select a category from the Default. aspx menu, the ProductList. aspx page is displayed, showing the list of all products of the current category.

Two actions occur when you use these two pages: the first is to list the product types (Default. aspx), the second is to list the product (ProductList. aspx ). We can create two functions to complete these two tasks. Both functions obtain data by calling the stored procedure. Corresponding to these two functions, we create two classes in a namespace called performancedotnet: Category and Product. (Figure 1) Use cecedotnet. Categories to instantiate the Categories class and use CommerceDotNet. Products to instantiate the Products class. To make the code clearer, we declare these two classes in different files. Since they share a namespace, even different file declarations will become a separate component after compilation.


The CategoryList method transmits a dataset containing all product types to the system. Add the CategoryList Method to the Categories class.

The Code is as follows:

Namespace performancedotnet {

Public class Categories {

Public DataSet CategoryList (){

}

}

}

After this class is created, add code for the CategoryList method.

The CategoryList method can be used to obtain data in four steps: 1. Establish data connections and command objects through SQLConnection and SQLDataSetCommand objects. 2. Set the command object type to stored procedure. 3. Send the result of the stored procedure ListCategory to the dataset. 4. Return the dataset containing the result to the function that calls it.

The complete CategoryList method code is as follows:

Public DataSet CategoryList (){

// Create a data connection and command object

SQLConnection myConnection = new SQLConnection

("Server = localhost; uid = sa; pwd =; database = cecedotnet ");

SQLDataSetCommand myCommand = new SQLDataSetCommand ("ListCategory", myConnection );

// Set the command object type to Stored Procedure

MyCommand. SelectCommand. CommandType = CommandType. StoredProcedure;

// Create and fill a dataset

DataSet myDataSet = new DataSet ();

MyCommand. FillDataSet (myDataSet, "CategoryList ");

// Return the dataset

Return myDataSet;

}

The ProductsList method is similar to the CategoryList method, but ProductsList passes a parameter to the stored procedure.

The Code is as follows:

Public DataSet ProductsList (int categoryID ){

// Create a data connection and command object

SQLConnection myConnection = new SQLConnection

("Server = localhost; uid = sa; pwd =; database = cecedotnet ");

SQLDataSetCommand myCommand = new SQLDataSetCommand ("ListProducts", myConnection );

// Set the command object type to Stored Procedure

MyCommand. SelectCommand. CommandType = CommandType. StoredProcedure;

// Pass parameters to the stored procedure

SQLParameter parameterCategoryID = new SQLParameter ("@ CategoryID", SQLDataType. Int, 4 );

ParameterCategoryID. Value = categoryID;

MyCommand. SelectCommand. Parameters. Add (parameterCategoryID );

// Create and fill a dataset

DataSet myDataSet = new DataSet ();

MyCommand. FillDataSet (myDataSet, "Products ");

// Return the dataset

Return myDataSet;

}

Compile the program after both classes are created.

Run the following command: csc/out: ../bin/CommerceDotNet. dll/t: library/r: System. Data. dll CategoryDB. cs ProductDB. cs.

"/Out:" The switch specifies the compiling database name and storage location. "/t:" The switch tells the compiler to create a library. "/r:" The switch specifies other libraries involved in the component, the complete source code is listed at the end of the article.

ASP + Page Development
After the component is created, the following work is to develop the ASP + page as the user interface. In the previous section, we designed the default. aspx and productslist. aspx pages. The Default. aspx page is the first page after the site is loaded. We will start from creating this default page.

When the page is read, The Page_Load () method is triggered. This method completes the data obtained from the database and bound to the MyList task. First, declare an ICollection variable menuItems. Then, instantiate the component and call the CategoryList method to store the dataset in the menuItems object. Set the data source of MyList to menuItems. Run MyList. DataBind to bind data to MyList.

The Code is as follows:

<Script language = "C #" runat = "server">

Void Page_Load (Object sender, EventArgs e ){

ICollection menuItems;

Performancedotnet. Categories categories = new performancedotnet. Categories ();

MenuItems = categories. CategoryList (). Tables [0]. DefaultView;

MyList. DataSource = menuItems;

MyList. DataBind ();

}

</Script>

Other code in default. aspx is similar to the traditional ASP page. For more information, see the Code listed at the end of this document.

ProductList. aspx and default. aspx is similar, but the code for binding data has some changes, because when developing the C # component, we use CommerceDotNet. parameters are passed to the stored procedure in the ProductsList method. Therefore, the ASP + page also needs to be changed accordingly.

Performancedotnet. Products products = new performancedotnet. Products ();

MyProductList. DataSource = products. ProductsList (categoryId). Tables [0]. DefaultView;

MyProductList. DataBind ();

Now, two ASP + pages are created.

In this way, the most basic application that combines C # and ASP + is created.

This program demonstrates the powerful power of the Microsoft. NET and C # technologies. On this basis, it is easy to develop a powerful site by making further additions and modifications.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.