RFID Programming

Here am shows how to read RFID data and store information in to database 

Form1.designer.cs

Code:

  1. namespace AMS  
  2. {  
  3.     partial class Form1  
  4.     {  
  5.         /// <summary>  
  6.         /// Required designer variable.  
  7.         /// </summary>  
  8.         private System.ComponentModel.IContainer components = null;  
  9.   
  10.         /// <summary>  
  11.         /// Clean up any resources being used.  
  12.         /// </summary>  
  13.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>  
  14.         protected override void Dispose(bool disposing)  
  15.         {  
  16.             if (disposing && (components != null))  
  17.             {  
  18.                 components.Dispose();  
  19.             }  
  20.             base.Dispose(disposing);  
  21.         }  
  22.  
  23.         #region Windows Form Designer generated code  
  24.   
  25.         /// <summary>  
  26.         /// Required method for Designer support - do not modify  
  27.         /// the contents of this method with the code editor.  
  28.         /// </summary>  
  29.         private void InitializeComponent()  
  30.         {  
  31.             this.txttag = new System.Windows.Forms.TextBox();  
  32.             this.label1 = new System.Windows.Forms.Label();  
  33.             this.label2 = new System.Windows.Forms.Label();  
  34.             this.label3 = new System.Windows.Forms.Label();  
  35.             this.label4 = new System.Windows.Forms.Label();  
  36.             this.label5 = new System.Windows.Forms.Label();  
  37.             this.txtstudname = new System.Windows.Forms.TextBox();  
  38.             this.txtrollno = new System.Windows.Forms.TextBox();  
  39.             this.comboclass = new System.Windows.Forms.ComboBox();  
  40.             this.combosec = new System.Windows.Forms.ComboBox();  
  41.             this.btnsubmit = new System.Windows.Forms.Button();  
  42.             this.btncancel = new System.Windows.Forms.Button();  
  43.             this.SuspendLayout();  
  44.             //   
  45.             // txttag  
  46.             //   
  47.             this.txttag.Location = new System.Drawing.Point(138, 50);  
  48.             this.txttag.Name = "txttag";  
  49.             this.txttag.Size = new System.Drawing.Size(100, 20);  
  50.             this.txttag.TabIndex = 0;  
  51.             //   
  52.             // label1  
  53.             //   
  54.             this.label1.AutoSize = true;  
  55.             this.label1.Location = new System.Drawing.Point(60, 50);  
  56.             this.label1.Name = "label1";  
  57.             this.label1.Size = new System.Drawing.Size(40, 13);  
  58.             this.label1.TabIndex = 1;  
  59.             this.label1.Text = "Tag ID";  
  60.             //   
  61.             // label2  
  62.             //   
  63.             this.label2.AutoSize = true;  
  64.             this.label2.Location = new System.Drawing.Point(44, 78);  
  65.             this.label2.Name = "label2";  
  66.             this.label2.Size = new System.Drawing.Size(75, 13);  
  67.             this.label2.TabIndex = 2;  
  68.             this.label2.Text = "Student Name";  
  69.             //   
  70.             // label3  
  71.             //   
  72.             this.label3.AutoSize = true;  
  73.             this.label3.Location = new System.Drawing.Point(60, 108);  
  74.             this.label3.Name = "label3";  
  75.             this.label3.Size = new System.Drawing.Size(42, 13);  
  76.             this.label3.TabIndex = 3;  
  77.             this.label3.Text = "Roll No";  
  78.             //   
  79.             // label4  
  80.             //   
  81.             this.label4.AutoSize = true;  
  82.             this.label4.Location = new System.Drawing.Point(60, 135);  
  83.             this.label4.Name = "label4";  
  84.             this.label4.Size = new System.Drawing.Size(32, 13);  
  85.             this.label4.TabIndex = 4;  
  86.             this.label4.Text = "Class";  
  87.             //   
  88.             // label5  
  89.             //   
  90.             this.label5.AutoSize = true;  
  91.             this.label5.Location = new System.Drawing.Point(57, 163);  
  92.             this.label5.Name = "label5";  
  93.             this.label5.Size = new System.Drawing.Size(43, 13);  
  94.             this.label5.TabIndex = 5;  
  95.             this.label5.Text = "Section";  
  96.             //   
  97.             // txtstudname  
  98.             //   
  99.             this.txtstudname.Location = new System.Drawing.Point(138, 78);  
  100.             this.txtstudname.Name = "txtstudname";  
  101.             this.txtstudname.Size = new System.Drawing.Size(100, 20);  
  102.             this.txtstudname.TabIndex = 6;  
  103.             //   
  104.             // txtrollno  
  105.             //   
  106.             this.txtrollno.Location = new System.Drawing.Point(138, 105);  
  107.             this.txtrollno.Name = "txtrollno";  
  108.             this.txtrollno.Size = new System.Drawing.Size(100, 20);  
  109.             this.txtrollno.TabIndex = 7;  
  110.             //   
  111.             // comboclass  
  112.             //   
  113.             this.comboclass.FormattingEnabled = true;  
  114.             this.comboclass.Items.AddRange(new object[] {  
  115.             "I",  
  116.             "II",  
  117.             "III",  
  118.             "IV",  
  119.             "V",  
  120.             "VI",  
  121.             "VII",  
  122.             "VIII",  
  123.             "IX",  
  124.             "X"});  
  125.             this.comboclass.Location = new System.Drawing.Point(138, 132);  
  126.             this.comboclass.Name = "comboclass";  
  127.             this.comboclass.Size = new System.Drawing.Size(121, 21);  
  128.             this.comboclass.TabIndex = 8;  
  129.             //   
  130.             // combosec  
  131.             //   
  132.             this.combosec.FormattingEnabled = true;  
  133.             this.combosec.Items.AddRange(new object[] {  
  134.             "A",  
  135.             "B",  
  136.             "C",  
  137.             "D",  
  138.             "E",  
  139.             "F",  
  140.             "G",  
  141.             "H"});  
  142.             this.combosec.Location = new System.Drawing.Point(138, 160);  
  143.             this.combosec.Name = "combosec";  
  144.             this.combosec.Size = new System.Drawing.Size(121, 21);  
  145.             this.combosec.TabIndex = 9;  
  146.             //   
  147.             // btnsubmit  
  148.             //   
  149.             this.btnsubmit.Location = new System.Drawing.Point(105, 202);  
  150.             this.btnsubmit.Name = "btnsubmit";  
  151.             this.btnsubmit.Size = new System.Drawing.Size(75, 23);  
  152.             this.btnsubmit.TabIndex = 10;  
  153.             this.btnsubmit.Text = "Submit";  
  154.             this.btnsubmit.UseVisualStyleBackColor = true;  
  155.             this.btnsubmit.Click += new System.EventHandler(this.btnsubmit_Click);  
  156.             //   
  157.             // btncancel  
  158.             //   
  159.             this.btncancel.Location = new System.Drawing.Point(197, 202);  
  160.             this.btncancel.Name = "btncancel";  
  161.             this.btncancel.Size = new System.Drawing.Size(75, 23);  
  162.             this.btncancel.TabIndex = 11;  
  163.             this.btncancel.Text = "Cancel";  
  164.             this.btncancel.UseVisualStyleBackColor = true;  
  165.             //   
  166.             // Form1  
  167.             //   
  168.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);  
  169.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;  
  170.             this.ClientSize = new System.Drawing.Size(290, 261);  
  171.             this.Controls.Add(this.btncancel);  
  172.             this.Controls.Add(this.btnsubmit);  
  173.             this.Controls.Add(this.combosec);  
  174.             this.Controls.Add(this.comboclass);  
  175.             this.Controls.Add(this.txtrollno);  
  176.             this.Controls.Add(this.txtstudname);  
  177.             this.Controls.Add(this.label5);  
  178.             this.Controls.Add(this.label4);  
  179.             this.Controls.Add(this.label3);  
  180.             this.Controls.Add(this.label2);  
  181.             this.Controls.Add(this.label1);  
  182.             this.Controls.Add(this.txttag);  
  183.             this.Name = "Form1";  
  184.             this.Text = "Form1";  
  185.             this.Load += new System.EventHandler(this.Form1_Load);  
  186.             this.ResumeLayout(false);  
  187.             this.PerformLayout();  
  188.   
  189.         }  
  190.  
  191.         #endregion  
  192.   
  193.         private System.Windows.Forms.TextBox txttag;  
  194.         private System.Windows.Forms.Label label1;  
  195.         private System.Windows.Forms.Label label2;  
  196.         private System.Windows.Forms.Label label3;  
  197.         private System.Windows.Forms.Label label4;  
  198.         private System.Windows.Forms.Label label5;  
  199.         private System.Windows.Forms.TextBox txtstudname;  
  200.         private System.Windows.Forms.TextBox txtrollno;  
  201.         private System.Windows.Forms.ComboBox comboclass;  
  202.         private System.Windows.Forms.ComboBox combosec;  
  203.         private System.Windows.Forms.Button btnsubmit;  
  204.         private System.Windows.Forms.Button btncancel;  
  205.     }  
  206. }  
Screen Design:

form 1
Form1.cs

Code:

Adding namespaces for serial port connection.
  1. using System.IO.Ports;  
  2. using System.Threading;  
  3. using System.Data.Sql;  
  4. using System.Data.SqlClient;  
Full Code:
  1. using System.IO.Ports;  
  2. using System.Threading;  
  3. using System.Data.Sql;  
  4. using System.Data.SqlClient;  
  5.   
  6. Full Code:  
  7. using System;  
  8. using System.Collections.Generic;  
  9. using System.ComponentModel;  
  10. using System.Data;  
  11. using System.Drawing;  
  12. using System.Linq;  
  13. using System.Text;  
  14. using System.Windows.Forms;  
  15. using System.IO.Ports;  
  16. using System.Threading;  
  17. using System.Data.Sql;  
  18. using System.Data.SqlClient;  
  19. namespace AMS  
  20. {  
  21.     public partial class Form1 : Form  
  22.     {  
  23.         string str = "Data Source=.;Initial Catalog=AMS;Integrated Security=True";  
  24.         private SerialPort RFID;  
  25.         private string DispString;  
  26.          public Form1()  
  27.         {  
  28.             InitializeComponent();  
  29.         }  
  30.   
  31.         private void Form1_Load(object sender, EventArgs e)  
  32.         {  
  33.                     
  34.             RFID = new SerialPort();  
  35.             RFID.PortName = "COM5";  
  36.             RFID.BaudRate = 9600;  
  37.             RFID.DataBits = 8;  
  38.             RFID.Parity = Parity.None;  
  39.             RFID.StopBits = StopBits.One;  
  40.             RFID.Open();  
  41.             RFID.ReadTimeout = 200;  
  42.             if (RFID.IsOpen)  
  43.             {  
  44.                 DispString = "";  
  45.                 txttag.Text = "";  
  46.             }  
  47.             else  
  48.             {  
  49.                 RFID.Close();  
  50.             }  
  51.             RFID.DataReceived += new SerialDataReceivedEventHandler(RFID_DataReceived);  
  52.                 
  53.         }  
  54.   
  55.         private void button1_Click(object sender, EventArgs e)  
  56.         {  
  57.               
  58.         }  
  59.         private void RFID_DataReceived(object sender, SerialDataReceivedEventArgs e)  
  60.         {  
  61.               
  62.             if (txttag.Text.Length >= 12)  
  63.             {  
  64.                RFID.Close();   
  65.   
  66.             }  
  67.             else  
  68.             {  
  69.                 DispString = RFID.ReadExisting();  
  70.                 this.Invoke(new EventHandler(DisplayText));  
  71.             }  
  72.              
  73.         }  
  74.         private void DisplayText(object sender, EventArgs e)  
  75.         {  
  76.             txttag.AppendText(DispString);  
  77.         }  
  78.   
  79.         private void btnsubmit_Click(object sender, EventArgs e)  
  80.         {  
  81.             switch (btnsubmit.Text)  
  82.             {  
  83.                 case "Submit":  
  84.                     if (txttag.Text.Length == 0 || txtrollno.Text.Length == 0 || txtstudname.Text.Length == 0)  
  85.                     { return; }  
  86.                     if (!CheckDuplicateRFID())  
  87.                     {  
  88.                         MessageBox.Show("Already Saved");  
  89.                         txttag.Text = "";  
  90.                         return;  
  91.                     }  
  92.                     break;  
  93.             }  
  94.             SqlConnection con = new SqlConnection(str);  
  95.             con.Open();  
  96.             SqlCommand cmd = new SqlCommand("insert into tbl_Studentdet(RFID,Student_Name,RollNo,Class,Section) values (@RFID,@Student_Name,@RollNo,@Class,@Section)", con);  
  97.             cmd.Parameters.AddWithValue("@RFID", txttag.Text);  
  98.             cmd.Parameters.AddWithValue("@Student_Name", txtstudname.Text);  
  99.             cmd.Parameters.AddWithValue("@RollNo", txtrollno.Text);  
  100.             cmd.Parameters.AddWithValue("@Section", combosec.SelectedItem.ToString());  
  101.             cmd.Parameters.AddWithValue("@Class", comboclass.SelectedItem.ToString());  
  102.             cmd.ExecuteNonQuery();  
  103.             MessageBox.Show("Student added");  
  104.             con.Close();  
  105.         }  
  106.         private bool CheckDuplicateRFID()  
  107.         {  
  108.             SqlConnection con = new SqlConnection(str);  
  109.             con.Open();  
  110.             SqlCommand cmd = new SqlCommand("select * from tbl_Studentdet where RFID='" + txttag.Text + "'", con);  
  111.             SqlDataReader rd = cmd.ExecuteReader();  
  112.             if (rd.HasRows)  
  113.             {  
  114.                 rd.Close();  
  115.                 return false;  
  116.             }  
  117.             else  
  118.             {  
  119.                 rd.Close();  
  120.                 return true;  
  121.             }  
  122.         }  
  123.                  
  124.      
  125.     }  
  126. }  
Output(While showing the card In to the RFID reader).

Output

Next Recommended Reading How To Start A Program/Process