Hi micah,
Check this example. Now please take its reference and correct your code.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
<asp:TextBox runat="server" ID="txtbox1" />
<asp:TextBox runat="server" ID="txtbox2" />
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constrr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constrr))
{
SqlCommand command = new SqlCommand("SELECT MAX(CustomerId) AS 'Max',MIN(CustomerId) AS 'Min' FROM Customers ", con);
con.Open();
SqlDataReader sdrr = command.ExecuteReader();
if (sdrr.Read())
{
txtbox1.Text = sdrr["Min"].ToString();
txtbox2.Text = sdrr["Max"].ToString();
}
con.Close();
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constrr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constrr)
Dim command As SqlCommand = New SqlCommand("SELECT MAX(CustomerId) AS 'Max',MIN(CustomerId) AS 'Min' FROM Customers ", con)
con.Open()
Dim sdrr As SqlDataReader = command.ExecuteReader()
If sdrr.Read() Then
txtbox1.Text = sdrr("Min").ToString()
txtbox2.Text = sdrr("Max").ToString()
End If
con.Close()
End Using
End If
End Sub
Screenshot