SQL Aggregate Functions Using ASP.NET
AggregateDemo.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AggregateDemo.aspx.cs" Inherits="WebApplication8.AggregateDemo" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lbl" runat="server" Text=""></asp:Label><br />
<asp:Button ID="btn" runat="server" Text="GetMaxValue" OnClick="btn_Click" />
<asp:Button ID="btnAvg" runat="server" Text="GetAvgSalary" Width="121px" OnClick="btnAvg_Click" />
</div>
</form>
</body>
</html>
AggregateDemo.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace WebApplication8
{
public partial class AggregateDemo : System.Web.UI.Page
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select max(salary) from demo";
cmd.Connection = con;
con.Open();
int max_Salary = (int) cmd.ExecuteScalar();
con.Close();
lbl.Text = max_Salary.ToString();
}
protected void btnAvg_Click(object sender, EventArgs e)
{
SqlConnection con1 = new SqlConnection(cs);
SqlCommand cmd1 = new SqlCommand();
cmd1.CommandText = "select avg(salary) from demo";
cmd1.Connection = con1;
con1.Open();
int avg_Salary = (int)cmd1.ExecuteScalar();
con1.Close();
lbl.Text = avg_Salary.ToString();
}
}
}
Comments
Post a Comment