473,386 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Calling a Stored Procedure from Code

I am using SQL Server 2000 as the back-end. I have created a stored
procedure in SQL server called usp_AddContract. This Stored procedure
inserts a new contract into a contracts table. I have tested the code in
Enterprise Manager and it works correctly. At the time that I run the SP I
pass a contract number and a SSN to the SP and it creates a new contract in
the contracts table with a unique value in the ConNum field and SSN field.
In order to run the stored procedure from within SQL Server, in Query
Analizer I do the following:

DECLARE @retCode int
DECLARE @ConNum nvarchar(10)
DECLARE @SSN nvarchar(11)
SET @ConNum = 'T00003'
SET @SSN = '554319132'
EXECUTE @retCode = usp_AddContract @ConNum,@SSN
PRINT @retCode

My question is, how do I do call the stored procedure under code in Visual
Studio. If I had a button on a form, what code would I write to call and
execute the stored procedure above?

I have fiddled with it myself and tried the following code in a button but I
seem to be missing something with regard to how to actually execute the SP.
(mySQLConnection and myConnectionString are public variables declared
elsewhere in my form)

Dim cmdContracts As New SqlCommand
mySqlConnection = New SqlConnection(myConnectionString)
mySqlConnection.Open()
cmdContracts = mySqlConnection.CreateCommand
cmdContracts.CommandType = CommandType.StoredProcedure
cmdContracts.CommandText = "usp_AddContract"
cmdContracts.Parameters.Add(New SqlParameter("@ConNum", "T00005"))
cmdContracts.Parameters.Add(New SqlParameter("@SSN", "554319132"))
mySqlConnection.Close()

Thank You


Nov 20 '05 #1
2 2091
Yeah, you're are not ever executing the Stored Procedure
add line
cmdContracts.ExecuteNonQuery
after you've filled the parameters

"Woody Splawn" <wo***@splawns.com> wrote in message
news:es**************@TK2MSFTNGP09.phx.gbl...
I am using SQL Server 2000 as the back-end. I have created a stored
procedure in SQL server called usp_AddContract. This Stored procedure
inserts a new contract into a contracts table. I have tested the code in
Enterprise Manager and it works correctly. At the time that I run the SP I pass a contract number and a SSN to the SP and it creates a new contract in the contracts table with a unique value in the ConNum field and SSN field.
In order to run the stored procedure from within SQL Server, in Query
Analizer I do the following:

DECLARE @retCode int
DECLARE @ConNum nvarchar(10)
DECLARE @SSN nvarchar(11)
SET @ConNum = 'T00003'
SET @SSN = '554319132'
EXECUTE @retCode = usp_AddContract @ConNum,@SSN
PRINT @retCode

My question is, how do I do call the stored procedure under code in Visual
Studio. If I had a button on a form, what code would I write to call and
execute the stored procedure above?

I have fiddled with it myself and tried the following code in a button but I seem to be missing something with regard to how to actually execute the SP. (mySQLConnection and myConnectionString are public variables declared
elsewhere in my form)

Dim cmdContracts As New SqlCommand
mySqlConnection = New SqlConnection(myConnectionString)
mySqlConnection.Open()
cmdContracts = mySqlConnection.CreateCommand
cmdContracts.CommandType = CommandType.StoredProcedure
cmdContracts.CommandText = "usp_AddContract"
cmdContracts.Parameters.Add(New SqlParameter("@ConNum", "T00005"))
cmdContracts.Parameters.Add(New SqlParameter("@SSN", "554319132"))
mySqlConnection.Close()

Thank You

Nov 20 '05 #2
You're pretty close; now you need to execute your sp. Before you close your
connection, do this:
dim ret as object = cmdContracts.ExecuteScalar()
Which will give you the return value that you want.

HTH

Josh Moody
VSU Team

--

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
--------------------
From: "Woody Splawn" <wo***@splawns.com>
Subject: Calling a Stored Procedure from Code
Date: Thu, 11 Dec 2003 12:45:18 -0800
Lines: 41
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <es**************@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: 168.158-60-66-fuji-dsl.static.surewest.net 66.60.158.168
Path: cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTN GP08.phx.gbl!TK2MSFTNGP09.
phx.gblXref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.languages.vb:163333
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

I am using SQL Server 2000 as the back-end. I have created a stored
procedure in SQL server called usp_AddContract. This Stored procedure
inserts a new contract into a contracts table. I have tested the code in
Enterprise Manager and it works correctly. At the time that I run the SP I
pass a contract number and a SSN to the SP and it creates a new contract in
the contracts table with a unique value in the ConNum field and SSN field.
In order to run the stored procedure from within SQL Server, in Query
Analizer I do the following:

DECLARE @retCode int
DECLARE @ConNum nvarchar(10)
DECLARE @SSN nvarchar(11)
SET @ConNum = 'T00003'
SET @SSN = '554319132'
EXECUTE @retCode = usp_AddContract @ConNum,@SSN
PRINT @retCode

My question is, how do I do call the stored procedure under code in Visual
Studio. If I had a button on a form, what code would I write to call and
execute the stored procedure above?

I have fiddled with it myself and tried the following code in a button but Iseem to be missing something with regard to how to actually execute the SP.
(mySQLConnection and myConnectionString are public variables declared
elsewhere in my form)

Dim cmdContracts As New SqlCommand
mySqlConnection = New SqlConnection(myConnectionString)
mySqlConnection.Open()
cmdContracts = mySqlConnection.CreateCommand
cmdContracts.CommandType = CommandType.StoredProcedure
cmdContracts.CommandText = "usp_AddContract"
cmdContracts.Parameters.Add(New SqlParameter("@ConNum", "T00005"))
cmdContracts.Parameters.Add(New SqlParameter("@SSN", "554319132"))
mySqlConnection.Close()

Thank You



Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: dw | last post by:
Hello all, I'm having a dickens of a time calling a stored procedure on a connection. Every time I do, it generates an error "Arguments are of the wrong type, are out of acceptable range, or are in...
4
by: Bob Murdoch | last post by:
I have an ASP application that calls a COM function to create a custom report as an Excel file. This works in a synchronous fashion, as long as the report does not take too long to create. If...
18
by: Jarrod Morrison | last post by:
Hi All I was wondering if there is a way to call a stored procedure from inside another stored procedure. So for example my first procedure will call a second stored procedure which when...
3
by: Jack Black | last post by:
Help!! I'm trying to call a custom stored procedure from a VB.Net code-behind page in an ASP.Net application, and I keep getting an error with no real helpful info... Basically, I'm accepting a...
2
by: singlal | last post by:
Hi, my question was not getting any attention because it moved to 2nd page; so posting it again. Sorry for any inconvenience but I need to get it resolved fast. Need your help! ...
4
by: Jack | last post by:
Hi, I am trying to run an example code from a book. However I am getting the following error message: Number: -2147217900 Description: Syntax error or access violation Source: Microsoft OLE...
4
by: eighthman11 | last post by:
I'm calling a stored procedure on a sql server from an access application. I just need the stored procedure to run I do not need any data returned from the stored procedure to my Access...
1
by: amgupta8 | last post by:
Note: This problem occurred when I updated the JDK from 1.3.1 to 1.4.1 or 1.4.2. Nothing else was changed in the code, other than updating the JDK on the database server (dbm cfg parm jdk_path) and...
6
Soniad
by: Soniad | last post by:
Hello, I am excecuting a stored procedure in my ASP page , it has one out parameter (@confirm) . after executing the procedure i want to retreive this out parameter and assign it to variable...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.