2

I've written some code to get some data out of my database. The stored procedure only uses the ID as the parameter, and uses that to filter results. I've run the stored procedure using the EXEC command in SSMS and it works. However, when I try and call it using the code at the bottom, it fails, saying that I have not provided the parameter. Can anyone see what I'm doing wrong?

using (SqlConnection sqlConnect = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{       
    try
    {
        DataTable dtBets = new DataTable("All Bets");
        using (SqlDataAdapter sqlDA = new SqlDataAdapter("up_Select_all", sqlConnect))
        {
            sqlDA.SelectCommand.Parameters.Add("@ID", SqlDbType.BigInt).Value = pCustomer.CustomerID;

            sqlDA.Fill(dtBets);
            return dtBets;
        }                        
    }

    catch (SqlException ex)
    {
        //catch code
    }
1
  • pCustomer is the object we're using. We've put a breakpoint on and it does pass a value. The sqlDA.SelectCommand.Parameters does have a value in there, as it claims the count is at 1. Commented Jan 28, 2013 at 16:57

1 Answer 1

8

You have forgotten to tell the DataAdapter that it should call a Stored-Procedure:

using (SqlDataAdapter sqlDA = new SqlDataAdapter("up_Select_all", sqlConnect))
{
    sqlDA.SelectCommand.CommandType = CommandType.StoredProcedure;
    sqlDA.SelectCommand.Parameters.Add("@ID", SqlDbType.BigInt).Value = pCustomer.CustomerID;

    sqlDA.Fill(dtBets);
    return dtBets;
}    
Sign up to request clarification or add additional context in comments.

Comments

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.