Click here to Skip to main content
15,844,498 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a a SQL Server stored procedure that is used in a vb.net winforms search form.

SQL
	@JobNumber nvarchar(12) = NULL,
	@CompanyID integer = NULL,
	@Title nvarchar(100) = 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 a.Contact,
                 a.Company_ID, 
                 a.Job,
                 a.Enquiry,
                 a.Start,
                 a.Closed, 
                 a.Title, 
                 b.Company 
                 FROM Job_List a 
                 INNER JOIN Company b ON a.Company_ID = b.Company_ID 
                 WHERE (a.job LIKE @JobNumber + '%' OR @JobNumber IS NULL) 
				 AND	(a.Company_ID = @CompanyID OR @CompanyID iS NULL)
				 AND	(a.title LIKE '%' + @Title + '%'OR @Title IS NULL) 
                 ORDER BY Job


If I execute the stored procedure from SSMS and only supply one parameter it returns the results I would expect.

However, when trying to execute the procedure from within my application using this:

VB
Using con As New SqlConnection(strConnection)

    Using cmd As New SqlCommand("dbo.AA_JobSearch")
        cmd.Connection = con

        cmd.Parameters.AddWithValue("@JobNumber", txtJobNumber.Text)
        cmd.Parameters.AddWithValue("@CompanyID", CompanyID)
        cmd.Parameters.AddWithValue("@Title", txtTitleSearch.Text)

        Using sda As New SqlDataAdapter(cmd)
            Dim dt As New DataTable
            sda.Fill(dt)


(The CompanyID variable is an integer derived from another query) This is data source for a data grid view. Regardless of how many parameters I provide the data grid view is populated with every record in the table, i.e. no filtering takes place.

What I have tried:

I have tried rewriting the VB parameters statements with Add, rather than AddWithValve, but this just results in a string to integer exception. I have spent hours searching the internet for a solution, but haven't yet found anything at works.
If is frustrating as the SQL side works as intended but I can't get the VB.net side to do the same.
Any help would be gratefully received.
Posted
Updated 22-Jul-23 7:51am
Comments
Member 15627495 22-Jul-23 11:14am    
using System;
using System.Data;
using Microsoft.Data.SqlClient;

namespace SqlCommandCS
{
    class Program
    {
        static void Main()
        {
            string str = "Data Source=(local);Initial Catalog=Northwind;"
                + "Integrated Security=SSPI";
            string qs = "SELECT OrderID, CustomerID FROM dbo.Orders;";
            CreateCommand(qs, str);
        }
        private static void CreateCommand(string queryString,
            string connectionString)
        {
            using (SqlConnection connection = new SqlConnection(
                       connectionString))
            {
                SqlCommand command = new SqlCommand(queryString, connection);
                command.Connection.Open(); ' <------
                command.ExecuteNonQuery();
            }
        }
    }
}


https://learn.microsoft.com/en-us/dotnet/api/microsoft.data.sqlclient.sqlconnection?view=sqlclient-dotnet-standard-5.1
Richard MacCutchan 22-Jul-23 11:50am    
Try running it from your VB code with the single parameter that you know works in SSMS. That will at least tell you if the basic settings are correct.

1 solution

You didn't tell SQL it was a stored procedure: add:
VB
cmd.CommandType = StoredProcedure
 
Share this answer
 
Comments
Aaron Tearle 23-Jul-23 12:50pm    
Adding to stored procedure line certainly helped!
I however still can't get the form to behave as the stored procedure does. The is issue I am having is with partial string matches. I have the wildcards in the store procedure and this works fine - it returns the records I would expect to see. In the form however it doesn't. In the form, if I feed in an exact match for a string in one of the fields I am searching, then the correct record is returned, if I input a partial match, nothing is returned. I have tried moving the wildcard from the stored procedure to the form parameter, but this doesn't seem to make any difference.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900