Search code examples
c#asp.netms-accessparametersoledb

OleDb parameters for SQL statement to Access


I am stuck looking and debugging this code and I can't see any errors. The error at the moment is No Value given for one or more required Parameters

The only column that is required is prodName and that is always checked to make sure the string is not empty, otherwise could it be the length parameters of my integer and currency values? I'm just trying to make them as large as possible to accept most reasonable sized data fields.

I'm also worried that my boolean does not fit the "is null, is not null" categories, but will return false if the checkbox is unclicked for it.

Here's my params, I would greatly appreciated if you guys looked over it briefly for syntax or logic errors i could have made, I am rather intermediate at c#

public int updateProducts(int prodId, string productName, string supplier, string category, string quantityPerUnit, string unitPrice, string unitsInStock, string unitsOnOrder, string reorderLevel, bool discontinued)
{
    int rows = 0;
    int PId = prodId;
    string prodName = productName;
    int supp = int.Parse(supplier);
    int? categoryID = 0;
    string qPerUnit = quantityPerUnit;
    Decimal? uPrice = 0;
    int? uInStock = 0;
    int? uOnOrder = 0;
    int? reorderLvl = 0;
    bool disc = discontinued;
    if (!string.IsNullOrWhiteSpace(unitPrice))
    {
        uPrice = Decimal.Parse(unitPrice);
    }
    if (!string.IsNullOrWhiteSpace(category))
    {
        categoryID = int.Parse(category);
    }
    if (!string.IsNullOrWhiteSpace(unitsInStock))
    {
        uInStock = int.Parse(unitsInStock);
    }
    if (!string.IsNullOrWhiteSpace(unitsOnOrder))
    {
        uOnOrder = int.Parse(unitsOnOrder);
    }
    if (!string.IsNullOrWhiteSpace(reorderLevel))
    {
        reorderLvl = int.Parse(reorderLevel);
    }

    string dbString = "UPDATE [Products] SET [ProductName]= ?,[Supplier]= ?,[Category]= ?,[QuantityPerUnit]= ?,[UnitPrice]= ?,[UnitsInStock]= ?,[UnitsOnOrder]= ?,[ReorderLevel]= ?, [Discontinued]= ? WHERE [SupplierID]= " + PId;

    try
    {
        conn.Open();
        oleCommand = new OleDbCommand(dbString, conn);

        oleCommand.Parameters.Add(new OleDbParameter("@prodName", OleDbType.VarChar, 30, ParameterDirection.Input, false, 10, 0, "ProductName", DataRowVersion.Original, null)).Value = prodName;
        oleCommand.Parameters.Add(new OleDbParameter("@supp", OleDbType.Integer, 40, ParameterDirection.Input, false, 10, 0, "Supplier", DataRowVersion.Original, null)).Value = supp;
        oleCommand.Parameters.Add(new OleDbParameter("@cat", OleDbType.Integer, 30, ParameterDirection.Input, true, 10, 0, "Category", DataRowVersion.Original, null)).Value = categoryID.HasValue ? (object)categoryID : DBNull.Value;
        oleCommand.Parameters.Add(new OleDbParameter("@qPU", OleDbType.VarChar, 20, ParameterDirection.Input, true, 10, 0, "QuantityPerUnit", DataRowVersion.Original, null)).Value = string.IsNullOrWhiteSpace(qPerUnit) ? DBNull.Value : (object)qPerUnit;
        oleCommand.Parameters.Add(new OleDbParameter("@uPrice", OleDbType.Currency, 6, ParameterDirection.Input, true, 10, 0, "UnitPrice", DataRowVersion.Original, null)).Value = uPrice.HasValue ? (object)uPrice : DBNull.Value;
        oleCommand.Parameters.Add(new OleDbParameter("@uInStock", OleDbType.Integer, 50, ParameterDirection.Input, true, 10, 0, "UnitsInStock", DataRowVersion.Original, null)).Value = uInStock.HasValue ? (object)uPrice : DBNull.Value;
        oleCommand.Parameters.Add(new OleDbParameter("@uOnOrder", OleDbType.Integer, 50, ParameterDirection.Input, true, 10, 0, "UnitsOnOrder", DataRowVersion.Original, null)).Value = uOnOrder.HasValue ? (object)uOnOrder : DBNull.Value;
        oleCommand.Parameters.Add(new OleDbParameter("@reorderLvl", OleDbType.Integer, 50, ParameterDirection.Input, true, 10, 10, "ReorderLevel", DataRowVersion.Original, null)).Value = reorderLvl.HasValue ? (object)reorderLvl : DBNull.Value;
        oleCommand.Parameters.Add(new OleDbParameter("@discontinued", OleDbType.Boolean, 10, ParameterDirection.Input, true, 10, 0, "Discontinued", DataRowVersion.Original, null)).Value = disc;
        rows = (int)oleCommand.ExecuteNonQuery();

    }
    catch (Exception ex)
    {
        dbError = "Add Supplier command Error: " + ex.Message;
    }
    finally
    {

        conn.Close();
    }
    return rows;
}

Solution

  • It's the square brackets. Square brackets means Parameter to Jet Sql. But it also means "this identifier has a space in it"

    None of your identifiers have spaces, so just remove the square brackets. If you did have an identifier with a space, you have to fully qualify the name.

    Ref: JET SQL for Access 2003