我想更新TextBoxes并在执行更新时添加ListView行吗?

时间:2019-07-16 18:15:26

标签: c# sql

在更新TextBoxes时,我需要在ListView上添加/编辑新行。我可以做什么?请给我一些建议。谢谢。 我尝试了这段代码:

private void btnUpdate_Click_1(object sender, EventArgs e)
{
    try
    {
        con = new SqlConnection(cs.DBConn);
        con.Open();
        String cb = "update Invoice_info set  VATPer='" + txtTaxPer.Text + "',VATAmount='" + txttableno.Text + "',DiscountPer='" + txtDiscountPer.Text + "',DiscountAmount='" + txtDiscountAmount.Text + "',GrandTotal= '" + txtTotal.Text + "',TotalPayment= '" + txtTotalPayment.Text + "',PaymentDue= '" + txtPaymentDue.Text + "',Remarks='" + txtCustomerName.Text + "' where Invoiceno= '" + txtInvoiceNo.Text + "'";
        cmd = new SqlCommand(cb);
        cmd.Connection = con;
        cmd.ExecuteReader();
        con.Close();

        //int i = 1;
        //ListView1.BeginUpdate();
        //ListView1.Items.Clear();
        // //var item = new  bool 
        //foreach (var  item in items )
        //{
        //    ListViewItem row = new ListViewItem(i.ToString());
        //    row.SubItems.Add(item.EventId);
        //    row.SubItems.Add(item.Name);
        //    row.SubItems.Add(item.WordPos);
        //    lstXmlItems.Items.Add(row);
        //    i++;
        //}
        //ListView1.EndUpdate();
        //ListView1.Refresh();
        //for (int i = 0; i <= ListView1.Items.Count ; i++)
        for (int i = 0; i < ListView1.Items.Count - 1; i++)
        {
            con = new SqlConnection(cs.DBConn);
            string cd = "insert Into ProductSold(InvoiceNo,ProductID,ProductName,Quantity,Price,TotalAmount) VALUES (@d1,@d2,@d3,@d4,@d5,@d6)";
            cmd = new SqlCommand(cd);
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("d1", txtInvoiceNo.Text);
            cmd.Parameters.AddWithValue("d2", ListView1.Items[i].SubItems[1].Text);
            cmd.Parameters.AddWithValue("d3", ListView1.Items[i].SubItems[2].Text);
            cmd.Parameters.AddWithValue("d4", ListView1.Items[i].SubItems[4].Text);
            cmd.Parameters.AddWithValue("d5", ListView1.Items[i].SubItems[3].Text);
            cmd.Parameters.AddWithValue("d6", ListView1.Items[i].SubItems[5].Text);
            con.Open();
            //  cmd.ExecuteNonQuery ();
            con.Close();
        }

        btnUpdate.Enabled = true;
        MessageBox.Show("Successfully updated", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}