Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello, I am very new to C# and SQL. I have a small routine that imports an invoice from a text file, checks the size of the fields and then loads it to a table. This is all working fine, though I imagine looks very clumsy. I am now trying to add parameters to the code and I am now lost. Can someone please show me how to do this with the existing code or alternately send me down a better path altogether. This is also my first request for help, so advice on framing questions will also be appreciated.

Here is the stored Procedure:

SQL
ALTER PROCEDURE spSelectInvoice
@supplierID int

AS
    SELECT * FROM tblSupplierInvoices WHERE fldSupplierID = @SupplierID



And this the part where I'm trying to get it to work, but after following multiple questions on various forums, I am now lost. How or where do I declare @supplierID as this fall over at da.fill(ds, "tblSupplierInvoices"); with the following error
Procedure or function 'spSelectInvoice' expects parameter '@supplierID', which was not supplied.

// sqlConnection is declared on the form and is correct.

myConnection = sqlConnection;
string connectionString = myConnection.ConnectionString;
string commandString = "EXECUTE spSelectInvoice";

SqlDataAdapter da = new SqlDataAdapter(commandString, connectionString);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, "tblSupplierInvoices");

DataTable dt = ds.Tables["tblSupplierInvoices"];
da.FillSchema(dt, SchemaType.Source);
Posted
Updated 12-Jun-12 19:12pm
v2

1 solution

Try this:
C#
myConnection = sqlConnection;
string connectionString = myConnection.ConnectionString;
string commandString = "spSelectInvoice";
SqlDataAdapter da = new SqlDataAdapter(commandString, connectionString);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.AddWithValue("@supplierID", myIdToSelect);
...
 
Share this answer
 
Comments
Stu Baby 13-Jun-12 1:15am    
Thank you, really appreciated!!
OriginalGriff 13-Jun-12 3:27am    
You're welcome!
VJ Reddy 13-Jun-12 1:15am    
Good answer. 5!

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