Exemplo n.º 1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CityId < 0)
            {
                if (Classes.PubClass.CompanyId == 0)
                {
                    MessageBox.Show("请选择客户");
                    return;
                }
                else
                {
                    MessageBox.Show("请先设置该客户所在地区");
                    return;
                }
            }

            if (dgvDetails.Rows.Count < 1)
            {
                MessageBox.Show("出库内容不能为空");
                return;
            }
            try
            {
                int ret = 0;

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

                            foreach (DataGridViewRow mRow in dgvDetails.Rows)
                            {
                                ret = adapter.Insert1((long)cmbUserName.SelectedValue, Classes.PubClass.CompanyId.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), CityId, mRow.Cells["ColumnUnit"].Value.ToString(), txtStockOutNo.Text.Trim(), "正常");

                                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);
            }
        }