Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

SQL
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

C#
<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.
Posted
Updated 29-May-19 5:43am
v2

Try:
C#
command.CommandType = CommandType.StoredProcedure;
 
Share this answer
 
It sounds like the user you're connecting to SQL as doesn't have EXECUTE permission on the stored procedure.
  1. In SSMS, right-click on the stored procedure and select the "Permissions" page.
  2. If your user doesn't appear in the list, click the "Search..." button and enter your username.
  3. Select your user in the list, and click on the "Effective" tab.
  4. If the effective permissions list doesn't include "EXECUTE", then your user doesn't have permission to execute the sproc.

Grant Permissions on a Stored Procedure - SQL Server | Microsoft Docs[^]
 
Share this answer
 

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



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