Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Articles » .NET Framework »
COM+ and .NET (Part 3) – Creating UI that uses COM+ transaction
|
Introduction This part of the article explains you how to access the transactional component that is described in the previous part.
Create a new Windows Application in C#. Add a reference to “TransServer.Dll”. Copy the code below to design the form.
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms;
namespace QtyCheck { // This class defines the UI for the COM+ Transaction
public class QtyForm : System.Windows.Forms.Form { private System.Windows.Forms.Label currentqtyLabel; private System.Windows.Forms.TextBox currentqty; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label currqtylabel; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.TextBox txtProduct; private System.Windows.Forms.TextBox txtnewQty; private System.Windows.Forms.Label label2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Label label3;
private System.ComponentModel.Container components = null; public QtyForm() { // Required for Windows Form Designer support // InitializeComponent(); }
protected override void OnLoad(EventArgs e) { try {
The 'using' statement is used to Dispose the object in use on exiting the curly braces. Disposing COM+ objects are significant, as COM+ metadata such as context does not remain in memory unnecessarily; COM+ services such as Object Pooling also work properly.
using( TransServer TransServer = new TransServer()) { currentqty.Text = TransServer.Getcurrentqty().ToString(); } } catch(currentqtyNotReadException) { MessageBox.Show("Unable to read the current qty”, "Error"); Application.Exit(); } catch(Exception ex) { MessageBox.Show(ex.Message + "\nUnable to launch the COM+ Server.”, "Error"); Application.Exit(); } }
protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows Form Designer generated code private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.txtProduct = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.txtnewQty = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.button3 = new System.Windows.Forms.Button(); this.label3 = new System.Windows.Forms.Label(); this.currqtylabel = new System.Windows.Forms.Label(); this.SuspendLayout(); // // button1 // this.button1.Location = new Point(8, 104); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(88, 23); this.button1.TabIndex = 2; this.button1.Text = "&Update"; this.button1.Click += new System.EventHandler(this.Button1_Click); // // button2 // this.button2.Location = new Point(104, 104); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(88, 23); this.button2.TabIndex = 3; this.button2.Text = "&AutoUpdate"; this.button2.Click += new System.EventHandler(this.Button2_Click); // // txtProduct // this.txtProduct.BackColor = Color.White; this.txtProduct.Location = new Point(144, 8); this.txtProduct.Name = "txtProduct"; this.txtProduct.ReadOnly = true; this.txtProduct.TabIndex = 7; this.txtProduct.Text = ""; // // label1 // this.label1.Location = new Point(32, 8); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(80, 16); this.label1.TabIndex = 6; this.label1.Text = "Product Id:"; // // txtnewQty // this.txtnewQty.Location = new Point(144, 40); this.txtnewQty.Name = "txtnewQty"; this.txtnewQty.TabIndex = 8; this.txtnewQty.Text = ""; // // label2 // this.label2.Location = new Point(32, 40); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(80, 16); this.label2.TabIndex = 9; this.label2.Text = "New Quantity:"; // // button3 // this.button3.Location = new Point(200, 104); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(64, 23); this.button3.TabIndex = 10; this.button3.Text = "&Close"; // // label3 // this.label3.Location = new Point(34, 72); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(94, 16); this.label3.TabIndex = 12; this.label3.Text = "Current Quantity:"; // // currqtylabel // this.currqtylabel.BorderStyle = BorderStyle.FixedSingle; this.currqtylabel.Location = new Point(144, 72); this.currqtylabel.Name = "currqtylabel"; this.currqtylabel.Size = new Size(96, 16); this.currqtylabel.TabIndex = 13; // // QtyForm // this.AutoScaleBaseSize = new Size(5, 13); this.ClientSize = new System.Drawing.Size(280, 133); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.currqtylabel, this.label3, this.button3, this.txtnewQty, this.label2, this.txtProduct, this.label1, this.button2, this.button1}); this.Name = "QtyForm"; this.Text = "Quantity on Hand - Product"; this.Load += new System.EventHandler(this.QtyForm_Load); this.ResumeLayout(false);
} #endregion
// Manual update - Update button private void Button1_Click(object sender, System.EventArgs e) { int val = 0; try { val = int.Parse(newValue.Text); } catch { MessageBox.Show("Enter a value to update!"); return; } try { using( TransServer TransServer = new TransServer()) { TransServer.Update(txtProduct.Text, val); } } catch(Exception ex) { MessageBox.Show(ex.Message); return; } try { using( TransServer TransServer = new TransServer()) { currentqty.Text = TransServer.Getcurrentqty(txtProduct.text).ToString(); } } catch(currentqtyNotReadException) { MessageBox.Show("Unable to read”,"Error"); return; } }
// automatic transaction update - autoupdate button private void Button2_Click(object sender, System.EventArgs e) { int val = 0; try { val = int.Parse(newValue.Text); } catch { MessageBox.Show("Enter a value!"); return; } try { using( TransServer TransServer = new TransServer()) { TransServer.AutoUpdate(val); } } catch(Exception ex) { MessageBox.Show(ex.Message); return; } try { using( TransServer TransServer = new TransServer()) { currentqty.Text = TransServer.Getcurrentqty(txtProduct.Text).ToString(); } } catch(Exception ex) { MessageBox.Show(ex.Message”, "Error"); return; } }
private static void Main(String[] args) { Application.Run(new QtyForm()); }
} }
Summary
COM+ services provides you underlying mechanism of handling your transactions. You need not have to worry about beginning, committing or aborting the transactions.
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|