(Winforms)Search Button that is using Stored Procedure is not listing a specific record

Cem Tuğanlı 1 Reputation point
2022-12-25T08:59:41.163+00:00

I am using Northwind database in Sql Server.
I am using Stored Procedure to connect Visual Studio with Sql Server and draw data from database.
In CRUD operations, I am tring to write a SEARCH button that is using Stored Procedure.
Other CRUD buttons are working okay.

Search button is not bringing only specific relevant data from the database and display it in the datagridview

It does not give any error, only continues to not Filter and Search data, but display in the DataGridView all the irrelevant & relevant data there is in the customers in the database.

My Stored Procedure :

ALTER PROC [dbo].[benimprocedurum]  
(  
@Id int = null,   
@CustomerID1 varchar(50) = null,  
@CompanyName varchar(50) = null,  
@ContactName varchar(50) = null,  
@Country varchar(50) = null,  
@Action varchar(50) = null  
)  
AS BEGIN     

(...)

Relevant "Select" Part of Stored Procedure Code is this :

IF @Action = 'Select'  
BEGIN  
SELECT * FROM Customers WHERE CustomerID = @CustomerID1   
END  
END  
END  

Search button code in my VS 2022 :

private void AraBtn_Click(object sender, EventArgs e)  
        {  
            if (textBox1.Text == "")  
            {  
                MessageBox.Show("Lütfen Müşteri bilgilerini giriniz");  
                textBox1.Select();  
                return;  
            }  
            using (SqlConnection cn = new SqlConnection(baglanticumlesi))  
            {  
                using (SqlCommand cmd = new SqlCommand("benimprocedurum", cn))  
                {  
                    cmd.CommandType = CommandType.StoredProcedure;  
                    cmd.Parameters.AddWithValue("@Action", "Select");  
                    cmd.Parameters.AddWithValue("@CustomerID1", textBox1.Text);  
                    cn.Open();  
                    cmd.ExecuteNonQuery();  
                    cn.Close();  
                    SqlDataAdapter da = new SqlDataAdapter(cmd);  
                    DataTable dt = new DataTable();  
                    da.Fill(dt);  
                    dataGridView1.DataSource = dt;  
                }  
  
            }  
            Form1_Load(this, null);  
        }  

When button is clicked it does nothing but viewing all datas of all Customers.

Developer technologies | Windows Forms
Developer technologies | Transact-SQL
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-12-25T09:33:46.757+00:00

    As pointed out, the open, ExecuteNonQuery and close are not needed and also the DataAdapter is also not needed. I would recommend that a stored procedure does only one type of operation rather than multiple type of operations.

    using (SqlConnection cn = new SqlConnection(baglanticumlesi))  
    {  
         using (SqlCommand cmd = new SqlCommand("benimprocedurum", cn))  
         {  
             cmd.CommandType = CommandType.StoredProcedure;  
             cmd.Parameters.AddWithValue("@Action", "Select");  
             cmd.Parameters.AddWithValue("@CustomerID1", textBox1.Text);  
    		 cn.Open();  
             DataTable dt = new DataTable();  
             dt.Load(cmd.ExecuteReader());  
             dataGridView1.DataSource = dt;  
         }  
    }  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.