DA Luces
DA Luces

Reputation: 49

Error in clearing the Items in a List View

I am dealing with an issue in my list view, named "lvPO". Whenever I clicked the "submit" button with this code, I wasn't able to clear the records in my list view.

Code:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = @"INSERT INTO PurchaseOrders VALUES (@SupplierID, @UserID,
        @PaymentMethod, @PurchaseDate, @PaymentDate, @DeliveryDate, @Status, @Remarks); 
        SELECT TOP 1 PONo FROM PurchaseOrders ORDER BY PONo DESC;";
    cmd.Parameters.AddWithValue("@SupplierID", ddlSupp.SelectedValue);
    cmd.Parameters.AddWithValue("@UserID", 1);
    cmd.Parameters.AddWithValue("@PaymentMethod", ddlPM.SelectedValue);
    cmd.Parameters.AddWithValue("@PurchaseDate", DateTime.Now);
    cmd.Parameters.AddWithValue("@PaymentDate", DBNull.Value);
    cmd.Parameters.AddWithValue("@DeliveryDate", DBNull.Value);
    cmd.Parameters.AddWithValue("@Status", DBNull.Value);
    cmd.Parameters.AddWithValue("@Remarks", txtRemarks.Text);
    int PONo = (int)cmd.ExecuteScalar();


    cmd.CommandText = "UPDATE PurchaseOrders SET Status=@Status, " +
        "PaymentDate=@PaymentDate WHERE PONo=@PONo";
    cmd.Parameters.Clear();
    cmd.Parameters.AddWithValue("@PONo", PONo);
    cmd.Parameters.AddWithValue("@Status", "Purchased");
    cmd.Parameters.AddWithValue("@PaymentDate", DBNull.Value);
    cmd.ExecuteNonQuery();
    lvPO.Items.Clear();
    con.Close();
    Response.Redirect("Default.aspx");

}

Here is the result, nothing change in my list view, even though I tried to clear the items based on the code above.

Please help me to clear those items in my list view whenever I hit the button submit. Thanks!

Upvotes: 0

Views: 89

Answers (1)

Matteo1010
Matteo1010

Reputation: 361

I think you can try to use DataBind() after the clear method. If this doesn't work you can try to set the DataSource to null. Bye

Upvotes: 1

Related Questions