Exemple #1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CityId < 0)
            {
                if (cmbCustomerName.SelectedIndex == -1)
                {
                    MessageBox.Show("请选择客户");
                    return;
                }
                else
                {
                    MessageBox.Show("请先设置该客户所在地区");
                    return;
                }
            }

            if (cmbSettlementModes.Text.Trim() == "")
            {
                MessageBox.Show("请输入结款方式");
                return;
            }
            if (txtStockOutNo.Text.Trim() == "")
            {
                MessageBox.Show("出库单编号不能为空");
                return;
            }
            if (dgvDetails.Rows.Count < 1)
            {
                MessageBox.Show("出库内容不能为空");
                return;
            }
            try
            {
                int ret = 0;
                //if (mId == -1)
                //{
                //int ret = new T_SaleDetailsTableAdapter().Insert((long)cmbUserName.SelectedValue, cmbCustomerName.Text.Trim(), "", dtpDate1.Value.Date, ProductId, Convert.ToDecimal(txtAmount.Text), Convert.ToDecimal(txtPrice.Text), Convert.ToDecimal(txtSum.Text), cmbSettlementModes.Text, null, makeInvoice, txtInvoiceContent.Text.Trim(), "", dtpInvioceDate.Value.Date, cmbInvoiceType.Text, txtInvoiceNo.Text.Trim(), (cmbCustomerName.SelectedValue.ToString() == "" ? -1 : (long)cmbCustomerName.SelectedValue));

                using (SqlConnection mconn = new SqlConnection(Common.CommonClass.SqlConnStr))
                {
                    mconn.Open();
                    using (SqlTransaction mTrans = mconn.BeginTransaction())
                    {
                        try
                        {
                            var adapter = new T_SaleDetailsTableAdapter();
                            adapter.Connection  = mconn;
                            adapter.Transaction = mTrans;

                            foreach (DataGridViewRow mRow in dgvDetails.Rows)
                            {
                                ret = adapter.Insert((long)cmbUserName.SelectedValue, cmbCustomerName.SelectedValue.ToString(), "", dtpDate1.Value.Date, Convert.ToInt64(mRow.Cells["ColumnProductId"].Value), Convert.ToDecimal(mRow.Cells["ColumnAmount"].Value), Convert.ToDecimal(mRow.Cells["ColumnPrice"].Value), Convert.ToDecimal(mRow.Cells["ColumnSumMoney"].Value), cmbSettlementModes.Text, CityId, null, txtStockOutNo.Text.Trim(), mRow.Cells["InvoiceFlag"].Value.ToString(), mRow.Cells["ColumnUnit"].Value.ToString());
                                if (ret == 0)
                                {
                                    MessageBox.Show("第" + mRow.Index.ToString() + "项插入失败");
                                    mTrans.Rollback();
                                    return;
                                }
                            }
                            mTrans.Commit();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                            return;
                        }
                    }
                }

                MessageBox.Show("保存成功");
                btnSave.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }