Find Maximum Number From DataBase Table Values Using Sql in Asp.Net c#

Find Maximum Number Of DataBase Table Values Using Sql


Find Maximum Number of Already Registered Values From DataBase Table Using Sql Query in Asp.Net C#.


DMEO




HTML CODING


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Get Maximum Values" />
        <asp:Label ID="lblMaximum" runat="server" ForeColor="Red" Height="29px" Width="276px"></asp:Label>
   
    </div>
    </form>
</body>
</html>


                           C# Coding

//  Maximum Value Get

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;

public partial class Max : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con.Open();

        SqlCommand cmd = new SqlCommand

  ("select MAX (CAST(  amount as INT)) from reg)", con);

        SqlDataReader rd = cmd.ExecuteReader();

        if (rd.Read())
        {

            lblMaximum.Text = "Third Maximum Amount = "+rd[0].ToString();
        
        }

    }
}



// Second Maximum Value Get

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;

public partial class Max : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con.Open();

        SqlCommand cmd = new SqlCommand

  ("select MAX (CAST(  amount as INT)) from reg where amount NOT IN ( select TOP 1 (amount) from reg order by amount desc)", con);

        SqlDataReader rd = cmd.ExecuteReader();

        if (rd.Read())
        {

            lblMaximum.Text = "Second Maximum Amount = "+rd[0].ToString();
        
        }

    }
}



                  // Third Maximum Value Get

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;

public partial class Max : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con.Open();

        SqlCommand cmd = new SqlCommand

  ("select MAX (CAST(  amount as INT)) from reg where amount NOT IN ( select TOP 2 (amount) from reg order by amount desc)", con);

        SqlDataReader rd = cmd.ExecuteReader();

        if (rd.Read())
        {

            lblMaximum.Text = "Third Maximum Amount = "+rd[0].ToString();
        
        }

    }
}


First -  Add  - New WebForm  - Select Button & Label From ToolBox







Next - Add DataBase Values  







Next - Sql Max Number Query & Convert To Integer 









Next - Run [F5]  Get Maximum Number











Next -   Sql Second Max Number Query & Convert To Integer 






Next - Run [F5]  Get Second Maximum Number







Next -   Sql Third Max Number Query & Convert To Integer 






Next - Run [F5]  Get Third Maximum Number








0 comments:

Post a Comment