简体   繁体   中英

Must declare Scalar Variable

Been looking over loads of other questions of this type but couldn't find any that helped me solved my problem.

Getting the error: Must declare the scalar variable "@SupplierID".

The code that's throwing the error:

 sqlProductFind = @"SELECT * from Product WHERE  SupplierID =  @SupplierID";
        conn = new SqlConnection(connstr);
        FindTheProducts = new SqlCommand(sqlProductFind, conn);
        FindTheProducts.Parameters.Add("@SupplierID", SqlDbType.Int);
        daProduct = new SqlDataAdapter(sqlProductFind, conn); //putting connection string in here
        //cmdProduct = new SqlCommandBuilder(daProduct);
        daProduct.FillSchema(dsProduct, SchemaType.Source, "Product");

I can see you creating a SqlCommand, setting the query and adding a parameter. But then you create a new SqlDataAdapter without using the SqlCommand and it's Parameter. You create the SqlDataAdapter with the query also, but you should have used the SqlCommand so the SqlDataAdapter will be instantiated with the SqlCommand as the SelectCommand.

daProduct = new SqlDataAdapter(FindTheProducts);

Also set the value of @SupplierID so you are actually going to filter on a supplierId.

int supplierId = 1; //Should be an input parameter
FindTheProducts.Parameters.Add("@SupplierID", supplierId);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM