Results 1 to 5 of 5

Thread: [RESOLVED] [2005] VB.NET passing parameters to Stored Procedure

  1. #1

    Thread Starter
    Lively Member smilbuta's Avatar
    Join Date
    Apr 2005
    Location
    Orlando
    Posts
    104

    Resolved [RESOLVED] [2005] VB.NET passing parameters to Stored Procedure

    Hello community,
    I am working on learning Stored procedures and parameter passing.

    This is My VB code.
    Code:
    Imports System.Data
    Imports System.Data.SqlClient
    
    
    Public Class Form1
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim nm As String = TextBox1.Text
            Dim rm As Integer = TextBox2.Text
    
            Dim mySqlConnection As SqlConnection = New SqlConnection(getConnectionString)
    
            Dim mysqlcommand As SqlCommand = New SqlCommand("InsertNames", mySqlConnection)
            mysqlcommand.CommandType = CommandType.StoredProcedure
            mySqlConnection.Open()
            mysqlcommand.Parameters.AddWithValue("@name", TextBox1.Text)
            mysqlcommand.Parameters.AddWithValue("@number", TextBox2.Text)
    
            mySqlConnection.Close()
    
    
        End Sub
    
        Private Function getConnectionString() As String
            Return "my connection information Bla bla"
        End Function
    End Class
    This is my Stored procedure on a SQL 2005 server.
    Code:
    USE [TestingArea]
    GO
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE PROCEDURE [smilbuta].[InsertNames]
    	-- Add the parameters for the stored procedure here
    	@name varchar(50),
    	@number int
    AS
    BEGIN
    	SET NOCOUNT ON;
    
      INSERT INTO StProc (fName, uRoom) VALUES (@name, @Number)
    END
    THe project is simple 1 form.. two text boxes , 1 holding a name and the other a number. a button to execute the procedure and pass the parms to the sql server to inserrt using a Stored procedure.
    The Issue is i get no errors at all.. yet my DB is not updated...

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] VB.NET passing parameters to Stored Procedure

    that is because all you are doing is opening and closing your connection....

    after you add the params to the command object, you need to call executenonquery to actually tell the command to execute against the database.

    vb Code:
    1. mysqlcommand.ExecuteNonQuery() 'EXECUTES STORED PROC

    Also make sure you are properly disposing of your database objects when you are doing using them.

  3. #3

    Thread Starter
    Lively Member smilbuta's Avatar
    Join Date
    Apr 2005
    Location
    Orlando
    Posts
    104

    Re: [2005] VB.NET passing parameters to Stored Procedure

    THank you again kleinma,

    but this may sound nieve.. but what do you mean Disopose of my Database objects?.. how do i do this correctly? I understand how to discard or Nothing out my form and objects in VB but how would i do so on the SQL server??.. or am i just totaly confusing myself...

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] VB.NET passing parameters to Stored Procedure

    I just mean most DB classes in .NET implement IDisposable, which means they have a dispose method to clean up their connection to unmanaged (non framework) resources.

    Here is a small example of how one might structure a piece of code to make sure the objects are getting cleaned up, and to have valid error handling, and clean up the objects even if an error happens.

    vb Code:
    1. Dim MySQLConnection As SqlClient.SqlConnection = Nothing
    2.         Dim MySQLCommand As SqlClient.SqlCommand = Nothing
    3.         Try
    4.             MySQLConnection = New SqlClient.SqlConnection("CONNECTION STRING HERE")
    5.             MySQLCommand = New SqlClient.SqlCommand("InsertNames", MySQLConnection)
    6.             'DO DATABASE STUFF HERE
    7.         Catch ex As Exception
    8.             'DO ERROR HANDLING HERE
    9.         Finally
    10.             'DO CLEANUP HERE
    11.             If MySQLCommand IsNot Nothing Then
    12.                 MySQLCommand.Dispose()
    13.                 MySQLCommand = Nothing
    14.             End If
    15.             If MySQLConnection IsNot Nothing Then
    16.                 If MySQLConnection.State <> ConnectionState.Closed Then
    17.                     MySQLConnection.Close()
    18.                 End If
    19.                 MySQLConnection.Dispose()
    20.                 MySQLConnection = Nothing
    21.             End If
    22.         End Try

  5. #5

    Thread Starter
    Lively Member smilbuta's Avatar
    Join Date
    Apr 2005
    Location
    Orlando
    Posts
    104

    Re: [2005] VB.NET passing parameters to Stored Procedure

    I'll go flog myself now....THanx!

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