Hello,
Before starting I should note that I have already looked through every google search result before asking this question
I the following SP in my database:
ALTER procedure [dbo].[chercher_client]
@Code_client nvarchar(50),
@Intitule_client varchar(100)
as
IF (@Code_client is not NULL AND @Code_client!='')
BEGIN
IF (@Intitule_client IS not NULL AND @Intitule_client!='')
select * from Clients where Code_client=@Code_client and Intitule_client=@Intitule_client
ELSE
select * from Clients where Code_client=@Code_client
END
ELSE IF (@Intitule_client is not null AND @Intitule_client!='')
BEGIN
select * from Clients where Intitule_client=@Intitule_client
END
And this is the C# code I am using to execute the SP
<pre> public void Chercher_client(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection(Properties.Settings.Default.Con);
connection.Open();
SqlCommand command = new SqlCommand("chercher_client", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@code_client", chercher_client_code.Text);
command.Parameters.AddWithValue("@Intitule_client", chercher_client_Intitule.Text);
command.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = command;
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
if (dt.Rows.Count < 1)
{
MessageBox.Show("Aucun résultat pour cette recherche");
}
else
{
resultat_client.DataSource = dt;
resultat_client.DataBind();
resultat_client.Visible = true;
}
connection.Close();
}
What I have tried:
I checked that the SP exists in the DB.
I executes the SP on SSMS and it's working fine.
I tried executing other older procedures from the SAME database in the same c# procedure to check if it's a connection problem, but they work fine.
I made a new test procedure with a simple select query and it doesen't work neither.