I have a windows form where the user can input multiple values in multiple textboxes for faster search results. But when running, it takes only 1st parameter i.e., the fullname and ignores the other. Don't know the reason why is it so. Am getting the full table in the result which I don't want. I have created a stored procedure.
CREATE PROCEDURE USP_SELECT_CUSTOMERS (
@fullName VARCHAR(100) = '',
@Address VARCHAR(100) = '',
@ContactNumber VARCHAR(15) = ''
)
AS BEGIN
SELECT * FROM Customers
WHERE ((ContactName LIKE @fullName+'%') OR (@fullName = ''))
OR ((Address LIKE @Address+'%') OR (@Address = ''))
OR ((Phone LIKE @ContactNumber+'%') OR (@ContactNumber = ''))
END
Here's how am i calling the stored procedure in my program :=>
private void btnSubmit_Click(object sender, EventArgs e)
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand("USP_SELECT_CUSTOMERS", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@fullName", txtFullName.Text);
cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
cmd.Parameters.AddWithValue("@ContactNumber", txtContactNumber.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
dataGridView1.Visible = true;
dataGridView1.DataSource = ds.Tables[0];
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
con.Close();
}
}
What I want is even if the 1 textbox if filled, the stored procedure should give the desired output. Kindly help me out in this.
Thanks.


