C# Corner
Tech
News
Videos
Forums
Trainings
Books
Events
More
Interviews
Jobs
Live
Learn
Career
Members
Blogs
Challenges
Certifications
Bounties
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
RFID Programming
WhatsApp
Vinodh Narayanan
10y
194.2
k
0
0
25
Blog
Here am shows how to read RFID data and store information in to database
Form1.designer.cs
Code:
namespace
AMS
{
partial
class
Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private
System.ComponentModel.IContainer components =
null
;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected
override
void
Dispose(
bool
disposing)
{
if
(disposing && (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
.txttag =
new
System.Windows.Forms.TextBox();
this
.label1 =
new
System.Windows.Forms.Label();
this
.label2 =
new
System.Windows.Forms.Label();
this
.label3 =
new
System.Windows.Forms.Label();
this
.label4 =
new
System.Windows.Forms.Label();
this
.label5 =
new
System.Windows.Forms.Label();
this
.txtstudname =
new
System.Windows.Forms.TextBox();
this
.txtrollno =
new
System.Windows.Forms.TextBox();
this
.comboclass =
new
System.Windows.Forms.ComboBox();
this
.combosec =
new
System.Windows.Forms.ComboBox();
this
.btnsubmit =
new
System.Windows.Forms.Button();
this
.btncancel =
new
System.Windows.Forms.Button();
this
.SuspendLayout();
//
// txttag
//
this
.txttag.Location =
new
System.Drawing.Point(138, 50);
this
.txttag.Name =
"txttag"
;
this
.txttag.Size =
new
System.Drawing.Size(100, 20);
this
.txttag.TabIndex = 0;
//
// label1
//
this
.label1.AutoSize =
true
;
this
.label1.Location =
new
System.Drawing.Point(60, 50);
this
.label1.Name =
"label1"
;
this
.label1.Size =
new
System.Drawing.Size(40, 13);
this
.label1.TabIndex = 1;
this
.label1.Text =
"Tag ID"
;
//
// label2
//
this
.label2.AutoSize =
true
;
this
.label2.Location =
new
System.Drawing.Point(44, 78);
this
.label2.Name =
"label2"
;
this
.label2.Size =
new
System.Drawing.Size(75, 13);
this
.label2.TabIndex = 2;
this
.label2.Text =
"Student Name"
;
//
// label3
//
this
.label3.AutoSize =
true
;
this
.label3.Location =
new
System.Drawing.Point(60, 108);
this
.label3.Name =
"label3"
;
this
.label3.Size =
new
System.Drawing.Size(42, 13);
this
.label3.TabIndex = 3;
this
.label3.Text =
"Roll No"
;
//
// label4
//
this
.label4.AutoSize =
true
;
this
.label4.Location =
new
System.Drawing.Point(60, 135);
this
.label4.Name =
"label4"
;
this
.label4.Size =
new
System.Drawing.Size(32, 13);
this
.label4.TabIndex = 4;
this
.label4.Text =
"Class"
;
//
// label5
//
this
.label5.AutoSize =
true
;
this
.label5.Location =
new
System.Drawing.Point(57, 163);
this
.label5.Name =
"label5"
;
this
.label5.Size =
new
System.Drawing.Size(43, 13);
this
.label5.TabIndex = 5;
this
.label5.Text =
"Section"
;
//
// txtstudname
//
this
.txtstudname.Location =
new
System.Drawing.Point(138, 78);
this
.txtstudname.Name =
"txtstudname"
;
this
.txtstudname.Size =
new
System.Drawing.Size(100, 20);
this
.txtstudname.TabIndex = 6;
//
// txtrollno
//
this
.txtrollno.Location =
new
System.Drawing.Point(138, 105);
this
.txtrollno.Name =
"txtrollno"
;
this
.txtrollno.Size =
new
System.Drawing.Size(100, 20);
this
.txtrollno.TabIndex = 7;
//
// comboclass
//
this
.comboclass.FormattingEnabled =
true
;
this
.comboclass.Items.AddRange(
new
object
[] {
"I"
,
"II"
,
"III"
,
"IV"
,
"V"
,
"VI"
,
"VII"
,
"VIII"
,
"IX"
,
"X"
});
this
.comboclass.Location =
new
System.Drawing.Point(138, 132);
this
.comboclass.Name =
"comboclass"
;
this
.comboclass.Size =
new
System.Drawing.Size(121, 21);
this
.comboclass.TabIndex = 8;
//
// combosec
//
this
.combosec.FormattingEnabled =
true
;
this
.combosec.Items.AddRange(
new
object
[] {
"A"
,
"B"
,
"C"
,
"D"
,
"E"
,
"F"
,
"G"
,
"H"
});
this
.combosec.Location =
new
System.Drawing.Point(138, 160);
this
.combosec.Name =
"combosec"
;
this
.combosec.Size =
new
System.Drawing.Size(121, 21);
this
.combosec.TabIndex = 9;
//
// btnsubmit
//
this
.btnsubmit.Location =
new
System.Drawing.Point(105, 202);
this
.btnsubmit.Name =
"btnsubmit"
;
this
.btnsubmit.Size =
new
System.Drawing.Size(75, 23);
this
.btnsubmit.TabIndex = 10;
this
.btnsubmit.Text =
"Submit"
;
this
.btnsubmit.UseVisualStyleBackColor =
true
;
this
.btnsubmit.Click +=
new
System.EventHandler(
this
.btnsubmit_Click);
//
// btncancel
//
this
.btncancel.Location =
new
System.Drawing.Point(197, 202);
this
.btncancel.Name =
"btncancel"
;
this
.btncancel.Size =
new
System.Drawing.Size(75, 23);
this
.btncancel.TabIndex = 11;
this
.btncancel.Text =
"Cancel"
;
this
.btncancel.UseVisualStyleBackColor =
true
;
//
// Form1
//
this
.AutoScaleDimensions =
new
System.Drawing.SizeF(6F, 13F);
this
.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this
.ClientSize =
new
System.Drawing.Size(290, 261);
this
.Controls.Add(
this
.btncancel);
this
.Controls.Add(
this
.btnsubmit);
this
.Controls.Add(
this
.combosec);
this
.Controls.Add(
this
.comboclass);
this
.Controls.Add(
this
.txtrollno);
this
.Controls.Add(
this
.txtstudname);
this
.Controls.Add(
this
.label5);
this
.Controls.Add(
this
.label4);
this
.Controls.Add(
this
.label3);
this
.Controls.Add(
this
.label2);
this
.Controls.Add(
this
.label1);
this
.Controls.Add(
this
.txttag);
this
.Name =
"Form1"
;
this
.Text =
"Form1"
;
this
.Load +=
new
System.EventHandler(
this
.Form1_Load);
this
.ResumeLayout(
false
);
this
.PerformLayout();
}
#endregion
private
System.Windows.Forms.TextBox txttag;
private
System.Windows.Forms.Label label1;
private
System.Windows.Forms.Label label2;
private
System.Windows.Forms.Label label3;
private
System.Windows.Forms.Label label4;
private
System.Windows.Forms.Label label5;
private
System.Windows.Forms.TextBox txtstudname;
private
System.Windows.Forms.TextBox txtrollno;
private
System.Windows.Forms.ComboBox comboclass;
private
System.Windows.Forms.ComboBox combosec;
private
System.Windows.Forms.Button btnsubmit;
private
System.Windows.Forms.Button btncancel;
}
}
Screen Design:
Form1.cs
Code:
Adding namespaces for serial port connection.
using
System.IO.Ports;
using
System.Threading;
using
System.Data.Sql;
using
System.Data.SqlClient;
Full Code:
using
System.IO.Ports;
using
System.Threading;
using
System.Data.Sql;
using
System.Data.SqlClient;
Full Code:
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.IO.Ports;
using
System.Threading;
using
System.Data.Sql;
using
System.Data.SqlClient;
namespace
AMS
{
public
partial
class
Form1 : Form
{
string
str =
"Data Source=.;Initial Catalog=AMS;Integrated Security=True"
;
private
SerialPort RFID;
private
string
DispString;
public
Form1()
{
InitializeComponent();
}
private
void
Form1_Load(
object
sender, EventArgs e)
{
RFID =
new
SerialPort();
RFID.PortName =
"COM5"
;
RFID.BaudRate = 9600;
RFID.DataBits = 8;
RFID.Parity = Parity.None;
RFID.StopBits = StopBits.One;
RFID.Open();
RFID.ReadTimeout = 200;
if
(RFID.IsOpen)
{
DispString =
""
;
txttag.Text =
""
;
}
else
{
RFID.Close();
}
RFID.DataReceived +=
new
SerialDataReceivedEventHandler(RFID_DataReceived);
}
private
void
button1_Click(
object
sender, EventArgs e)
{
}
private
void
RFID_DataReceived(
object
sender, SerialDataReceivedEventArgs e)
{
if
(txttag.Text.Length >= 12)
{
RFID.Close();
}
else
{
DispString = RFID.ReadExisting();
this
.Invoke(
new
EventHandler(DisplayText));
}
}
private
void
DisplayText(
object
sender, EventArgs e)
{
txttag.AppendText(DispString);
}
private
void
btnsubmit_Click(
object
sender, EventArgs e)
{
switch
(btnsubmit.Text)
{
case
"Submit"
:
if
(txttag.Text.Length == 0 || txtrollno.Text.Length == 0 || txtstudname.Text.Length == 0)
{
return
; }
if
(!CheckDuplicateRFID())
{
MessageBox.Show(
"Already Saved"
);
txttag.Text =
""
;
return
;
}
break
;
}
SqlConnection con =
new
SqlConnection(str);
con.Open();
SqlCommand cmd =
new
SqlCommand(
"insert into tbl_Studentdet(RFID,Student_Name,RollNo,Class,Section) values (@RFID,@Student_Name,@RollNo,@Class,@Section)"
, con);
cmd.Parameters.AddWithValue(
"@RFID"
, txttag.Text);
cmd.Parameters.AddWithValue(
"@Student_Name"
, txtstudname.Text);
cmd.Parameters.AddWithValue(
"@RollNo"
, txtrollno.Text);
cmd.Parameters.AddWithValue(
"@Section"
, combosec.SelectedItem.ToString());
cmd.Parameters.AddWithValue(
"@Class"
, comboclass.SelectedItem.ToString());
cmd.ExecuteNonQuery();
MessageBox.Show(
"Student added"
);
con.Close();
}
private
bool
CheckDuplicateRFID()
{
SqlConnection con =
new
SqlConnection(str);
con.Open();
SqlCommand cmd =
new
SqlCommand(
"select * from tbl_Studentdet where RFID='"
+ txttag.Text +
"'"
, con);
SqlDataReader rd = cmd.ExecuteReader();
if
(rd.HasRows)
{
rd.Close();
return
false
;
}
else
{
rd.Close();
return
true
;
}
}
}
}
Output(While showing the card In to the RFID reader).
People also reading
Membership not found