Using ExecuteScalar Method without Stored Procedures

 

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 10
May 12, 2002
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

This simple example shows how to use the ExecuteScalar to do a SELECT MAX(column) FROM Table and return the value without using stored procedures.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
If Not IsPostBack Then
Dim dtStartDate As DateTime = DateTime.Now.AddDays(-7)
Dim dtEndDate As DateTime = DateTime.Now.AddDays(-1)
startdate.Text = dtStartDate.ToShortDateString
enddate.Text = dtEndDate.ToShortDateString
literal1.Text = GetEditionNumber()
End If
End Sub
Function GetEditionNumber()
Dim conn As SqlConnection = 
New SqlConnection(ConfigurationSettings.AppSettings("DSN")) Dim cmd As SqlCommand = New SqlCommand("SELECT MAX(Id) from Table", conn) conn.Open() Dim id As String = cmd.ExecuteScalar conn.Close() Return id End Function </script> <html>
<head></head> <body> <form runat="server"> <asp:literal id="literal1" runat="server" /><br> <asp:TextBox id="startDate" runat="server"></asp:TextBox><br> <asp:TextBox id="endDate" runat="server"></asp:TextBox> </form> </body> </html>
Web.Config used in Example
<?xml version="1.0" encoding="utf-8" ?>
<configuration>  <appSettings> <add key="DSN" value="server=localhost;uid=sa;pwd=;Database=pubs"/> </appSettings></configuration>
blog comments powered by Disqus
ASP CODE ARTICLES

- ASP Forms
- ASP: The Beginning
- Getting Remote Files With ASP Continued
- Inbox and Outbox Manipulation in ASP
- Relational DropDownList Using VB.NET
- Ad Tracking URL Hits
- Use ViewState to display one record per page...
- Send Email using ASP.NET formatted in HTML
- ASP File Explorer
- ASP/XML Interview questions by Srivatsan Sri...
- Pressing RETURN won't submit the form
- This shows how you get the TEXT of a combo r...
- Group Data by Adrian Forbes
- Multiple checkbox select sample
- Multiple checkbox select with all values sam...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials