0
我在我的庫存表中添加一個狀態來自listview中的輸入。如果數量超出臨界水平,我想將狀態更改爲可用,但不會添加任何狀態。從列表視圖插入數據使用按鈕外部列表視圖
我試過這段代碼。它是增加庫存,但似乎我的狀態仍然是空
protected void btnAdd_Click(object sender, EventArgs e)
{
bool existingSupply = IsExisting();
foreach (ListViewItem item in lvPODetails.Items)
{
var inventory = new DataTable();
using (var da = new SqlDataAdapter("SELECT * FROM Inventory", con))
{
da.Fill(inventory);
}
var products = new DataTable();
using (var da = new SqlDataAdapter("SELECT * FROM SupplierProducts", con))
{
da.Fill(products);
}
TextBox quantity = (TextBox)item.FindControl("txtQuantity");
Label ltr = (Label)item.FindControl("lblProduct");
TextBox odr = (TextBox)item.FindControl("txtOrdered");
string name = ltr.Text;
int delivered = Convert.ToInt32(quantity.Text);
int ordered = Convert.ToInt32(odr.Text);
string status = String.Empty;
string InventStatus = String.Empty;
int finalQuantity = Convert.ToInt32(inventory.Rows[0]["Quantity"]) + delivered;
int criticalLevel = Convert.ToInt32(products.Rows[0]["CriticalLevel"]);
if (finalQuantity <= criticalLevel && finalQuantity != 0)
{
InventStatus = "Critical";
}
else if (finalQuantity > criticalLevel)
{
InventStatus = "Available";
}
else
{
InventStatus = "Unavailable";
}
if (delivered >= ordered)
{
status = "Complete";
}
if (delivered < ordered)
{
status = "Partially Completed";
}
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
if (existingSupply)
{
cmd.CommandText = "UPDATE Inventory SET Quantity=quantity + @Quantity, [email protected] WHERE [email protected]";
}
else
{
cmd.CommandText = "INSERT INTO Inventory VALUES (@ProductID, @SupplierID, " +
"@Quantity, @CriticalLevel, @Price, @Status, @UserID, @DateAdded)";
}
cmd.Parameters.AddWithValue("@ProductID", name);
cmd.Parameters.AddWithValue("@SupplierID", txtSupplierID.Text);
cmd.Parameters.AddWithValue("@Quantity", delivered);
cmd.Parameters.AddWithValue("@CriticalLevel", DBNull.Value);
cmd.Parameters.AddWithValue("@Price", DBNull.Value);
cmd.Parameters.AddWithValue("@Status", InventStatus);
cmd.Parameters.AddWithValue("@UserID", Session["userid"].ToString());
cmd.Parameters.AddWithValue("@DateAdded", DateTime.Now);
cmd.Parameters.AddWithValue("@DateModified", DateTime.Now);
cmd.ExecuteNonQuery();
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "UPDATE PurchaseOrder SET [email protected] WHERE [email protected]";
cmd.Parameters.AddWithValue("@POStatus", status);
cmd.Parameters.AddWithValue("@PONo", txtPONo.Text);
cmd.ExecuteNonQuery();
con.Close();
}
Response.Redirect("Default.aspx");
}
它顯示了這樣的結果