I have a stored procedure that gets data from a table depending on the parameter being passed in:
SELECT *
FROM [dbo].Table1
WHERE Column1 = @Param1 AND Column2 = @Param2
Now, I'm calling the SQL Server stored procedure through VB.Net function with this code:
Public Function GetSPValues(ByVal Param1 As Integer,
ByVal Param2 As String) As DataTable
Dim sqlCon As SqlConnection
Dim dt As DataTable = New DataTable()
sqlCon = strCon
Using (sqlCon)
Dim cmd As New SqlCommand
cmd.Connection = sqlCon
cmd.CommandText = "GetSP"
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@Param1", Param1)
cmd.Parameters.AddWithValue("@Param2", Param2)
sqlCon.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
dt.TableName = "SPValuesTable"
dt.Load(reader)
Return dt
End Using
End Function
The thing is when I try to execute the stored procedure in SQL Server Management Studio, it works properly and returns all the data I need.
But when I try to call from VB.NET, it doesn't return anything at all.
I'm not sure if the parameters I'm passing are really being to the stored procedure. Would you help me with this?
Update: This is my whole SP:
ALTER PROCEDURE [dbo].[GetSP]
@Param1 int = null,
@Param2 nvarchar(50) = null
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT * FROM dbo.tbl1 WHERE Column1 = @Param1 AND Column2
= @Param2
END