Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7005303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:20:02+00:00 2026-05-27T21:20:02+00:00

Hey all this is the first time i am calling a stored procedure via

  • 0

Hey all this is the first time i am calling a stored procedure via vb.net and i would like to make sure i have everything correct before i execute it.

This is my code:

Dim connectionString As String = GetConnectionString()
Dim intCount As Integer = 0

Using connection As New SqlConnection(connectionString)
   Dim command As SqlCommand = connection.CreateCommand()

   Try
      connection.Open()
      command.CommandType = CommandType.StoredProcedure
      command.CommandText = "Complete_S_H"
      command.Parameters.Add("@J_ID", SqlDbType.Int)
      command.Parameters.Add("@O_Nbr", SqlDbType.VarChar)
      command.Parameters.Add("@R_Nbr", SqlDbType.VarChar)
      command.Parameters("@theOutput").Direction = ParameterDirection.Output

      Dim dataReader As SqlDataReader = command.ExecuteReader()
      Do While dataReader.Read()
         ListView1.Items.Add(Trim(dataReader(0)))
         ListView1.Items(CInt(intCount)).SubItems.Add(Trim(dataReader(1)))
         ListView1.Items(CInt(intCount)).SubItems.Add(Trim(dataReader(2)))
         ListView1.Items(CInt(intCount)).SubItems.Add(Trim(dataReader(3)))
         ListView1.Items(CInt(intCount)).SubItems.Add(Trim(dataReader(4)))
         ListView1.Items(CInt(intCount)).SubItems.Add(Trim(dataReader(5)))
         intCount = intCount + 1
      Loop

      dataReader.Close()
      connection.Close()
   Catch ex As Exception
      Console.WriteLine(ex.Message)
   End Try
End Using

The stored procedure returns 6 colums worth of data and i would like to add that data to a listview. I’m not sure i have the correct sytax for doing that but this is what i used in my previous sql code (running a query with, not a stored procedure).

Also, i’m not sure how to go about getting data for the @xxx names above from a textbox? How do i pass the values into the @xxx names from the users textbox?

The MS SQL mangement studio code is this for the the stored procedure:

 EXEC [dbo].[Complete_S_H]
@J_ID = 208660,
@O_Nbr = NULL,
@R_Nbr = NULL

Two of the passed varibles can be NULL. Only one needs to be filled out in order for it to return data.

Any help would be great!

David

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:20:02+00:00Added an answer on May 27, 2026 at 9:20 pm

    You are pretty close.

    Couple of notes:

    1. You can get a local reference to the list item that was added, which speeds up and cleans up the code

    2. Unless you know that the DB values will never be null, you should always test them for DbNull before using them.

    3. To use the values from the textbox, you can use Parameters.AddWithValue. I have modified the code to show how.

    The alternative is to set the Parameter’s .Value property once it is added:

    command.Parameters.Add("@J_ID", SqlDbType.Int).Value = CInt(TextBox1.Text)
    

    or

    command.Parameters("@J_ID").Value = CInt(TextBox1.Text)
    

    Here is a rewrite with these ideas and a bonus loop for setting the subitems (not required):

        Dim connectionString As String = GetConnectionString()
    
        Using connection As New SqlConnection(connectionString)
            Dim command As SqlCommand = connection.CreateCommand()
    
            Try
                connection.Open()
                command.CommandType = CommandType.StoredProcedure
                command.CommandText = "Complete_S_H"
                command.Parameters.AddWithValue("@J_ID", CInt(TextBox1.Text))
                command.Parameters.AddWithValue("@O_Nbr", TextBox2.Text)
                command.Parameters.AddWithValue("@R_Nbr", TextBox3.Text)
                command.Parameters("@theOutput").Direction = ParameterDirection.Output
                'command.ExecuteNonQuery()
    
                Dim dataReader As SqlDataReader = command.ExecuteReader()
                Do While dataReader.Read()
                    Dim oItem As ListViewItem
    
                    oItem = ListView1.Items.Add(Trim(dataReader(0)))
    
                    For nI As Integer = 1 To dataReader.FieldCount - 1
                        If Not dataReader.IsDBNull(nI) Then
                            oItem.SubItems.Add(Trim(dataReader(nI)))
                        Else
                            oItem.SubItems.Add(String.Empty)
                        End If
                    Next
                Loop
    
                dataReader.Close()
                connection.Close()
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Using
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey this is my first time trying anything like so I don't know if
Hey all, this is the code i have to check for a day thats
Hey all, I'm having a difficult time wording this properly which is why im
Hey all, i have been trying for a few minutes on this problem and
Hey all, I'm about to rip my hair out. I have this client that
Hey all i am new to XML parsing on VB.net. This is the code
hey all, i have a question about urlmapping in grails. I'm trying to make
Hey Everyone. I'm a first time poster, but I've browsed this site a number
Hey guys, This is my first time making an app with any kind of
Hey guys I'm not sure if I'm going about all this the right way.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.