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
You are pretty close.
Couple of notes:
You can get a local reference to the list item that was added, which speeds up and cleans up the code
Unless you know that the DB values will never be null, you should always test them for DbNull before using them.
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:
or
Here is a rewrite with these ideas and a bonus loop for setting the subitems (not required):