3

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 a SQL Server database using a stored procedure. I am passing one parameter that is a SqlDbType.Varchar(50) type, but I get back a 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

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:

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";
1
  • ...SqlCommand("EXEC dbo.WyszukajPrzesylki @Nazwisko = 'Example';"), con); Commented Dec 14, 2012 at 19:53

1 Answer 1

1

You're doing almost everything right - up to this line:

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

You're creating a SQL data adapter, but only based on the SQL statement of the com - you should use this instead:

SqlDataAdapter ad = new SqlDataAdapter(com);

You should use the whole com object you've already set up with parameters and everything - not just it's SQL statement text!

Sign up to request clarification or add additional context in comments.

1 Comment

This line was wrong SqlDataAdapter ad = new SqlDataAdapter(com.CommandText, con); Instead it should be like this SqlDataAdapter ad = new SqlDataAdapter(com); Can't belive I spent 16 hours on this!!! :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.