Last login 2018-7-9
Online Time 3 hours
Prestige 2 points
Points 0 points
Background code: ///
/// Query, return datatable ///
/// Parameters/// sql statement/// database information///DataTable
public static DataTable SelectDataTable(DataTable param, string sql, string info) { DataTable dt = new DataTable("result"); SqlConnection conn = OpenCloseDB.OpenDatabase(info); try { SqlDataAdapter adapter = new SqlDataAdapter(sql, conn); adapter .SelectCommand.Parameters.Clear(); if (param != null) { foreach (DataRow dic in param.Rows) { adapter.SelectCommand.Parameters.AddWithValue(Dtv(dic, "name", string.Empty), Dtv(dic, "value", string.Empty)); } } adapter.Fill( dt); adapter = null; } catch (Exception ex) { Common.WriteLog(ex, "query return form", ""); return dt; } finally { OpenCloseDB.CloseDatabase(conn); } return dt; } Front-end code: private void Query() { string billno = this.txt_Billno.Text.Trim(); string sql = "select ic.FNumber , CEILING(ie.FAuxQtyMust) as FAuxQtyMust , CEILING(ie.FAuxQty) as FAuxQty , case ie.FAuxQtyMust when 0 then 0 else (ie.FQtyMust /ie.FAuxQtyMust) end as fsecqty, ic.FModel , tp.FName as stockplace " + " ,ib.FStatus, ie.fentryid, ib.finterid from ICStockBill ib inner join ICStockBillEntry ie on ib.FInterID = ie.FInterID " + " inner join t_ICItem ic on ie.FItemID = ic.FItemID " + " inner join t_StockPlace tp on ie.FDCSPID = tp.FSPID " + " where ib.FBillNo like '%" + billno + "%' and ib.fbillno like 'SOUT%' "; try { DataTable t = WebRef.obj.SelectDataTable(null, sql, login.conn); detail.Clear(); if (t != null && t.Rows.Count > 0) { if (t.Rows[0]["FStatus"].ToString() == "1") { MessageBox.Show("Document has been approved"); this.txt_Billno.Text = ""; return; } foreach (DataRow dd in t.Rows) { DataRow dr = this.detail.NewRow(); dr["fentryid"] = dd["fentryid"]; dr["FQty"] = dd["FAuxQty"]; dr["FAuxQty"] =0; dr["FSecQty"] = dd["fsecqty"]; dr["FModel"] = dd["FModel"].ToString(); dr["stockplace"] = dd["stockplace"].ToString(); dr["FNumber"] = dd["FNumber"].ToString(); dr["finterid"] = dd["finterid"]; this.detail.Rows.Add(dr); this.detail .AcceptChanges(); } sql = "select yz.fmodel, count(1) as num from yzbarcode_tmp yt inner join yz_icitem yz on yt.bptype = yz.bptype where chukubillno = '" + billno + "' and isvalid = 1 group by yz.fmodel "; DataTable bindingDT = WebRef.obj.SelectDataTable(null, sql, login.conn); if (bindingDT != null || bindingDT.Rows.Count > 0) { foreach(DataRow r in bindingDT.Rows) { int num = (int)r["num"]; DataRow[] drs = detail.Select("FModel = '" + r["fmodel"].ToString() + "'"); if (drs != null && drs. Length > 0) { foreach (DataRow rr in drs) { if (num > 0) { int qty = (int)rr["FQty"]; if (num > qty) { rr["FAuxQty"] = qty; num -= qty; } else { rr["FAuxQty"] = num; num = 0; } } } } } this.detail.AcceptChanges(); } } scBillno = billno; } catch (Exception ex) { MessageBox.Show("Exception: " + ex.ToString()); return; } }conn); if (bindingDT != null || bindingDT.Rows.Count > 0) { foreach(DataRow r in bindingDT.Rows) { int num = (int)r["num"]; DataRow[] drs = detail. Select("FModel = '" + r["fmodel"].ToString() + "'"); if (drs != null && drs.Length > 0) { foreach (DataRow rr in drs) { if (num > 0) { int qty = (int)rr["FQty"]; if (num > qty) { rr["FAuxQty"] = qty; num -= qty; } else { rr["FAuxQty"] = num; num = 0; } } } } } this.detail.AcceptChanges(); } } scBillno = billno; } catch (Exception ex) { MessageBox.Show("Exception: " + ex.ToString()); return; } }conn); if (bindingDT != null || bindingDT.Rows.Count > 0) { foreach(DataRow r in bindingDT.Rows) { int num = (int)r["num"]; DataRow[] drs = detail. Select("FModel = '" + r["fmodel"].ToString() + "'"); if (drs != null && drs.Length > 0) { foreach (DataRow rr in drs) { if (num > 0) { int qty = (int)rr["FQty"]; if (num > qty) { rr["FAuxQty"] = qty; num -= qty; } else { rr["FAuxQty"] = num; num = 0; } } } } } this.detail.AcceptChanges(); } } scBillno = billno; } catch (Exception ex) { MessageBox.Show("Exception: " + ex.ToString()); return; } }Length > 0) { foreach (DataRow rr in drs) { if (num > 0) { int qty = (int)rr["FQty"]; if (num > qty) { rr["FAuxQty"] = qty; num -= qty; } else { rr["FAuxQty"] = num; num = 0; } } } } } this.detail.AcceptChanges(); } } scBillno = billno; } catch (Exception ex) { MessageBox.Show("Exception: " + ex.ToString()); return; } }Length > 0) { foreach (DataRow rr in drs) { if (num > 0) { int qty = (int)rr["FQty"]; if (num > qty) { rr["FAuxQty"] = qty; num -= qty; } else { rr["FAuxQty"] = num; num = 0; } } } } } this.detail.AcceptChanges(); } } scBillno = billno; } catch (Exception ex) { MessageBox.Show("Exception: " + ex.ToString()); return; } }