Skip to content

How to get rfid value and store the database in asp.net

Kavi Mithran

Asked Sep 22, 2016, 7:13 PM EDT1 answer
Share
LinkedInX
any one help me, how to get & add rfid value into database

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    1. Form1.designer.cs

      Code:

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