EDN Admin
Well-known member
Hi All,
I am trying to figure out the method to print a form created in VS2008 using C#.<br/>
My form generates a report on an inventory maintenance.<br/>
I have tried all the available options online but I have not been successful in searching.
There are some print controls such as PrintDialog, PrintPreviewControl, but I do not know how to use them, or even know if they are meant for this task. Below is my coding i hope someone can help me input the printing function.<br/>
Thanks coolness
using System;<br/>
using System.Drawing;<br/>
using System.Collections;<br/>
using System.ComponentModel;<br/>
using System.Windows.Forms;<br/>
using System.Data;<br/>
using System.Data.SqlClient;<br/>
using System.Data.OleDb;<br/>
<br/>
namespace ABSCMS<br/>
{<br/>
/// <summary><br/>
/// Summary description for w_sales_report.<br/>
/// </summary><br/>
public class w_inventory_report : System.Windows.Forms.Form<br/>
{<br/>
private System.Windows.Forms.Button btnLoad;<br/>
private System.Windows.Forms.DataGrid dataGrid1;<br/>
private System.Windows.Forms.Button button1;<br/>
private System.Windows.Forms.Button button2;<br/>
string SQLs;<br/>
/// <summary><br/>
/// Required designer variable.<br/>
/// </summary><br/>
private System.ComponentModel.Container components = null;<br/>
<br/>
public w_inventory_report()<br/>
{<br/>
//<br/>
// Required for Windows Form Designer support<br/>
//<br/>
InitializeComponent();<br/>
<br/>
//<br/>
// TODO: Add any constructor code after InitializeComponent call<br/>
//<br/>
}<br/>
<br/>
/// <summary><br/>
/// Clean up any resources being used.<br/>
/// </summary><br/>
protected override void Dispose( bool disposing )<br/>
{<br/>
if( disposing )<br/>
{<br/>
if(components != null)<br/>
{<br/>
components.Dispose();<br/>
}<br/>
}<br/>
base.Dispose( disposing );<br/>
}<br/>
<br/>
#region Windows Form Designer generated code<br/>
/// <summary><br/>
/// Required method for Designer support - do not modify<br/>
/// the contents of this method with the code editor.<br/>
/// </summary><br/>
private void InitializeComponent()<br/>
{<br/>
this.btnLoad = new System.Windows.Forms.Button();<br/>
this.dataGrid1 = new System.Windows.Forms.DataGrid();<br/>
this.button1 = new System.Windows.Forms.Button();<br/>
this.button2 = new System.Windows.Forms.Button();<br/>
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();<br/>
this.SuspendLayout();<br/>
// <br/>
// btnLoad<br/>
// <br/>
this.btnLoad.Location = new System.Drawing.Point(8, 16);<br/>
this.btnLoad.Name = "btnLoad";<br/>
this.btnLoad.Size = new System.Drawing.Size(75, 23);<br/>
this.btnLoad.TabIndex = 7;<br/>
this.btnLoad.Text = "&Load";<br/>
this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);<br/>
// <br/>
// dataGrid1<br/>
// <br/>
this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)<br/>
| System.Windows.Forms.AnchorStyles.Left)<br/>
| System.Windows.Forms.AnchorStyles.Right)));<br/>
this.dataGrid1.DataMember = "";<br/>
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;<br/>
this.dataGrid1.Location = new System.Drawing.Point(8, 48);<br/>
this.dataGrid1.Name = "dataGrid1";<br/>
this.dataGrid1.Size = new System.Drawing.Size(720, 256);<br/>
this.dataGrid1.TabIndex = 12;<br/>
// <br/>
// button1<br/>
// <br/>
this.button1.Location = new System.Drawing.Point(116, 19);<br/>
this.button1.Name = "button1";<br/>
this.button1.Size = new System.Drawing.Size(88, 23);<br/>
this.button1.TabIndex = 13;<br/>
this.button1.Text = "&Create PO";<br/>
this.button1.Click += new System.EventHandler(this.button1_Click);<br/>
// <br/>
// button2<br/>
// <br/>
this.button2.Location = new System.Drawing.Point(659, 19);<br/>
this.button2.Name = "button2";<br/>
this.button2.Size = new System.Drawing.Size(69, 23);<br/>
this.button2.TabIndex = 14;<br/>
this.button2.Text = "Clos&e";<br/>
this.button2.Click += new System.EventHandler(this.button2_Click);<br/>
// <br/>
// w_inventory_report<br/>
// <br/>
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);<br/>
this.ClientSize = new System.Drawing.Size(744, 310);<br/>
this.Controls.Add(this.button2);<br/>
this.Controls.Add(this.button1);<br/>
this.Controls.Add(this.dataGrid1);<br/>
this.Controls.Add(this.btnLoad);<br/>
this.Name = "w_inventory_report";<br/>
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;<br/>
this.Text = "Inventory Report";<br/>
this.Load += new System.EventHandler(this.w_inventory_Load);<br/>
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();<br/>
this.ResumeLayout(false);<br/>
<br/>
}<br/>
#endregion<br/>
<br/>
DataTable dt;<br/>
DataRow dr;<br/>
<br/>
private void w_inventory_Load(object sender, System.EventArgs e)<br/>
{<br/>
// constructing a data table<br/>
dt = new DataTable("Transactions");<br/>
dt.Columns.Add("Item Code",typeof(string));<br/>
dt.Columns[0].ReadOnly = true;<br/>
dt.Columns.Add("Supplier ID",typeof(string));<br/>
dt.Columns[1].ReadOnly = true;<br/>
dt.Columns.Add("Qty",typeof(double));<br/>
dt.Columns[2].ReadOnly = true;<br/>
dt.Columns.Add("Price",typeof(double));<br/>
dt.Columns[3].ReadOnly = true;<br/>
dt.Columns.Add("On Order Qty",typeof(double));<br/>
dt.Columns[4].ReadOnly = true;<br/>
dt.Columns.Add("ROP",typeof(double));<br/>
dt.Columns[5].ReadOnly = true;<br/>
dt.Columns.Add("Item Location",typeof(string));<br/>
dt.Columns[6].ReadOnly = true;<br/>
dt.Columns.Add("Consumption Status",typeof(string));<br/>
dt.Columns[7].ReadOnly = true;<br/>
dt.Columns.Add("Date",typeof(System.DateTime));<br/>
dt.Columns[8].ReadOnly = true;<br/>
}<br/>
<br/>
public DataSet get_data()<br/>
{<br/>
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +<br/>
"Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString()
+ "\POS_DB.mdb";<br/>
OleDbConnection oConn = new OleDbConnection(strConn);<br/>
oConn.Open();<br/>
<br/>
SQLs = "SELECT * FROM inventory ";<br/>
<br/>
OleDbCommand cmd = new OleDbCommand(SQLs);<br/>
cmd.Connection = oConn;<br/>
OleDbDataReader rdr = cmd.ExecuteReader();<br/>
<br/>
while (rdr.Read())<br/>
{<br/>
dr = dt.NewRow();<br/>
<br/>
dr["Item Code"] = rdr.GetValue(0).ToString();<br/>
dr["Supplier ID"] = rdr.GetValue(1).ToString();<br/>
dr["Qty"] = Convert.ToDouble(rdr.GetValue(2));<br/>
dr["Price"] = Convert.ToDouble(rdr.GetValue(3));<br/>
dr["On Order Qty"] = Convert.ToDouble(rdr.GetValue(4));<br/>
dr["ROP"] = Convert.ToDouble(rdr.GetValue(5));<br/>
dr["Item Location"] = rdr.GetValue(6).ToString();<br/>
dr["Consumption Status"] = rdr.GetValue(7).ToString();<br/>
dr["Date"] = Convert.ToDateTime(rdr.GetValue(8));<br/>
dt.Rows.Add(dr);<br/>
}<br/>
<br/>
DataTable tempT;<br/>
tempT = dt;<br/>
dataGrid1.DataSource = tempT; <br/>
<br/>
rdr.Close();<br/>
oConn.Close();<br/>
<br/>
return dt.DataSet;<br/>
}<br/>
<br/>
private void btnLoad_Click(object sender, System.EventArgs e)<br/>
{<br/>
/*<br/>
int a;<br/>
InventoryAgent.InventoryAgent IA = new InventoryAgent.InventoryAgent();<br/>
a = IA.GetPOID();<br/>
MessageBox.Show("Value = " + a.ToString());<br/>
*/<br/>
<br/>
//Clear the records whenever re-loading.<br/>
dt.Clear();<br/>
<br/>
try<br/>
{<br/>
get_data();<br/>
}<br/>
catch (System.Exception eMsg)<br/>
{<br/>
MessageBox.Show(eMsg.Message);<br/>
}<br/>
}<br/>
<br/>
private void button1_Click(object sender, System.EventArgs e)<br/>
{<br/>
DataTable dTab;<br/>
DataRow dRow;<br/>
<br/>
dTab = new DataTable("PORecords");<br/>
dTab.Columns.Add("Item Code",typeof(string));<br/>
dTab.Columns[0].ReadOnly = true;<br/>
dTab.Columns.Add("Supplier ID",typeof(string));<br/>
dTab.Columns[1].ReadOnly = true;<br/>
dTab.Columns.Add("Qty",typeof(double));<br/>
dTab.Columns[2].ReadOnly = true;<br/>
dTab.Columns.Add("On Order Qty",typeof(double));<br/>
dTab.Columns[3].ReadOnly = true;<br/>
dTab.Columns.Add("ROP",typeof(double));<br/>
dTab.Columns[4].ReadOnly = true;<br/>
dTab.Columns.Add("Consumption Status",typeof(string));<br/>
dTab.Columns[5].ReadOnly = true;<br/>
<br/>
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +<br/>
"Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString()
+ "\POS_DB.mdb";<br/>
OleDbConnection oConn = new OleDbConnection(strConn);<br/>
oConn.Open();<br/>
<br/>
SQLs = "SELECT DISTINCT item_code, supplier_id, qty, on_order_qty, re_order_point, high_consumption FROM inventory ";<br/>
<br/>
OleDbCommand cmd = new OleDbCommand(SQLs);<br/>
cmd.Connection = oConn;<br/>
OleDbDataReader rdr = cmd.ExecuteReader();<br/>
<br/>
<br/>
<br/>
while (rdr.Read())<br/>
{<br/>
dRow = dTab.NewRow();<br/>
dRow["Item Code"] = rdr.GetValue(0).ToString();<br/>
dRow["Supplier ID"] = rdr.GetValue(1).ToString();<br/>
dRow["Qty"] = Convert.ToInt32(rdr.GetValue(2));<br/>
dRow["On Order Qty"] = Convert.ToInt32(rdr.GetValue(3));<br/>
dRow["ROP"] = Convert.ToInt32(rdr.GetValue(4));<br/>
dRow["Consumption Status"] = rdr.GetValue(5).ToString();<br/>
dTab.Rows.Add(dRow);<br/>
}<br/>
<br/>
rdr.Close();<br/>
oConn.Close();<br/>
<br/>
int i, cnt;<br/>
int qty1, qty2, rop;<br/>
string item_code, supplier_id, consump_status;<br/>
<br/>
cnt = Convert.ToInt32(dTab.Rows.Count);<br/>
<br/>
for (i=0; i<cnt; i++)<br/>
{ <br/>
item_code = dTab.Rows.ItemArray[0].ToString();<br/>
supplier_id = dTab.Rows.ItemArray[1].ToString();<br/>
qty1 = Convert.ToInt32(dTab.Rows.ItemArray[2]);<br/>
qty2 = Convert.ToInt32(dTab.Rows.ItemArray[3]);<br/>
rop = Convert.ToInt32(dTab.Rows.ItemArray[4]);<br/>
consump_status = dTab.Rows.ItemArray[5].ToString();<br/>
<br/>
if ((rop > (qty1+qty2))&& (consump_status.Equals("Yes")))<br/>
{<br/>
//PO table: po_id, item_code, order_qty, supplier_id, order_status<br/>
Create_PO(item_code, rop*2, supplier_id);<br/>
Update_Inv(item_code, rop*2);<br/>
<br/>
//Check the its associated item<br/>
//if found, create po for that item as well<br/>
string AssocItem = Check_AssocItem(item_code);<br/>
if (AssocItem != "")<br/>
Create_PO(AssocItem, rop*2, supplier_id);<br/>
<br/>
MessageBox.Show("PO for " + item_code + " created!");<br/>
}<br/>
else if ((rop > (qty1 + qty2)) && (consump_status.Equals ("No")))<br/>
{<br/>
//PO table: po_id, item_code, order_qty, supplier_id, order_status<br/>
Create_PO(item_code, rop, supplier_id);<br/>
Update_Inv(item_code, rop);<br/>
<br/>
//Check the its associated item<br/>
//if found, create po for that item as well<br/>
string AssocItem = Check_AssocItem(item_code);<br/>
if (AssocItem != "")<br/>
Create_PO(AssocItem, rop, supplier_id);<br/>
<br/>
MessageBox.Show("PO for " + item_code + " created!");<br/>
}<br/>
}<br/>
<br/>
<br/>
}<br/>
<br/>
private string Check_AssocItem(string item_code)<br/>
{<br/>
string item, retItem;<br/>
double support;<br/>
retItem = "";<br/>
<br/>
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +<br/>
"Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString()
+ "\POS_DB.mdb";<br/>
OleDbConnection oConn = new OleDbConnection(strConn);<br/>
oConn.Open();<br/>
<br/>
SQLs = "SELECT consequent, support FROM assocItems WHERE antecedent = " + item_code + "";<br/>
<br/>
OleDbCommand cmd = new OleDbCommand(SQLs);<br/>
cmd.Connection = oConn;<br/>
OleDbDataReader rdr = cmd.ExecuteReader();<br/>
<br/>
<br/>
<br/>
if (rdr.Read() == true)<br/>
{<br/>
item = rdr.GetValue(0).ToString();<br/>
support = Convert.ToInt32(rdr.GetValue(1));<br/>
if (support*100 > 70)<br/>
retItem = item;<br/>
}<br/>
<br/>
rdr.Close();<br/>
oConn.Close();<br/>
<br/>
return retItem; <br/>
}<br/>
<br/>
private void Create_PO(string Item_Code, int ROP, string Supplier_ID)<br/>
{<br/>
<br/>
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +<br/>
"Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString()
+ "\POS_DB.mdb";<br/>
OleDbConnection oConn = new OleDbConnection(strConn);<br/>
oConn.Open();<br/>
<br/>
OleDbCommand SQL = new OleDbCommand("INSERT INTO PO (PO_ID, Item_Code, Order_Qty, Supplier_ID, Order_Status, Create_Date) VALUES (@PO_ID, @Item_Code, @Order_Qty, @Supplier_ID, @Order_Status,
" + System.DateTime.Now + ")");<br/>
SQL.Parameters.Add("@PO_ID", Convert.ToString(GetPOID()));<br/>
SQL.Parameters.Add("@Item_Code", Item_Code);<br/>
SQL.Parameters.Add("@Order_Qty",Convert.ToInt32(ROP));<br/>
SQL.Parameters.Add("@Supplier_ID", Supplier_ID);<br/>
SQL.Parameters.Add("@Order_Status","New"); <br/>
//SQL.Parameters.Add("@Create_Date",System.DateTime.Now);<br/>
<br/>
SQL.Connection = oConn;<br/>
SQL.ExecuteNonQuery();<br/>
oConn.Close();<br/>
}<br/>
<br/>
private void Update_Inv(string Item_Code, int ROP)<br/>
{<br/>
<br/>
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +<br/>
"Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString()
+ "\POS_DB.mdb";<br/>
OleDbConnection oConn = new OleDbConnection(strConn);<br/>
oConn.Open();<br/>
<br/>
<br/>
OleDbCommand SQL = new OleDbCommand("UPDATE inventory SET on_order_qty = " + @ROP + " WHERE item_code = " + @Item_Code + "");<br/>
SQL.Parameters.Add("@ROP", Convert.ToInt32(ROP));<br/>
SQL.Parameters.Add("@Item_Code", Item_Code);<br/>
<br/>
SQL.Connection = oConn;<br/>
SQL.ExecuteNonQuery();<br/>
oConn.Close();<br/>
}<br/>
<br/>
private int GetPOID()<br/>
{<br/>
int NewPOID;<br/>
<br/>
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +<br/>
"Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString()
+ "\POS_DB.mdb";<br/>
OleDbConnection oConn = new OleDbConnection(strConn);<br/>
oConn.Open();<br/>
<br/>
SQLs = "SELECT MAX(PO_ID) FROM PO ";<br/>
<br/>
OleDbCommand cmd = new OleDbCommand(SQLs);<br/>
cmd.Connection = oConn;<br/>
OleDbDataReader rdr = cmd.ExecuteReader();<br/>
<br/>
<br/>
<br/>
rdr.Read();<br/>
if (rdr.GetValue(0).ToString() == "")<br/>
NewPOID = 1;<br/>
else <br/>
NewPOID = Convert.ToInt32(rdr.GetValue(0)) + 1;<br/>
rdr.Close();<br/>
oConn.Close();<br/>
<br/>
return NewPOID; <br/>
}<br/>
private void button2_Click(object sender, System.EventArgs e)<br/>
{<br/>
this.Close();<br/>
}<br/>
<br/>
<br/>
<br/>
<br/>
}<br/>
}
View the full article
I am trying to figure out the method to print a form created in VS2008 using C#.<br/>
My form generates a report on an inventory maintenance.<br/>
I have tried all the available options online but I have not been successful in searching.
There are some print controls such as PrintDialog, PrintPreviewControl, but I do not know how to use them, or even know if they are meant for this task. Below is my coding i hope someone can help me input the printing function.<br/>
Thanks coolness
using System;<br/>
using System.Drawing;<br/>
using System.Collections;<br/>
using System.ComponentModel;<br/>
using System.Windows.Forms;<br/>
using System.Data;<br/>
using System.Data.SqlClient;<br/>
using System.Data.OleDb;<br/>
<br/>
namespace ABSCMS<br/>
{<br/>
/// <summary><br/>
/// Summary description for w_sales_report.<br/>
/// </summary><br/>
public class w_inventory_report : System.Windows.Forms.Form<br/>
{<br/>
private System.Windows.Forms.Button btnLoad;<br/>
private System.Windows.Forms.DataGrid dataGrid1;<br/>
private System.Windows.Forms.Button button1;<br/>
private System.Windows.Forms.Button button2;<br/>
string SQLs;<br/>
/// <summary><br/>
/// Required designer variable.<br/>
/// </summary><br/>
private System.ComponentModel.Container components = null;<br/>
<br/>
public w_inventory_report()<br/>
{<br/>
//<br/>
// Required for Windows Form Designer support<br/>
//<br/>
InitializeComponent();<br/>
<br/>
//<br/>
// TODO: Add any constructor code after InitializeComponent call<br/>
//<br/>
}<br/>
<br/>
/// <summary><br/>
/// Clean up any resources being used.<br/>
/// </summary><br/>
protected override void Dispose( bool disposing )<br/>
{<br/>
if( disposing )<br/>
{<br/>
if(components != null)<br/>
{<br/>
components.Dispose();<br/>
}<br/>
}<br/>
base.Dispose( disposing );<br/>
}<br/>
<br/>
#region Windows Form Designer generated code<br/>
/// <summary><br/>
/// Required method for Designer support - do not modify<br/>
/// the contents of this method with the code editor.<br/>
/// </summary><br/>
private void InitializeComponent()<br/>
{<br/>
this.btnLoad = new System.Windows.Forms.Button();<br/>
this.dataGrid1 = new System.Windows.Forms.DataGrid();<br/>
this.button1 = new System.Windows.Forms.Button();<br/>
this.button2 = new System.Windows.Forms.Button();<br/>
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();<br/>
this.SuspendLayout();<br/>
// <br/>
// btnLoad<br/>
// <br/>
this.btnLoad.Location = new System.Drawing.Point(8, 16);<br/>
this.btnLoad.Name = "btnLoad";<br/>
this.btnLoad.Size = new System.Drawing.Size(75, 23);<br/>
this.btnLoad.TabIndex = 7;<br/>
this.btnLoad.Text = "&Load";<br/>
this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);<br/>
// <br/>
// dataGrid1<br/>
// <br/>
this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)<br/>
| System.Windows.Forms.AnchorStyles.Left)<br/>
| System.Windows.Forms.AnchorStyles.Right)));<br/>
this.dataGrid1.DataMember = "";<br/>
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;<br/>
this.dataGrid1.Location = new System.Drawing.Point(8, 48);<br/>
this.dataGrid1.Name = "dataGrid1";<br/>
this.dataGrid1.Size = new System.Drawing.Size(720, 256);<br/>
this.dataGrid1.TabIndex = 12;<br/>
// <br/>
// button1<br/>
// <br/>
this.button1.Location = new System.Drawing.Point(116, 19);<br/>
this.button1.Name = "button1";<br/>
this.button1.Size = new System.Drawing.Size(88, 23);<br/>
this.button1.TabIndex = 13;<br/>
this.button1.Text = "&Create PO";<br/>
this.button1.Click += new System.EventHandler(this.button1_Click);<br/>
// <br/>
// button2<br/>
// <br/>
this.button2.Location = new System.Drawing.Point(659, 19);<br/>
this.button2.Name = "button2";<br/>
this.button2.Size = new System.Drawing.Size(69, 23);<br/>
this.button2.TabIndex = 14;<br/>
this.button2.Text = "Clos&e";<br/>
this.button2.Click += new System.EventHandler(this.button2_Click);<br/>
// <br/>
// w_inventory_report<br/>
// <br/>
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);<br/>
this.ClientSize = new System.Drawing.Size(744, 310);<br/>
this.Controls.Add(this.button2);<br/>
this.Controls.Add(this.button1);<br/>
this.Controls.Add(this.dataGrid1);<br/>
this.Controls.Add(this.btnLoad);<br/>
this.Name = "w_inventory_report";<br/>
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;<br/>
this.Text = "Inventory Report";<br/>
this.Load += new System.EventHandler(this.w_inventory_Load);<br/>
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();<br/>
this.ResumeLayout(false);<br/>
<br/>
}<br/>
#endregion<br/>
<br/>
DataTable dt;<br/>
DataRow dr;<br/>
<br/>
private void w_inventory_Load(object sender, System.EventArgs e)<br/>
{<br/>
// constructing a data table<br/>
dt = new DataTable("Transactions");<br/>
dt.Columns.Add("Item Code",typeof(string));<br/>
dt.Columns[0].ReadOnly = true;<br/>
dt.Columns.Add("Supplier ID",typeof(string));<br/>
dt.Columns[1].ReadOnly = true;<br/>
dt.Columns.Add("Qty",typeof(double));<br/>
dt.Columns[2].ReadOnly = true;<br/>
dt.Columns.Add("Price",typeof(double));<br/>
dt.Columns[3].ReadOnly = true;<br/>
dt.Columns.Add("On Order Qty",typeof(double));<br/>
dt.Columns[4].ReadOnly = true;<br/>
dt.Columns.Add("ROP",typeof(double));<br/>
dt.Columns[5].ReadOnly = true;<br/>
dt.Columns.Add("Item Location",typeof(string));<br/>
dt.Columns[6].ReadOnly = true;<br/>
dt.Columns.Add("Consumption Status",typeof(string));<br/>
dt.Columns[7].ReadOnly = true;<br/>
dt.Columns.Add("Date",typeof(System.DateTime));<br/>
dt.Columns[8].ReadOnly = true;<br/>
}<br/>
<br/>
public DataSet get_data()<br/>
{<br/>
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +<br/>
"Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString()
+ "\POS_DB.mdb";<br/>
OleDbConnection oConn = new OleDbConnection(strConn);<br/>
oConn.Open();<br/>
<br/>
SQLs = "SELECT * FROM inventory ";<br/>
<br/>
OleDbCommand cmd = new OleDbCommand(SQLs);<br/>
cmd.Connection = oConn;<br/>
OleDbDataReader rdr = cmd.ExecuteReader();<br/>
<br/>
while (rdr.Read())<br/>
{<br/>
dr = dt.NewRow();<br/>
<br/>
dr["Item Code"] = rdr.GetValue(0).ToString();<br/>
dr["Supplier ID"] = rdr.GetValue(1).ToString();<br/>
dr["Qty"] = Convert.ToDouble(rdr.GetValue(2));<br/>
dr["Price"] = Convert.ToDouble(rdr.GetValue(3));<br/>
dr["On Order Qty"] = Convert.ToDouble(rdr.GetValue(4));<br/>
dr["ROP"] = Convert.ToDouble(rdr.GetValue(5));<br/>
dr["Item Location"] = rdr.GetValue(6).ToString();<br/>
dr["Consumption Status"] = rdr.GetValue(7).ToString();<br/>
dr["Date"] = Convert.ToDateTime(rdr.GetValue(8));<br/>
dt.Rows.Add(dr);<br/>
}<br/>
<br/>
DataTable tempT;<br/>
tempT = dt;<br/>
dataGrid1.DataSource = tempT; <br/>
<br/>
rdr.Close();<br/>
oConn.Close();<br/>
<br/>
return dt.DataSet;<br/>
}<br/>
<br/>
private void btnLoad_Click(object sender, System.EventArgs e)<br/>
{<br/>
/*<br/>
int a;<br/>
InventoryAgent.InventoryAgent IA = new InventoryAgent.InventoryAgent();<br/>
a = IA.GetPOID();<br/>
MessageBox.Show("Value = " + a.ToString());<br/>
*/<br/>
<br/>
//Clear the records whenever re-loading.<br/>
dt.Clear();<br/>
<br/>
try<br/>
{<br/>
get_data();<br/>
}<br/>
catch (System.Exception eMsg)<br/>
{<br/>
MessageBox.Show(eMsg.Message);<br/>
}<br/>
}<br/>
<br/>
private void button1_Click(object sender, System.EventArgs e)<br/>
{<br/>
DataTable dTab;<br/>
DataRow dRow;<br/>
<br/>
dTab = new DataTable("PORecords");<br/>
dTab.Columns.Add("Item Code",typeof(string));<br/>
dTab.Columns[0].ReadOnly = true;<br/>
dTab.Columns.Add("Supplier ID",typeof(string));<br/>
dTab.Columns[1].ReadOnly = true;<br/>
dTab.Columns.Add("Qty",typeof(double));<br/>
dTab.Columns[2].ReadOnly = true;<br/>
dTab.Columns.Add("On Order Qty",typeof(double));<br/>
dTab.Columns[3].ReadOnly = true;<br/>
dTab.Columns.Add("ROP",typeof(double));<br/>
dTab.Columns[4].ReadOnly = true;<br/>
dTab.Columns.Add("Consumption Status",typeof(string));<br/>
dTab.Columns[5].ReadOnly = true;<br/>
<br/>
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +<br/>
"Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString()
+ "\POS_DB.mdb";<br/>
OleDbConnection oConn = new OleDbConnection(strConn);<br/>
oConn.Open();<br/>
<br/>
SQLs = "SELECT DISTINCT item_code, supplier_id, qty, on_order_qty, re_order_point, high_consumption FROM inventory ";<br/>
<br/>
OleDbCommand cmd = new OleDbCommand(SQLs);<br/>
cmd.Connection = oConn;<br/>
OleDbDataReader rdr = cmd.ExecuteReader();<br/>
<br/>
<br/>
<br/>
while (rdr.Read())<br/>
{<br/>
dRow = dTab.NewRow();<br/>
dRow["Item Code"] = rdr.GetValue(0).ToString();<br/>
dRow["Supplier ID"] = rdr.GetValue(1).ToString();<br/>
dRow["Qty"] = Convert.ToInt32(rdr.GetValue(2));<br/>
dRow["On Order Qty"] = Convert.ToInt32(rdr.GetValue(3));<br/>
dRow["ROP"] = Convert.ToInt32(rdr.GetValue(4));<br/>
dRow["Consumption Status"] = rdr.GetValue(5).ToString();<br/>
dTab.Rows.Add(dRow);<br/>
}<br/>
<br/>
rdr.Close();<br/>
oConn.Close();<br/>
<br/>
int i, cnt;<br/>
int qty1, qty2, rop;<br/>
string item_code, supplier_id, consump_status;<br/>
<br/>
cnt = Convert.ToInt32(dTab.Rows.Count);<br/>
<br/>
for (i=0; i<cnt; i++)<br/>
{ <br/>
item_code = dTab.Rows.ItemArray[0].ToString();<br/>
supplier_id = dTab.Rows.ItemArray[1].ToString();<br/>
qty1 = Convert.ToInt32(dTab.Rows.ItemArray[2]);<br/>
qty2 = Convert.ToInt32(dTab.Rows.ItemArray[3]);<br/>
rop = Convert.ToInt32(dTab.Rows.ItemArray[4]);<br/>
consump_status = dTab.Rows.ItemArray[5].ToString();<br/>
<br/>
if ((rop > (qty1+qty2))&& (consump_status.Equals("Yes")))<br/>
{<br/>
//PO table: po_id, item_code, order_qty, supplier_id, order_status<br/>
Create_PO(item_code, rop*2, supplier_id);<br/>
Update_Inv(item_code, rop*2);<br/>
<br/>
//Check the its associated item<br/>
//if found, create po for that item as well<br/>
string AssocItem = Check_AssocItem(item_code);<br/>
if (AssocItem != "")<br/>
Create_PO(AssocItem, rop*2, supplier_id);<br/>
<br/>
MessageBox.Show("PO for " + item_code + " created!");<br/>
}<br/>
else if ((rop > (qty1 + qty2)) && (consump_status.Equals ("No")))<br/>
{<br/>
//PO table: po_id, item_code, order_qty, supplier_id, order_status<br/>
Create_PO(item_code, rop, supplier_id);<br/>
Update_Inv(item_code, rop);<br/>
<br/>
//Check the its associated item<br/>
//if found, create po for that item as well<br/>
string AssocItem = Check_AssocItem(item_code);<br/>
if (AssocItem != "")<br/>
Create_PO(AssocItem, rop, supplier_id);<br/>
<br/>
MessageBox.Show("PO for " + item_code + " created!");<br/>
}<br/>
}<br/>
<br/>
<br/>
}<br/>
<br/>
private string Check_AssocItem(string item_code)<br/>
{<br/>
string item, retItem;<br/>
double support;<br/>
retItem = "";<br/>
<br/>
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +<br/>
"Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString()
+ "\POS_DB.mdb";<br/>
OleDbConnection oConn = new OleDbConnection(strConn);<br/>
oConn.Open();<br/>
<br/>
SQLs = "SELECT consequent, support FROM assocItems WHERE antecedent = " + item_code + "";<br/>
<br/>
OleDbCommand cmd = new OleDbCommand(SQLs);<br/>
cmd.Connection = oConn;<br/>
OleDbDataReader rdr = cmd.ExecuteReader();<br/>
<br/>
<br/>
<br/>
if (rdr.Read() == true)<br/>
{<br/>
item = rdr.GetValue(0).ToString();<br/>
support = Convert.ToInt32(rdr.GetValue(1));<br/>
if (support*100 > 70)<br/>
retItem = item;<br/>
}<br/>
<br/>
rdr.Close();<br/>
oConn.Close();<br/>
<br/>
return retItem; <br/>
}<br/>
<br/>
private void Create_PO(string Item_Code, int ROP, string Supplier_ID)<br/>
{<br/>
<br/>
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +<br/>
"Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString()
+ "\POS_DB.mdb";<br/>
OleDbConnection oConn = new OleDbConnection(strConn);<br/>
oConn.Open();<br/>
<br/>
OleDbCommand SQL = new OleDbCommand("INSERT INTO PO (PO_ID, Item_Code, Order_Qty, Supplier_ID, Order_Status, Create_Date) VALUES (@PO_ID, @Item_Code, @Order_Qty, @Supplier_ID, @Order_Status,
" + System.DateTime.Now + ")");<br/>
SQL.Parameters.Add("@PO_ID", Convert.ToString(GetPOID()));<br/>
SQL.Parameters.Add("@Item_Code", Item_Code);<br/>
SQL.Parameters.Add("@Order_Qty",Convert.ToInt32(ROP));<br/>
SQL.Parameters.Add("@Supplier_ID", Supplier_ID);<br/>
SQL.Parameters.Add("@Order_Status","New"); <br/>
//SQL.Parameters.Add("@Create_Date",System.DateTime.Now);<br/>
<br/>
SQL.Connection = oConn;<br/>
SQL.ExecuteNonQuery();<br/>
oConn.Close();<br/>
}<br/>
<br/>
private void Update_Inv(string Item_Code, int ROP)<br/>
{<br/>
<br/>
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +<br/>
"Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString()
+ "\POS_DB.mdb";<br/>
OleDbConnection oConn = new OleDbConnection(strConn);<br/>
oConn.Open();<br/>
<br/>
<br/>
OleDbCommand SQL = new OleDbCommand("UPDATE inventory SET on_order_qty = " + @ROP + " WHERE item_code = " + @Item_Code + "");<br/>
SQL.Parameters.Add("@ROP", Convert.ToInt32(ROP));<br/>
SQL.Parameters.Add("@Item_Code", Item_Code);<br/>
<br/>
SQL.Connection = oConn;<br/>
SQL.ExecuteNonQuery();<br/>
oConn.Close();<br/>
}<br/>
<br/>
private int GetPOID()<br/>
{<br/>
int NewPOID;<br/>
<br/>
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +<br/>
"Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString()
+ "\POS_DB.mdb";<br/>
OleDbConnection oConn = new OleDbConnection(strConn);<br/>
oConn.Open();<br/>
<br/>
SQLs = "SELECT MAX(PO_ID) FROM PO ";<br/>
<br/>
OleDbCommand cmd = new OleDbCommand(SQLs);<br/>
cmd.Connection = oConn;<br/>
OleDbDataReader rdr = cmd.ExecuteReader();<br/>
<br/>
<br/>
<br/>
rdr.Read();<br/>
if (rdr.GetValue(0).ToString() == "")<br/>
NewPOID = 1;<br/>
else <br/>
NewPOID = Convert.ToInt32(rdr.GetValue(0)) + 1;<br/>
rdr.Close();<br/>
oConn.Close();<br/>
<br/>
return NewPOID; <br/>
}<br/>
private void button2_Click(object sender, System.EventArgs e)<br/>
{<br/>
this.Close();<br/>
}<br/>
<br/>
<br/>
<br/>
<br/>
}<br/>
}
View the full article