|
-
Nov 8th, 2006, 11:20 AM
#1
Thread Starter
Lively Member
[RESOLVED] How do I call a Stored Procedure from a VB Sub
I have an Update Stored Procedure on SQL Server and I just simply want to call a Sub that "fires" off that SP.
VB Code:
Sub UpdateTableWithSP()
sqlConn.Open() 'connection declared globally
Dim sqlSP As New SqlCommand("MTFE_UpdateTable")
sqlSP.Connection = sqlConn
sqlSP.CommandType = CommandType.StoredProcedure
sqlSP.ExecuteReader()
sqlConn.Close()
End Sub
This doesn't seem right, how exactly is this supposed to be coded?
Thanks!
f u cn rd ths, u cn gt a gd jb n cmptr prgmmng.
-
Nov 8th, 2006, 11:47 AM
#2
Re: How do I call a Stored Procedure from a VB Sub
VB Code:
Dim sqlSP As New ADODB.Command
Set sqlSP = Nothing
sqlSP.ActiveConnection = sqlConn
sqlSP.CommandType = adCmdStoredProc
sqlSP.CommandText = "MTFE_UpdateTable"
sqlConn.open sqlSP
-
Nov 8th, 2006, 12:17 PM
#3
Thread Starter
Lively Member
Re: How do I call a Stored Procedure from a VB Sub
danasegarane,
Thanks for your reply.
I copy/pasted your example into my sub and (naturally) had to change a couple of things. I made the changes according to what was available in the Intellisense list as I entered the code. Here's where I'm at, at this point:
VB Code:
Dim sqlSP As New SqlCommand 'You had ADODB.Command
sqlSP = Nothing
sqlSP.Connection = sqlConn 'You had "sqlSP.ActiveConnection..."
sqlSP.CommandType = CommandType.StoredProcedure 'You had "adCmdStoredProc" for the command type
sqlSP.CommandText = "MTFE_UpdateTable"
sqlConn.Open()
The biggest issue is, the sqlConn.Open line. I get a "blue squiggly" under the line when I put "sqlSP" in the parenthesis - "Overload resolution failed because no accessible 'Open' accepts this number of arguments."
Any idea how to fix that?
Thanks again!
f u cn rd ths, u cn gt a gd jb n cmptr prgmmng.
-
Nov 8th, 2006, 12:27 PM
#4
Frenzied Member
Re: How do I call a Stored Procedure from a VB Sub
Hello
You could try this to create a command
Code:
sqlCommand cmd = sqlConn.createCommand()
However, this is in c#
I think vb would be:
Code:
dim cmd = sqlcnn.createCommand()
Are you also sure you are initializing your connection with a connection string.
Good website:
www.connectionstrings.com
Steve
-
Nov 8th, 2006, 12:31 PM
#5
Thread Starter
Lively Member
Re: How do I call a Stored Procedure from a VB Sub
Thanks, Steve.
Yes, the connection string is working properly - it's used elsewhere in other Subs.
[Edit]
Also, I did try working with "createcommand", but couldn't figure out where to go with that.
Last edited by capella07; Nov 8th, 2006 at 12:43 PM.
f u cn rd ths, u cn gt a gd jb n cmptr prgmmng.
-
Nov 8th, 2006, 01:05 PM
#6
Thread Starter
Lively Member
Re: How do I call a Stored Procedure from a VB Sub
Got it!
Actually, I found the solution in an ASP.NET book I have (& love dearly), "ASP.NET 2.0: Unleashed" by Stephen Walther, Sams Publishing, (c) 2006.
VB Code:
Dim sqlSP As New SqlCommand("MTFE_UpdateTable", sqlConn)
sqlSP.CommandType = CommandType.StoredProcedure
sqlConn.Open()
Dim reader As SqlDataReader = sqlSP.ExecuteReader
I simply put this code in a Sub and called the sub from a button click event- viola! Updated table!
f u cn rd ths, u cn gt a gd jb n cmptr prgmmng.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|