Free templates
Meny Search Free templates

Calculations in a SELECT from a SQL database (C#, VB and ASP.NET) | Free code

Here you can download free code (C# and VB) for calculations in a SQL SELECT statement to add, subtract, divide, multiply and sum values for instance.

If you need to do calculations on values in a results list then it can be good to do these calculations directly in the SELECT statement. In a SELECT statement can you, for example, perform calculations on two fields and create a new field for the result or you can calculate the sum (SUM), the average (AVG), the count (COUNT) or the standard deviation (STD) for grouped items.

If you want calculate the sum, the average, the number or the standard deviation of grouped data, you have to group records by entering the argument GROUP BY and the fields to be displayed as unique records without merging. To avoid problems with null values in sum calculations can you use the COALESCE argument to assign the value to 0 if the value would have had a "null" value.

This code for calculations in a SELECT statement is divided into two files, an aspx page where information from a database are presented and a code page (C# or VB) that contains code for the SQL SELECT statement. The web page for the form is not connected to a "MasterPage", a web project should contain a "MasterPage" that is linked to CSS files so that it becomes easy to maintain the web design for the entire website.

Before you can retrieve data from a database you must have created a database, tables in the database and a named connection string to the database in the "Web.config" file. In this template we have created a connection string to a MySQL database with the name of "ConnectionString" and we make a reference to this connection string when we execute SQL statements.

The code for the SELECT statement is surrounded by a "Try/Catch/Finally" block and this means that any errors in the code are handled by the web application, it is good to avoid to let errors be handled by the web server for a web host for example.

Remember that you in your web hosting account must have made a setting for the correct version of "ASP.NET" on your website (the version listed in the file "Web.config"). We are using Visual Web Developer 2005 (2010) Express Edition for web programming.
Updated: 01-01-2015 | Created by

Download Calculations in a SELECT from a SQL database (C#, VB and ASP.NET) | Free code ยป

Tags: sql database code