Return Value from Stored Procedure using .NET

Contributed by
Rating: 3 stars3 stars3 stars3 stars3 stars / 16
December 05, 2001
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Return Value from Stored Procedure

This tip uses ExecuteScalar to return a value from a stored procedure and increment the value by one.



<%@ Page Language="VB" EnableSessionState="False" EnableViewState="False" 

Trace="False"
Debug="False" Strict="True" %>

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.SQLClient" %>

<script language="VB" runat="server">

Sub Page_Load(Sender As Object, E As EventArgs)


Dim myConnection As SqlConnection = _
New SqlConnection(ConfigurationSettings.AppSettings("DSN"))

Dim MyCommand as SQLCommand = New SQLCommand("spReturnarticleId", MyConnection)

MyCommand.CommandType = CommandType.StoredProcedure


myConnection.Open()


'Execute the Stored Procedure, return the value and increment it by 1

Dim count As Int32 = CInt(myCommand.ExecuteScalar()) + 1

dim strID as string = cstr(count)


'Set the value of a textbox

text1.text = strId


'Set the Value of a message to test what value is returned

Message.InnerHtml = strId

myConnection.Close()

End Sub

</script>

<html>

<head></head>

<body>

<h1 align="center">Article Info</h1>

<span id="Message" style="font: arial 11pt;color:#339900;" runat="server"/></h2>

<asp:textbox id="text1" size="20" runat="server" />

</body>

</html>

 

Stored Proc code.

CREATE PROCEDURE spReturnArticleID

as

SELECT max(articleId) FROM articleInfo

RETURN

GO

Table Code

CREATE TABLE [dbo].[articleInfo] (

[articleId] [int] NOT NULL ,

[catID] [int] NOT NULL,

[typeID] [int] NOT NULL,

[fpTitle] [varchar] (255),

[fpMessage] [varchar] (8000),

[titleOfDemo] [varchar] (255),

[href] [varchar] (150),

[demoLink] [varchar] (255),

[downloadLink] [varchar] (255),

[dateSubmitted] [datetime] NULL,

[dateUpdated] [datetime] NULL,

[authorId] [int] NULL,

[active] [bit] NOT NULL,

[pageHits] [int] NULL,

[fileName] [varchar] (150),

[filePath] [varchar] (50),

[fileCreated] [datetime] NULL

) ON [PRIMARY]

GO

ALTER TABLE [dbo].[articleInfo] WITH NOCHECK ADD

CONSTRAINT [DF_tblWhatsNew_new_Active] DEFAULT (1) FOR [active],

CONSTRAINT [PK_tblWhatsNew_new] PRIMARY KEY NONCLUSTERED

(

[articleId]

) WITH FILLFACTOR = 90 ON [PRIMARY]

GO

 
blog comments powered by Disqus
ASP.NET CODE ARTICLES

- How to Use the ListBox Control in ASP.NET 2.0
- How to Load XML Documents in ASP.NET 2.0
- DataGrid Code
- ASP.NET Guestbook
- User Controls and Client Side Scripting
- ASP.NET Programming with Microsoft's AS...
- ASP.NET Basics (part 3): Hard Choices
- ASP.NET Basics (part 2): Not My Type
- ASP.NET Basics (part 1): Nothing But .Net
- Directory Tree Browser
- How to get the confirmation of Yes/No from a...
- Complete example using custom errors and wri...
- Paging Certain # records per page .NET style
- General Methods of formatting and Subtractin...
- .NET LinkButton web control

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 5 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials