Results 1 to 2 of 2

Thread: Call storeprocedure with parameter

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784

    Call storeprocedure with parameter

    My store procedure was contained:

    CREATE PROCEDURE [usp_showdepartment] @mHostCode CHAR AS
    SELECT * FROM Department WHERE HostCode = @mHostCode ORDER BY DepartmentName
    GO

    How to call it from vb.net code? .. I tried below codes but I got nothing

    Private Sub LoadDepartment()
    Dim mHostCode As String = UCase(HostCode.Text.Trim)
    Dim SQLDataAdapter As New SqlClient.SqlDataAdapter("usp_showdepartment", SQLConn1)
    Dim DSDepartment As New DataSet("Department")

    SQLDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
    SQLDataAdapter.SelectCommand.Parameters.Add("@mHostCode", SqlDbType.Char)
    SQLDataAdapter.SelectCommand.Parameters("@mHostCode").Value = mHostCode

    DSDepartment.Clear()
    SQLDataAdapter.Fill(DSDepartment, "Department")

    Me.C1TrueDBGrid2.DataSource = DSDepartment.Tables("Department")

    end sub

    please advise ,.. many thanks in advance

    Regards
    Winan

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    This should work:

    VB Code:
    1. Dim mHostCode As String = UCase(HostCode.Text.Trim)
    2.         Dim SQLDataAdapter As New SqlClient.SqlDataAdapter("usp_showdepartment", SQLConn1)
    3.         Dim cn As New SqlConnection(strConnectionString)
    4.         Dim spCommand As New SqlCommand("usp_showdepartment", cn)
    5.  
    6.         spCommand.CommandType = CommandType.StoredProcedure
    7.         spCommand.Parameters.Add("@mHostCode", SqlDbType.Char)
    8.         spCommand.Parameters("@mHostCode").Value = mHostCode
    9.  
    10.  
    11.         Dim da As New SqlDataAdapter(spCommand)
    12.         Dim ds As New DataSet
    13.         da.Fill(ds, "Departments")
    14.         Me.DataGrid1.DataSource = ds.Tables("Departments")

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