Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello
I searched through this site and found some topic about my problem but still cannot make this work....
I am simply trying to retrieve data from SQL Database using a stored procedure.
I am passing one parameter that is a SqlDbType.Varchar(50) type, but get back an SQL error:

Procedure or function 'WyszukajPrzesylki' expects parameter '@Nazwisko', which was not supplied.

What am I doing wrong? Store procedure works if I test it in SSMS ...
It even works when I type

SqlCommand com = new SqlCommand("WyszukajPrzesylki @Nazwisko = Example", con);


You are my last hope guys... I've already lost 2 days with this problem and cannot get any further... don't have any problems with inserting and updating records using c# ... just this....

So here is the code of SP

SQL
ALTER PROCEDURE [dbo].[WyszukajPrzesylki] 
	-- Add the parameters for the stored procedure here
	@Nazwisko varchar(50)
    AS
    BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	-- SELECT * FROM Przesylki WHERE (NazwiskoAdresata = @NazwiskoAdresata)
	SELECT * FROM Przesylki WHERE (Przesylki.NazwiskoAdresata = @Nazwisko)
    END



and action of the button:


C#
SqlConnection con = new SqlConnection(Properties.Settings.Default.Monitoring_PrzesylekString1);
          SqlCommand com = new SqlCommand("WyszukajPrzesylki", con);
          com.CommandType = CommandType.StoredProcedure;
          SqlParameter parametrNazwisko = new SqlParameter("@Nazwisko", SqlDbType.VarChar, 50);
          parametrNazwisko.Value = "Dziubak";
          com.Parameters.Add(parametrNazwisko);

          SqlDataAdapter ad = new SqlDataAdapter(com.CommandText, con);
          DataSet ds = new DataSet();

          ad.Fill(ds, "id");
          con.Close();
          DataTable datatableA = ds.Tables[0];
          dataGridView1.DataSource = ds;
          dataGridView1.DataMember = "id";
Posted
Updated 18-Jul-16 22:47pm
Comments
Aelion 14-Dec-12 14:58pm    
Why I cannot pass parameter using

parametrNazwisko.Value = "Dziubak";

? Any ideas?
__TR__ 14-Dec-12 15:02pm    
Try
com.Parameters.Add(new SqlParameter("@Nazwisko", "Dziubak"));

I found problem solution!

I should pass whole command parameter like this

C#
SqlDataAdapter ad = new SqlDataAdapter(com);


not just com.CommandText ... it was stupid.... but I lost so much time...
 
Share this answer
 
Comments
OriginalGriff 14-Dec-12 15:23pm    
Not stupid, just annoying. We all make mistakes!

You get my five for posting the solution.
Aelion 14-Dec-12 15:55pm    
Thank you... thats my first "five" on this website ;-)
From this sample C# SqlParameter[^] it looks like you should use
C#
SqlParameter parametrNazwisko = new SqlParameter("Nazwisko", SqlDbType.VarChar, 50);
in stead off
C#
SqlParameter parametrNazwisko = new SqlParameter("@Nazwisko", SqlDbType.VarChar, 50);

So remove the '@' from the parameter name.
 
Share this answer
 
v2
since you are using the stored procedure as the SQL statement you dont need to pass the parameter name with @ symbol just pass the name of the parameter SQL will do the rest.

SqlParameter parametrNazwisko = new SqlParameter("Nazwisko", "Dziubak"); 
 
Share this answer
 
Add your solution here
Quote:
Add your solution here
 
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