我在清单表中添加了一个来自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, DateModified=@DateModified WHERE ProductID=@ProductID";
}
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 POStatus=@POStatus WHERE PONo=@PONo";
cmd.Parameters.AddWithValue("@POStatus", status);
cmd.Parameters.AddWithValue("@PONo", txtPONo.Text);
cmd.ExecuteNonQuery();
con.Close();
}
Response.Redirect("Default.aspx");
}
它显示了这样的结果
答案 0 :(得分:0)
代码中唯一缺少的是它调用行中的第一个。我只是添加一个where语句和我想要选择的ID。