*How can I compare the quantity of the UnitsInStock to the quantity the user wants to purchase?*
Here I have a query to my SQL database for the list of columns put into a DataSet. The “where” clause is the item I am browsing currently, extracting all the current information about this product.
I tried to use get the data out into data columns, then I would now need to compare that with the int, quantity.
string selectionString =
"SELECT Products.ProductID, " +
"Products.ProductName, Categories.CategoryName, " +
"Suppliers.CompanyName, Products.QuantityPerUnit," +
"Products.UnitPrice, Products.UnitsInStock " +
"FROM Suppliers INNER JOIN (Categories " +
"INNER JOIN Products ON " +
"Categories.CategoryID = Products.CategoryID) " +
"ON Suppliers.SupplierID = Products.SupplierID " +
"WHERE Products.ProductID = " +
int.Parse(Session["Current Item"].ToString());
DataSet ds = new DataSet();//this is the dataset that keeps my columns and rows.
Then I have a loop to assign all columns except for the last, which is UnitsInStock, towards text boxes. The value that I need to work with is an int, quantity.
Depending on the item that the user opens up, each item’s stock will be different. So the item that I open up, I want to know how I can compare the variable quantity, decided by the user, to the quantity number that I have extracted from the DataSet.
The index of the UnitsInStock column is 6. Don’t worry about QuantityPerUnit, it is nothing.
Here is code that takes ProductName, CatergoryName, CompanyName, QuantityPerUnit, UnitPrice, and UnitsInStock, so you can see how the dataset is used.
if (ds.Tables[0].Columns.Count != 0 &&
ds.Tables[0].Rows.Count != 0)
{
for (int index = 0; index < ds.Tables[0].Columns.Count - 1; index++)/// count -1, explain
{
labelArray[index].Text = ds.Tables[0].Columns[index].ColumnName;//named constant
if (index == 5) // The price field
{
textBoxArray[index].Text = ((Decimal)ds.Tables[0].Rows[0][index]).ToString("F");
}
else
textBoxArray[index].Text = ds.Tables[0].Rows[0][index].ToString();
So, how can I compare the quantity retrieved by the database to the quantity the user wants to purchase, gathered from a textbox.
The easiest way to compare them in your existing code would be another
if (index ==statement in yourforloop: