Hello,I was wondering, if anybody could kindly help me. Basically, I am very new to 'C#'. The problem I have is that, I have a database which details all the tags on the process plants, there could at anytime be same tags, i.e. duplicates in this database. I wrote the following code to look for a tag which the user wants. But I had to put a "break statement" when I have found the very first one. When I take this "break statement", then the software crashes. Could you kindly tell me what I can do so that the occurances of a particular tag will be displayed depending how many of them are within the database. Many thanks Behzad using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Data.OleDb;
namespace SearchTag{ /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { DataSet varsData = new DataSet();
bool firstTime = true; private System.Windows.Forms.Label tagLabel; private System.Windows.Forms.TextBox textBoxTag; private System.Windows.Forms.DataGrid dataGrid1; string tempString; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuItem menuItem2; private System.Windows.Forms.MenuItem menuItem3; private System.Windows.Forms.MenuItem menuItem4; private System.Windows.Forms.MenuItem menuItem5; private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox textBox3; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null;
public Form1() { // // Required for Windows Form Designer support // InitializeComponent();
// // TODO: Add any constructor code after InitializeComponent call // }
/// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.tagLabel = new System.Windows.Forms.Label(); this.textBoxTag = new System.Windows.Forms.TextBox(); this.dataGrid1 = new System.Windows.Forms.DataGrid(); this.menuItem1 = new System.Windows.Forms.MenuItem(); this.menuItem2 = new System.Windows.Forms.MenuItem(); this.menuItem3 = new System.Windows.Forms.MenuItem(); this.menuItem4 = new System.Windows.Forms.MenuItem(); this.menuItem5 = new System.Windows.Forms.MenuItem(); this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.textBox3 = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox1 = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.SuspendLayout(); // // tagLabel // this.tagLabel.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.tagLabel.Location = new System.Drawing.Point(18, 39); this.tagLabel.Name = "tagLabel"; this.tagLabel.Size = new System.Drawing.Size(112, 23); this.tagLabel.TabIndex = 0; this.tagLabel.Text = "Please Enter Tag"; // // textBoxTag // this.textBoxTag.BackColor = System.Drawing.SystemColors.Window; this.textBoxTag.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.2F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.textBoxTag.ForeColor = System.Drawing.SystemColors.WindowText; this.textBoxTag.Location = new System.Drawing.Point(135, 36); this.textBoxTag.MaxLength = 13; this.textBoxTag.Name = "textBoxTag"; this.textBoxTag.Size = new System.Drawing.Size(200, 27); this.textBoxTag.TabIndex = 1; this.textBoxTag.Text = ""; this.textBoxTag.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; this.textBoxTag.TextChanged += new System.EventHandler(this.textBoxTag_TextChanged); // // dataGrid1 // this.dataGrid1.CaptionFont = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.dataGrid1.DataMember = ""; this.dataGrid1.Font = new System.Drawing.Font("Times New Roman", 7.8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(6, 222); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(807, 129); this.dataGrid1.TabIndex = 4; // // menuItem1 // this.menuItem1.Index = 0; this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem2, this.menuItem3}); this.menuItem1.Text = "&File"; // // menuItem2 // this.menuItem2.Index = 0; this.menuItem2.Text = "&Open"; this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click); // // menuItem3 // this.menuItem3.Index = 1; this.menuItem3.Text = "&Exit"; this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click); // // menuItem4 // this.menuItem4.Index = 1; this.menuItem4.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem5}); this.menuItem4.Text = "&Search"; // // menuItem5 // this.menuItem5.Index = 0; this.menuItem5.Text = "&TagSearch"; this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click_1); // // mainMenu1 // this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem1, this.menuItem4}); // // groupBox1 // this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] { this.tagLabel, this.textBoxTag}); this.groupBox1.Location = new System.Drawing.Point(15, 12); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(342, 93); this.groupBox1.TabIndex = 5; this.groupBox1.TabStop = false; this.groupBox1.Text = "TagEntry"; // // groupBox2 // this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] { this.textBox3, this.label3, this.label2, this.textBox2, this.textBox1, this.label1}); this.groupBox2.Location = new System.Drawing.Point(384, 9); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(405, 186); this.groupBox2.TabIndex = 6; this.groupBox2.TabStop = false; this.groupBox2.Text = "Flow Details"; // // textBox3 // this.textBox3.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.2F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.textBox3.Location = new System.Drawing.Point(141, 117); this.textBox3.Name = "textBox3"; this.textBox3.Size = new System.Drawing.Size(192, 27); this.textBox3.TabIndex = 5; this.textBox3.Text = ""; // // label3 // this.label3.Location = new System.Drawing.Point(3, 114); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(129, 24); this.label3.TabIndex = 4; this.label3.Text = "Service"; // // label2 // this.label2.Location = new System.Drawing.Point(6, 72); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(129, 24); this.label2.TabIndex = 3; this.label2.Text = "Fluid Type"; // // textBox2 // this.textBox2.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.2F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.textBox2.Location = new System.Drawing.Point(141, 72); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(192, 27); this.textBox2.TabIndex = 2; this.textBox2.Text = ""; // // textBox1 // this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.2F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.textBox1.Location = new System.Drawing.Point(141, 30); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(192, 27); this.textBox1.TabIndex = 1; this.textBox1.Text = "15\'C @ 1.103 bara"; this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged); // // label1 // this.label1.Location = new System.Drawing.Point(6, 30); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(129, 24); this.label1.TabIndex = 0; this.label1.Text = "Standard Condition"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 15); this.ClientSize = new System.Drawing.Size(807, 360); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.groupBox2, this.groupBox1, this.dataGrid1}); this.ForeColor = System.Drawing.SystemColors.Desktop; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; this.MaximizeBox = false; this.Menu = this.mainMenu1; this.MinimizeBox = false; this.Name = "Form1"; this.Text = "SerachTag"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.groupBox1.ResumeLayout(false); this.groupBox2.ResumeLayout(false); this.ResumeLayout(false);
} #endregion
/// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); }
private void Form1_Load(object sender, System.EventArgs e) { }
private void endButton_Click(object sender, System.EventArgs e) { } private void ClearTextBox() { this.textBoxTag.Clear(); this.textBoxTag.Focus(); } private void WriteToDataGrid(string tagToUse) { string common = ""; string message = "The tag does not exist"; string caption = "Tag Not Found"; MessageBoxButtons button= MessageBoxButtons.RetryCancel; DialogResult result; bool found = false; int counter = 0; DataTable dt = new DataTable();
dt.Columns.Add("FC"); dt.Columns.Add("StreamId"); dt.Columns.Add("TemTag"); dt.Columns.Add("Temp0mA"); dt.Columns.Add("Temp20mA"); dt.Columns.Add("TempLoAla"); dt.Columns.Add("TempHiAla"); dt.Columns.Add("TempKP"); //Pressure dt.Columns.Add("PresTag"); dt.Columns.Add("Pres0mA"); dt.Columns.Add("Pres20mA"); dt.Columns.Add("PresLoAla"); dt.Columns.Add("PresHiAla"); dt.Columns.Add("PresKP");
//Flow dt.Columns.Add("FlowTag"); dt.Columns.Add("Flow0mA"); dt.Columns.Add("Flow20mA"); dt.Columns.Add("FlowLoAla"); dt.Columns.Add("FlowHiAla"); dt.Columns.Add("FlowKP");
//Density dt.Columns.Add("DensTag"); dt.Columns.Add("Dens0mA"); dt.Columns.Add("Dens20mA"); dt.Columns.Add("DensLoAl"); dt.Columns.Add("DensHiAl"); dt.Columns.Add("DensKP");
//Type dt.Columns.Add("FluidType"); dt.Columns.Add("Service");
// Repeat for each table in the DataSet collection. foreach(DataTable table in varsData.Tables) { foreach(DataRow row in table.Rows) {
// Put the TempTag in a common string for string comparison common = row[tagToUse].ToString(); if (string.Compare(common,0,textBoxTag.Text,0,textBoxTag.Text.Length)==0) { found = true; dt.Rows.Add(new object[] {row["FC"].ToString(),row["StreamId"].ToString(), row["TempTag"].ToString(),row["Temp0mA"].ToString(), row["Temp20mA"].ToString(),row["TempLoAL"].ToString(), row["TempHiAL"].ToString(),row["TempKeyPad"].ToString(), row["PressTag"].ToString(),row["Press0mA"].ToString(), row["Press20mA"].ToString(),row["PressLoAL"].ToString(), row["PressHiAL"].ToString(),row["PressKeyPad"].ToString(), row["FlowTag"].ToString(),row["Flow0mA"].ToString(), row["Flow20mA"].ToString(),row["FlowLoAL"].ToString(), row["FlowHiAL"].ToString(),row["FlowKeyPad"].ToString(), row["DensTag"].ToString(), row["Dens0mA"].ToString(), row["Dens20mA"].ToString(), row["DensLoAl"].ToString(), row["DensHiAl"].ToString(), row["DensKeyPad"].ToString(), row["FluidType"].ToString(),row["Service"].ToString()});
textBox2.Text = row["FluidType"].ToString(); textBox3.Text = row["Service"].ToString(); this.dataGrid1.DataSource = dt; break; } } } if (found == false) { result = MessageBox.Show(this, message, caption,button, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign); if (result == DialogResult.Cancel) return; else { ClearTextBox(); dt.Rows.Clear(); this.dataGrid1.DataSource = dt; } } }
private void button1_Click(object sender, System.EventArgs e) { }
private void textBoxTag_TextChanged(object sender, System.EventArgs e) { }
private void menuItem3_Click(object sender, System.EventArgs e) { Application.Exit(); }
private void menuItem2_Click(object sender, System.EventArgs e) { OpenFileDialog fdlg = new OpenFileDialog(); fdlg.Title = "Open File Dialog" ; fdlg.InitialDirectory = @"c:\OMNIAZER\PROJECTS" ; fdlg.Filter = "All files (*.*)|*.*|Access Database(*.mdb) files (*.mdb)|*.mdb" ; fdlg.FilterIndex = 2 ; fdlg.RestoreDirectory = true ; if(fdlg.ShowDialog() == DialogResult.OK) tempString = fdlg.FileName; }
private void menuItem5_Click(object sender, System.EventArgs e) { }
private void menuItem5_Click_1(object sender, System.EventArgs e) {
string connectionString; string whichOne = "TIT", whichVar, textEntry, tagToUse = ""; string [] tags = new String[]{"TIT", "PIT","FIT", "DT "}; int i; bool found = false;
if (tempString != null) { connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+tempString;
OleDbConnection myConnection = new OleDbConnection(connectionString); myConnection.Open(); string query = "select * from Vars"; OleDbDataAdapter myAdapter = new OleDbDataAdapter(query, myConnection); myAdapter.Fill(varsData); textEntry = textBoxTag.Text; if (textEntry.Length<12) { MessageBox.Show("The entry is not a valid, please re-enter"); ClearTextBox(); return; } whichVar = textBoxTag.Text.Substring(3,3); for (i =0; i < tags.Length; i++) { if (string.Compare(whichVar,0,tags[i],0,3) ==0) { found = true; whichOne = tags[i]; } } if (found != true) { MessageBox.Show("Please enter a valid tag, The valid tag should have 'TIT' OR 'PIT' OR 'FIT'"); ClearTextBox(); return; } else { if (whichOne[0] == 'T') tagToUse = "TempTag"; else if(whichOne[0] == 'P') tagToUse = "PressTag"; else if (whichOne[0] == 'D') tagToUse = "DensTag"; else tagToUse = "FlowTag"; } WriteToDataGrid(tagToUse); } else { MessageBox.Show(this, "You Must Open *.mdb file first", "NoFileOpenYet",MessageBoxButtons.OK, MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign); } }
private void textBox1_TextChanged(object sender, System.EventArgs e) { } }}