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. /// <summary>
  10. /// Clean up any resources being used.
  11. /// </summary>
  12. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  13. protected override void Dispose(bool disposing)
  14. {
  15. if (disposing && (components != null))
  16. {
  17. components.Dispose();
  18. }
  19. base.Dispose(disposing);
  20. }
  21. #region Windows Form Designer generated code
  22. /// <summary>
  23. /// Required method for Designer support - do not modify
  24. /// the contents of this method with the code editor.
  25. /// </summary>
  26. private void InitializeComponent()
  27. {
  28. this.txttag = new System.Windows.Forms.TextBox();
  29. this.label1 = new System.Windows.Forms.Label();
  30. this.label2 = new System.Windows.Forms.Label();
  31. this.label3 = new System.Windows.Forms.Label();
  32. this.label4 = new System.Windows.Forms.Label();
  33. this.label5 = new System.Windows.Forms.Label();
  34. this.txtstudname = new System.Windows.Forms.TextBox();
  35. this.txtrollno = new System.Windows.Forms.TextBox();
  36. this.comboclass = new System.Windows.Forms.ComboBox();
  37. this.combosec = new System.Windows.Forms.ComboBox();
  38. this.btnsubmit = new System.Windows.Forms.Button();
  39. this.btncancel = new System.Windows.Forms.Button();
  40. this.SuspendLayout();
  41. //
  42. // txttag
  43. //
  44. this.txttag.Location = new System.Drawing.Point(138, 50);
  45. this.txttag.Name = "txttag";
  46. this.txttag.Size = new System.Drawing.Size(100, 20);
  47. this.txttag.TabIndex = 0;
  48. //
  49. // label1
  50. //
  51. this.label1.AutoSize = true;
  52. this.label1.Location = new System.Drawing.Point(60, 50);
  53. this.label1.Name = "label1";
  54. this.label1.Size = new System.Drawing.Size(40, 13);
  55. this.label1.TabIndex = 1;
  56. this.label1.Text = "Tag ID";
  57. //
  58. // label2
  59. //
  60. this.label2.AutoSize = true;
  61. this.label2.Location = new System.Drawing.Point(44, 78);
  62. this.label2.Name = "label2";
  63. this.label2.Size = new System.Drawing.Size(75, 13);
  64. this.label2.TabIndex = 2;
  65. this.label2.Text = "Student Name";
  66. //
  67. // label3
  68. //
  69. this.label3.AutoSize = true;
  70. this.label3.Location = new System.Drawing.Point(60, 108);
  71. this.label3.Name = "label3";
  72. this.label3.Size = new System.Drawing.Size(42, 13);
  73. this.label3.TabIndex = 3;
  74. this.label3.Text = "Roll No";
  75. //
  76. // label4
  77. //
  78. this.label4.AutoSize = true;
  79. this.label4.Location = new System.Drawing.Point(60, 135);
  80. this.label4.Name = "label4";
  81. this.label4.Size = new System.Drawing.Size(32, 13);
  82. this.label4.TabIndex = 4;
  83. this.label4.Text = "Class";
  84. //
  85. // label5
  86. //
  87. this.label5.AutoSize = true;
  88. this.label5.Location = new System.Drawing.Point(57, 163);
  89. this.label5.Name = "label5";
  90. this.label5.Size = new System.Drawing.Size(43, 13);
  91. this.label5.TabIndex = 5;
  92. this.label5.Text = "Section";
  93. //
  94. // txtstudname
  95. //
  96. this.txtstudname.Location = new System.Drawing.Point(138, 78);
  97. this.txtstudname.Name = "txtstudname";
  98. this.txtstudname.Size = new System.Drawing.Size(100, 20);
  99. this.txtstudname.TabIndex = 6;
  100. //
  101. // txtrollno
  102. //
  103. this.txtrollno.Location = new System.Drawing.Point(138, 105);
  104. this.txtrollno.Name = "txtrollno";
  105. this.txtrollno.Size = new System.Drawing.Size(100, 20);
  106. this.txtrollno.TabIndex = 7;
  107. //
  108. // comboclass
  109. //
  110. this.comboclass.FormattingEnabled = true;
  111. this.comboclass.Items.AddRange(new object[] {
  112. "I",
  113. "II",
  114. "III",
  115. "IV",
  116. "V",
  117. "VI",
  118. "VII",
  119. "VIII",
  120. "IX",
  121. "X"});
  122. this.comboclass.Location = new System.Drawing.Point(138, 132);
  123. this.comboclass.Name = "comboclass";
  124. this.comboclass.Size = new System.Drawing.Size(121, 21);
  125. this.comboclass.TabIndex = 8;
  126. //
  127. // combosec
  128. //
  129. this.combosec.FormattingEnabled = true;
  130. this.combosec.Items.AddRange(new object[] {
  131. "A",
  132. "B",
  133. "C",
  134. "D",
  135. "E",
  136. "F",
  137. "G",
  138. "H"});
  139. this.combosec.Location = new System.Drawing.Point(138, 160);
  140. this.combosec.Name = "combosec";
  141. this.combosec.Size = new System.Drawing.Size(121, 21);
  142. this.combosec.TabIndex = 9;
  143. //
  144. // btnsubmit
  145. //
  146. this.btnsubmit.Location = new System.Drawing.Point(105, 202);
  147. this.btnsubmit.Name = "btnsubmit";
  148. this.btnsubmit.Size = new System.Drawing.Size(75, 23);
  149. this.btnsubmit.TabIndex = 10;
  150. this.btnsubmit.Text = "Submit";
  151. this.btnsubmit.UseVisualStyleBackColor = true;
  152. this.btnsubmit.Click += new System.EventHandler(this.btnsubmit_Click);
  153. //
  154. // btncancel
  155. //
  156. this.btncancel.Location = new System.Drawing.Point(197, 202);
  157. this.btncancel.Name = "btncancel";
  158. this.btncancel.Size = new System.Drawing.Size(75, 23);
  159. this.btncancel.TabIndex = 11;
  160. this.btncancel.Text = "Cancel";
  161. this.btncancel.UseVisualStyleBackColor = true;
  162. //
  163. // Form1
  164. //
  165. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  166. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  167. this.ClientSize = new System.Drawing.Size(290, 261);
  168. this.Controls.Add(this.btncancel);
  169. this.Controls.Add(this.btnsubmit);
  170. this.Controls.Add(this.combosec);
  171. this.Controls.Add(this.comboclass);
  172. this.Controls.Add(this.txtrollno);
  173. this.Controls.Add(this.txtstudname);
  174. this.Controls.Add(this.label5);
  175. this.Controls.Add(this.label4);
  176. this.Controls.Add(this.label3);
  177. this.Controls.Add(this.label2);
  178. this.Controls.Add(this.label1);
  179. this.Controls.Add(this.txttag);
  180. this.Name = "Form1";
  181. this.Text = "Form1";
  182. this.Load += new System.EventHandler(this.Form1_Load);
  183. this.ResumeLayout(false);
  184. this.PerformLayout();
  185. }
  186. #endregion
  187. private System.Windows.Forms.TextBox txttag;
  188. private System.Windows.Forms.Label label1;
  189. private System.Windows.Forms.Label label2;
  190. private System.Windows.Forms.Label label3;
  191. private System.Windows.Forms.Label label4;
  192. private System.Windows.Forms.Label label5;
  193. private System.Windows.Forms.TextBox txtstudname;
  194. private System.Windows.Forms.TextBox txtrollno;
  195. private System.Windows.Forms.ComboBox comboclass;
  196. private System.Windows.Forms.ComboBox combosec;
  197. private System.Windows.Forms.Button btnsubmit;
  198. private System.Windows.Forms.Button btncancel;
  199. }
  200. }
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. Full Code:
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Drawing;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Windows.Forms;
  14. using System.IO.Ports;
  15. using System.Threading;
  16. using System.Data.Sql;
  17. using System.Data.SqlClient;
  18. namespace AMS
  19. {
  20. public partial class Form1 : Form
  21. {
  22. string str = "Data Source=.;Initial Catalog=AMS;Integrated Security=True";
  23. private SerialPort RFID;
  24. private string DispString;
  25. public Form1()
  26. {
  27. InitializeComponent();
  28. }
  29. private void Form1_Load(object sender, EventArgs e)
  30. {
  31. RFID = new SerialPort();
  32. RFID.PortName = "COM5";
  33. RFID.BaudRate = 9600;
  34. RFID.DataBits = 8;
  35. RFID.Parity = Parity.None;
  36. RFID.StopBits = StopBits.One;
  37. RFID.Open();
  38. RFID.ReadTimeout = 200;
  39. if (RFID.IsOpen)
  40. {
  41. DispString = "";
  42. txttag.Text = "";
  43. }
  44. else
  45. {
  46. RFID.Close();
  47. }
  48. RFID.DataReceived += new SerialDataReceivedEventHandler(RFID_DataReceived);
  49. }
  50. private void button1_Click(object sender, EventArgs e)
  51. {
  52. }
  53. private void RFID_DataReceived(object sender, SerialDataReceivedEventArgs e)
  54. {
  55. if (txttag.Text.Length >= 12)
  56. {
  57. RFID.Close();
  58. }
  59. else
  60. {
  61. DispString = RFID.ReadExisting();
  62. this.Invoke(new EventHandler(DisplayText));
  63. }
  64. }
  65. private void DisplayText(object sender, EventArgs e)
  66. {
  67. txttag.AppendText(DispString);
  68. }
  69. private void btnsubmit_Click(object sender, EventArgs e)
  70. {
  71. switch (btnsubmit.Text)
  72. {
  73. case "Submit":
  74. if (txttag.Text.Length == 0 || txtrollno.Text.Length == 0 || txtstudname.Text.Length == 0)
  75. { return; }
  76. if (!CheckDuplicateRFID())
  77. {
  78. MessageBox.Show("Already Saved");
  79. txttag.Text = "";
  80. return;
  81. }
  82. break;
  83. }
  84. SqlConnection con = new SqlConnection(str);
  85. con.Open();
  86. SqlCommand cmd = new SqlCommand("insert into tbl_Studentdet(RFID,Student_Name,RollNo,Class,Section) values (@RFID,@Student_Name,@RollNo,@Class,@Section)", con);
  87. cmd.Parameters.AddWithValue("@RFID", txttag.Text);
  88. cmd.Parameters.AddWithValue("@Student_Name", txtstudname.Text);
  89. cmd.Parameters.AddWithValue("@RollNo", txtrollno.Text);
  90. cmd.Parameters.AddWithValue("@Section", combosec.SelectedItem.ToString());
  91. cmd.Parameters.AddWithValue("@Class", comboclass.SelectedItem.ToString());
  92. cmd.ExecuteNonQuery();
  93. MessageBox.Show("Student added");
  94. con.Close();
  95. }
  96. private bool CheckDuplicateRFID()
  97. {
  98. SqlConnection con = new SqlConnection(str);
  99. con.Open();
  100. SqlCommand cmd = new SqlCommand("select * from tbl_Studentdet where RFID='" + txttag.Text + "'", con);
  101. SqlDataReader rd = cmd.ExecuteReader();
  102. if (rd.HasRows)
  103. {
  104. rd.Close();
  105. return false;
  106. }
  107. else
  108. {
  109. rd.Close();
  110. return true;
  111. }
  112. }
  113. }
  114. }
Output(While showing the card In to the RFID reader).

Output