1

I've got this calling method:

public string RunReportSteps(int _reportKey) {
    DataTable fStepsTable;
    fStepsTable =  GetStepsTable("xxx.console.pr_xxx");
    return (string)fStepsTable.Rows[1][3];
}

It calls this private method:

private DataTable GetStepsTable(string procName) {
    var connectionString = ConfigurationManager.ConnectionStrings["xxx"].ConnectionString;
    using(var conn = new SqlConnection(connectionString))
    using(var adapt = new SqlDataAdapter())
    using(var cmd = new SqlCommand(procName, conn)) {

        conn.Open();

        SqlParameter p = new SqlParameter("@ReportKey", this.ReportKey);
        p.Direction = ParameterDirection.Input;
        cmd.Parameters.Add(p);
        adapt.SelectCommand = cmd;

        DataSet mySet = new DataSet();
        adapt.Fill(mySet);     //<<<<<<<<<<<<<<<<<<<errors here
        return mySet.Tables[0];
    }
}

Why am I getting the following error message?

Procedure or function 'pr_xxx' expects parameter '@ReportKey', which was not supplied.

1
  • 3
    I don't know if it's the root of the issue, but shouldn't you specify that you are using a stored procedure? Commented Nov 26, 2012 at 15:08

1 Answer 1

5

I assume that procName is the name of the stored-procedure. You haven't set the CommandType of the SqlCommand to StoredProcedure:

using(var conn = new SqlConnection(connectionString))
using(var cmd = new SqlCommand(procName, conn))
using(var adapt = new SqlDataAdapter(cmd)) {
    cmd.CommandType = CommandType.StoredProcedure; // <<< this was missing

    SqlParameter p = new SqlParameter("@ReportKey", this.ReportKey);
    p.Direction = ParameterDirection.Input;
    cmd.Parameters.Add(p);

    DataTable table = new DataTable();
    adapt.Fill(table);     
    return table;
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks ...+ I had no real need for the dataset; + I'd confused the order of the adapter and sqlcommand!
@whytheq: note also that you don't need to open the connection manually since the DataAdapter will do it for you in Fill.

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.