Results 1 to 6 of 6

Thread: [RESOLVED] How do I call a Stored Procedure from a VB Sub

  1. #1

    Thread Starter
    Lively Member capella07's Avatar
    Join Date
    Oct 2006
    Location
    At work
    Posts
    71

    Resolved [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:
    1. Sub UpdateTableWithSP()
    2. sqlConn.Open() 'connection declared globally
    3.  
    4. Dim sqlSP As New SqlCommand("MTFE_UpdateTable")
    5.  
    6. sqlSP.Connection = sqlConn
    7. sqlSP.CommandType = CommandType.StoredProcedure
    8.  
    9. sqlSP.ExecuteReader()
    10.  
    11. sqlConn.Close()
    12. 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.

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: How do I call a Stored Procedure from a VB Sub

    VB Code:
    1. Dim sqlSP As New ADODB.Command
    2.    Set sqlSP = Nothing
    3.     sqlSP.ActiveConnection = sqlConn
    4.     sqlSP.CommandType = adCmdStoredProc
    5.     sqlSP.CommandText = "MTFE_UpdateTable"
    6.    sqlConn.open sqlSP

  3. #3

    Thread Starter
    Lively Member capella07's Avatar
    Join Date
    Oct 2006
    Location
    At work
    Posts
    71

    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:
    1. Dim sqlSP As New SqlCommand 'You had ADODB.Command
    2. sqlSP = Nothing
    3. sqlSP.Connection = sqlConn 'You had "sqlSP.ActiveConnection..."
    4. sqlSP.CommandType = CommandType.StoredProcedure 'You had "adCmdStoredProc" for the command type
    5. sqlSP.CommandText = "MTFE_UpdateTable"
    6. 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.

  4. #4
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    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
    steve

  5. #5

    Thread Starter
    Lively Member capella07's Avatar
    Join Date
    Oct 2006
    Location
    At work
    Posts
    71

    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.

  6. #6

    Thread Starter
    Lively Member capella07's Avatar
    Join Date
    Oct 2006
    Location
    At work
    Posts
    71

    Resolved 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:
    1. Dim sqlSP As New SqlCommand("MTFE_UpdateTable", sqlConn)
    2. sqlSP.CommandType = CommandType.StoredProcedure
    3. sqlConn.Open()
    4. 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
  •  



Click Here to Expand Forum to Full Width