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

Joined
Sep 26, 2022
Messages
16
Programming Experience
1-3
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 :
C#:
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 :

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


Search button code in my VS 2022 :

C#:
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.
 
First of all, don't call event handlers directly. It is a very bad habit to get into. If there's code that you want to execute at different times, put it into its own method and then call that method at different times.
 
Why are you calling ExecuteNonQuery and also Fill? Firstly, you want to execute a query, so how could ExecuteNonQuery be useful? Secondly, the Fill call is retrieving the data and populating a DataTable so what is the ExecuteNonQuery call supposed to be for?
 
That Fill method returns the number of records retrieved. When you debug the code, what number is returned and what do you expect to be returned? Can you run the procedure in SSMS with the same parameter values and get a different result set?
 
Back
Top Bottom