TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
kiat kiat
NA
1
0
Modify C# program to sort repeated item for point of sales
Jul 20 2009 4:38 AM
Hi, is there anyone who can help me to modify a program regarding point of sales checkout system. Right now this program has the only the general feature and i need to to modify it to sort the number of repeated item. I have written a comment in purple next to the method. Thanks....! The source code is as below
using
System;
using
System.Collections.Generic;
using
System.Collections;
using
System.ComponentModel;
using
System.Data;
using
System.Data.SqlClient;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
namespace
PointofSales
{
public
partial
class
FrmMain
:
Form
{
// database connection string for SQLServer 2005
private
string
dbConn =
"Data Source=SE\\sqlexpress;Initial Catalog=PointOfSales;Trusted_Connection=True"
;
private
string
ReadCommand =
"SELECT id, antenna_id, read_count, protocol_id FROM tag_id WHERE protocol_id='GEN2' set time_out=1000;"
;
private
ReaderMode
readermode =
ReaderMode
.Read;
// add a delegate & an event
public
delegate
void
TCPDataUpdateHandler
(
object
sender,
TCPEventArgs
e);
public
event
TCPDataUpdateHandler
TCPDataUpdated;
private
enum
ReaderMode
{
Read,
Write
}
public
FrmMain()
{
InitializeComponent();
InitApp();
// select thing magic reader
axInRFIDCtrl1.SelectThingMagic();
axInRFIDCtrl1.ReaderIPAddress =
"10.0.0.101"
;
axInRFIDCtrl1.ReaderTCPPort =
"8080"
;
}
private
void
exitToolStripMenuItem_Click(
object
sender,
EventArgs
e)
{
this
.Close();
}
private
void
aboutToolStripMenuItem_Click(
object
sender,
EventArgs
e)
{
MessageBox
.Show(
"RFID Point of Sales by Intensecomp Pte Ltd. Developed in Year 2009"
);
}
private
void
addNewProductToolStripMenuItem_Click(
object
sender,
EventArgs
e)
{
FrmAddProduct
frm =
new
FrmAddProduct
(dbConn);
if
(frm.ShowDialog() ==
DialogResult
.OK)
{
// process return value from dialog
}
}
// initialize application
private
void
InitApp()
{
LoadInventoryData(dbConn);
lstOrder.Clear();
lstOrder.Columns.Add(
"ID"
, 50);
lstOrder.Columns.Add(
"Description"
, 100);
lstOrder.Columns.Add(
"Price"
, 80);
lstOrder.Columns.Add(
"Quantity"
, 70);
btnAddOrder.Enabled =
false
;
btnRegister.Enabled =
false
;
}
// add item on list view
private
void
LoadInventoryData(
string
sqlConn)
{
// establish connection to SQLServer
System.Data.SqlClient.
SqlConnection
conn =
new
System.Data.SqlClient.
SqlConnection
();
conn.ConnectionString = sqlConn;
// prepare sql statement
string
sql =
string
.Empty;
sql =
"select TagId, ProductName as 'Product Name', Description, UnitPrice As 'Price' from Tag, Product where ProductId = Product.Id"
;
SqlDataReader
rdr =
null
;
try
{
// Open the connection
conn.Open();
SqlCommand
cmd =
new
SqlCommand
(sql, conn);
// get query results
rdr = cmd.ExecuteReader();
lstData.Clear();
for
(
int
j = 0; j < rdr.FieldCount; j++)
{
lstData.Columns.Add(rdr.GetName(j), rdr.GetName(j).Length * 2 + 150);
}
// print the CustomerID of each record
int
k = 1;
while
(rdr.Read())
{
ListViewItem
lst =
new
ListViewItem
(rdr[0].ToString());
for
(
int
i = 1; i < rdr.FieldCount; i++)
{
// if column is unit price, convert from int to decimal.
if
(i == 3)
{
double
money =
Convert
.ToDouble(rdr[i].ToString());
money = money / 100;
lst.SubItems.Add(money.ToString(
"$##.00"
));
}
else
lst.SubItems.Add(rdr[i].ToString());
}
lstData.Items.Add(lst);
k++;
}
}
catch
(
Exception
ex)
{
throw
new
Exception
(
"Exception occur at AssetInfo.GetAll()-->"
+ ex.Message);
}
finally
{
// close the reader
if
(rdr !=
null
)
{
rdr.Close();
}
// Close the connection
if
(conn !=
null
)
{
conn.Close();
}
}
}
// add new order.
private
void
btnAddOrder_Click(
object
sender,
EventArgs
e)
//click on scan button to call this method to scan
{
for
(
int
x = 0; x < 5; x++)
{
// read from RFID tag
string
tagid =
string
.Empty;
// send data to Reader
axInRFIDCtrl1.TMagicSend(ReadCommand);
}
}
public
string
GetTagData(
string
tagid)
{
string
result =
string
.Empty;
System.Data.SqlClient.
SqlConnection
conn =
new
System.Data.SqlClient.
SqlConnection
();
conn.ConnectionString = dbConn;
SqlDataReader
rdr =
null
;
try
{
// Open the connection
conn.Open();
// Pass the connection to a command object
string
sql =
string
.Empty;
sql =
"select Tag.TagID, ProductName as 'Product Name', UnitPrice As Price from Tag,Product where ProductId = Product.Id and Tag.TagID = '"
+ tagid +
"'"
;
SqlCommand
cmd =
new
SqlCommand
(sql, conn);
// get query results
rdr = cmd.ExecuteReader();
while
(rdr.Read())
{
result = rdr[0].ToString() +
"|"
+ rdr[1].ToString() +
"|"
+ rdr[2].ToString();
}
}
catch
(
Exception
ex)
{
throw
new
Exception
(
"Exception occur at GetEventList()-->"
+ ex.Message);
}
finally
{
// close the reader
if
(rdr !=
null
)
{
rdr.Close();
}
// Close the connection
if
(conn !=
null
)
{
conn.Close();
}
}
return
result;
}
public
int
GetProductId(
string
tagid)
{
int
result = 0;
System.Data.SqlClient.
SqlConnection
conn =
new
System.Data.SqlClient.
SqlConnection
();
conn.ConnectionString = dbConn;
SqlDataReader
rdr =
null
;
try
{
// Open the connection
conn.Open();
// Pass the connection to a command object
string
sql =
string
.Empty;
sql =
"select ProductId from Tag where Tag.TagID = '"
+ tagid +
"'"
;
SqlCommand
cmd =
new
SqlCommand
(sql, conn);
// get query results
rdr = cmd.ExecuteReader();
while
(rdr.Read())
{
result =
Convert
.ToInt32(rdr[0]);
}
}
catch
(
Exception
ex)
{
throw
new
Exception
(
"Exception occur at GetEventList()-->"
+ ex.Message);
}
finally
{
// close the reader
if
(rdr !=
null
)
{
rdr.Close();
}
// Close the connection
if
(conn !=
null
)
{
conn.Close();
}
}
return
result;
}
public
int
GetLastOrderId()
{
int
result = 0;
System.Data.SqlClient.
SqlConnection
conn =
new
System.Data.SqlClient.
SqlConnection
();
conn.ConnectionString = dbConn;
SqlDataReader
rdr =
null
;
try
{
// Open the connection
conn.Open();
// Pass the connection to a command object
string
sql =
string
.Empty;
sql =
"select Max(Id) from OrderInfo"
;
SqlCommand
cmd =
new
SqlCommand
(sql, conn);
// get query results
rdr = cmd.ExecuteReader();
while
(rdr.Read())
{
result =
Convert
.ToInt32(rdr[0]);
}
}
catch
(
Exception
ex)
{
throw
new
Exception
(
"Exception occur at GetEventList()-->"
+ ex.Message);
}
finally
{
// close the reader
if
(rdr !=
null
)
{
rdr.Close();
}
// Close the connection
if
(conn !=
null
)
{
conn.Close();
}
}
return
result;
}
// add new product item to the inventory
private
void
btnRegister_Click(
object
sender,
EventArgs
e)
{
// select a product and assign to RFID tag
FrmRegister
frm =
new
FrmRegister
(
this
, dbConn, axInRFIDCtrl1);
if
(frm.ShowDialog() ==
DialogResult
.OK)
{
LoadInventoryData(dbConn);
}
}
private
void
btnClear_Click(
object
sender,
EventArgs
e)
{
lstOrder.Items.Clear();
}
private
void
startReaderToolStripMenuItem_Click(
object
sender,
EventArgs
e)
{
if
(axInRFIDCtrl1.TMagicConnect() > 0)
{
//axInRFIDCtrl1.ISOSet15693();
MessageBox
.Show(
"RFID Reader ready for operation"
);
btnAddOrder.Enabled =
true
;
btnRegister.Enabled =
true
;
}
else
MessageBox
.Show(
"RFID Reader not found"
);
}
private
void
stopReaderToolStripMenuItem_Click(
object
sender,
EventArgs
e)
{
axInRFIDCtrl1.TMagicDisconnect();
MessageBox
.Show(
"RFID Reader has been stopped"
);
btnAddOrder.Enabled =
false
;
btnRegister.Enabled =
false
;
}
private
void
btnRefresh_Click(
object
sender,
EventArgs
e)
{
LoadInventoryData(dbConn);
}
private
void
btnPay_Click(
object
sender,
EventArgs
e)
{
// nothing in the list, do not process
if
(lstOrder.Items.Count <= 0)
return
;
// calculate payment summary
int
tprice = 0;
for
(
int
j=0; j< lstOrder.Items.Count; j++)
{
tprice +=
Convert
.ToInt32((
Convert
.ToDouble(lstOrder.Items[j].SubItems[2].Text.Replace(
"$"
,
""
)) * 100));
}
frmPayment
frm =
new
frmPayment
(tprice);
if
(frm.ShowDialog() ==
DialogResult
.OK)
{
// process the order, create order first
int
orderid = CreateOrder(dbConn);
if
(orderid > 0)
{
orderid = GetLastOrderId();
for
(
int
i = 0; i < lstOrder.Items.Count; i++)
{
int
price = 0;
string
tagid = lstOrder.Items[i].Text;
price =
Convert
.ToInt32((
Convert
.ToDouble(lstOrder.Items[i].SubItems[2].Text.Replace(
"$"
,
""
)) * 100));
int
productid = 0;
productid = GetProductId(tagid);
int
oid = InsertOrderDetails(dbConn, orderid, tagid, price, productid);
if
(oid > 0)
{
// save completed, remove item from inventory
RemoveInventory(dbConn, tagid);
}
}
lstOrder.Items.Clear();
// update inventory record
LoadInventoryData(dbConn);
}
}
}
private
int
CreateOrder(
string
sqlconn)
{
int
result = 0;
// setup connection
System.Data.SqlClient.
SqlConnection
conn =
new
System.Data.SqlClient.
SqlConnection
();
conn.ConnectionString = sqlconn;
try
{
// Open the connection
conn.Open();
// Pass the connection to a command object
string
sql =
string
.Empty;
sql =
"INSERT ORDERINFO (fPaid) VALUES (1)"
;
SqlCommand
cmd =
new
SqlCommand
(sql, conn);
// execute cmd with non query
result = cmd.ExecuteNonQuery();
}
catch
(
Exception
ex)
{
throw
new
Exception
(
"Exception occur at GetEventList()-->"
+ ex.Message);
}
finally
{
// Close the connection
if
(conn !=
null
)
0 {
conn.Close();
}
}
return
result;
}
private
int
InsertOrderDetails(
string
sqlconn,
int
orderid,
string
tagid,
int
price,
int
productid)
{
int
result = 0;
System.Data.SqlClient.
SqlConnection
conn =
new
System.Data.SqlClient.
SqlConnection
();
conn.ConnectionString = sqlconn;
try
{
// Open the connection
conn.Open();
// Pass the connection to a command object
string
sql =
string
.Empty;
sql =
"INSERT ORDERDETAILS (OrderId, TagId, UnitPrice, ProductId) VALUES ("
+ orderid.ToString() +
",'"
+ tagid +
"',"
+ price.ToString() +
","
+ productid +
")"
;
SqlCommand
cmd =
new
SqlCommand
(sql, conn);
result = cmd.ExecuteNonQuery();
}
catch
(
Exception
ex)
{
throw
new
Exception
(
"Exception occur at GetEventList()-->"
+ ex.Message);
}
finally
{
// Close the connection
if
(conn !=
null
)
{
conn.Close();
}
}
return
result;
}
private
int
RemoveInventory(
string
sqlconn,
string
tagid)
{
int
result = 0;
System.Data.SqlClient.
SqlConnection
conn =
new
System.Data.SqlClient.
SqlConnection
();
conn.ConnectionString = sqlconn;
try
{
// Open the connection
conn.Open();
// Pass the connection to a command object
string
sql =
string
.Empty;
sql =
"DELETE FROM Tag where TagId = '"
+ tagid +
"'"
;
SqlCommand
cmd =
new
SqlCommand
(sql, conn);
result = cmd.ExecuteNonQuery();
}
catch
(
Exception
ex)
{
throw
new
Exception
(
"Exception occur at GetEventList()-->"
+ ex.Message);
}
finally
{
// Close the connection
if
(conn !=
null
)
{
conn.Close();
}
}
return
result;
}
private
void
viewOrderHistoryToolStripMenuItem_Click(
object
sender,
EventArgs
e)
{
frmViewOrder
frm =
new
frmViewOrder
(dbConn);
if
(frm.ShowDialog() ==
DialogResult
.OK)
{
}
}
private
void
viewProductListToolStripMenuItem_Click(
object
sender,
EventArgs
e)
{
frmViewProduct
frm =
new
frmViewProduct
(dbConn);
if
(frm.ShowDialog() ==
DialogResult
.OK)
{
}
}
// reply from the reader
this should be the part
where i need to write a method to sort repeated item.
private
void
axInRFIDCtrl1_DataReceived(
object
sender, AxINRFIDCTRLLib.
_DInRFIDCtrlEvents_DataReceivedEvent
e)
{
string
msg =
string
.Empty;
try
{
// data received from reader
// e
msg = e.data;
// process
msg = msg.TrimEnd(
'\n'
);
msg = msg.TrimStart(
'\n'
);
/*
if (msg.Length <= 0)
{
UpdateLabel(lblLog, "No tag available", UpdateType.New);
if (readermode == ReaderMode.Write)
{
Thread.Sleep(1000);
ReadTag(writeantenna);
}
else
return;
}
*/
// update text box
//UpdateTextBox(tbxRawData, System.DateTime.Now.ToString() + " " + msg + "\r\n", UpdateType.Add);
//WriteProcessLog(msg, true);
int
readcount = 0;
string
[] tag = msg.Split(
'\n'
);
switch
(readermode)
{
case
ReaderMode
.Read:
if
(msg.Length > 0)
{
if
(msg.IndexOf(
"Error"
) <= 0)
{
for
(
int
i = 0; i < tag.Length; i++)
{
string
[] data = tag[i].Split(
'|'
);
readcount =
Convert
.ToInt32(data[2]);
string
result =
string
.Empty;
string
tagid = data[0].Remove(data[0].Length - 4, 4);
// get data from database by tag id
result = GetTagData(tagid);
if
(result !=
""
)
{
string
[] res = result.Split(
'|'
);
int
count = 0;
/*
// search for tagid if already in the list, then ignore
for (int j = 0; j < lstOrder.Items.Count; j++)
{
if (lstOrder.Items[j].Text == tagid)
{
count++;
break;
}
}
if (count <= 0)
{
// convert price from int to decimal
res[2] = (Convert.ToDouble(res[2]) / 100).ToString("$##.00");
ListViewItem lst = new ListViewItem(res);
lstOrder.Items.Add(lst);
}
*/
res[2] = (
Convert
.ToDouble(res[2]) / 100).ToString(
"$##.00"
);
// update list box
// consolidate the tags scanned
ListViewItem
lst =
new
ListViewItem
(res);
UpdateListView(lstOrder, lst);
}
else
{
//MessageBox.Show("Error getting tag data. Tag may not exist in inventory");
// instance the event args and pass it each value
TCPEventArgs
args =
new
TCPEventArgs
(tagid);
if
(TCPDataUpdated !=
null
)
TCPDataUpdated(
new
object
(), args);
}
}
// update here for qty
}
else
{
//UpdateLabel(lblLog, msg, UpdateType.New);
}
//UpdateLabel(lblLog, "Read completed", UpdateType.New);
}
break
;
case
ReaderMode
.Write:
/*
if (msg.Length > 0)
{
if (tag.Length == 1)
{
if (msg.IndexOf("Error") <= 0)
{
string[] data = tag[0].Split('|');
if (completecount >= writecount)
{
//update list view
ListViewItem item = new ListViewItem(msg);
UpdateListView(lstData, item);
UpdateLabel(lblLog, "Write operation completed.", UpdateType.New);
UpdateButtonText(btnWrite, "Write", UpdateType.New);
}
else
{
// check for previous tag
string prevtagid = String.Format("0x{0:X24}", prewriteID);
string comparetag = data[0].Remove(data[0].Length - 4, 4);
WriteProcessLog(prevtagid + " " + comparetag, true);
if (comparetag == prevtagid)
{
//update list view
ListViewItem item = new ListViewItem(msg);
UpdateListView(lstData, item);
UpdateLabel(lblLog, "Put a new tag to retry. Writing ID: " + writeID.ToString(), UpdateType.New);
Thread.Sleep(2000);
ReadTag(writeantenna);
}
else
{
string tagid = String.Format("0x{0:X24}", writeID);
prewriteID = writeID; // store previous write id
InsertTagInfo(tagid);
WriteTag(writeID, writeantenna);
writeID++;
completecount++;
UpdateLabel(lblLog, "Write " + tagid + " successful", UpdateType.New);
Thread.Sleep(500);
}
}
}
else
{
UpdateLabel(lblLog, msg, UpdateType.New);
{
Thread.Sleep(1000);
ReadTag(writeantenna);
}
}
}
else
{
UpdateLabel(lblLog, "Too many tags detected", UpdateType.New);
WriteProcessLog("Too mang tags - " + msg, true);
Thread.Sleep(1000);
ReadTag(writeantenna);
}
}
else
{
UpdateLabel(lblLog, "No reply from reader", UpdateType.New);
Thread.Sleep(1000);
ReadTag(writeantenna);
}
*/
break
;
}
}
catch
(
Exception
ex)
{
MessageBox
.Show(ex.Message, ex.Source);
}
}
private
void
FrmMain_FormClosing(
object
sender,
FormClosingEventArgs
e)
{
axInRFIDCtrl1.TMagicDisconnect();
}
// this function updates listview item if available, add new item if not available.
private
void
UpdateListView(
ListView
control,
ListViewItem
item)
{
if
(control.InvokeRequired)
{
MethodInvoker
invoker =
new
MethodInvoker
(
delegate
()
{
if
(control.Items.Count > 0)
{
ListViewItem
foundItem = control.FindItemWithText(item.Text,
true
, 0,
true
);
if
(foundItem ==
null
)
{
control.Items.Add(item);
}
else
{
/*
// update read count, subitem no 2
if (foundItem.SubItems.Count >= 3 && item.SubItems.Count >= 3)
{
foundItem.SubItems[2].Text = Convert.ToString(Convert.ToInt32(foundItem.SubItems[2].Text) + Convert.ToInt32(item.SubItems[2].Text));
}
*/
}
}
else
control.Items.Add(item);
});
Invoke(invoker);
}
else
{
if
(control.Items.Count > 0)
{
ListViewItem
foundItem = control.FindItemWithText(item.Text,
true
, 0,
true
);
if
(foundItem ==
null
)
{
control.Items.Add(item);
}
else
{
/*
// update read count, subitem no 2
foundItem.SubItems[2].Text = Convert.ToString(Convert.ToInt32(foundItem.SubItems[2].Text) + Convert.ToInt32(item.SubItems[2].Text));
*/
}
}
else
control.Items.Add(item);
}
}
private
void
RemoveItemListView(
ListView
control,
string
item)
{
if
(control.InvokeRequired)
{
MethodInvoker
invoker =
new
MethodInvoker
(
delegate
()
{
// remove client from list view
for
(
int
i = 0; i < control.Items.Count; i++)
{
ListViewItem
it = control.Items[i];
if
(it.SubItems[0].Text == item)
{
control.Items.RemoveAt(i);
break
;
}
}
});
Invoke(invoker);
}
else
{
// remove client from list view
for
(
int
i = 0; i < control.Items.Count; i++)
{
ListViewItem
it = control.Items[i];
if
(it.SubItems[0].Text == item)
{
control.Items.RemoveAt(i);
break
;
}
}
}
}
}
public
class
TCPEventArgs
: System.
EventArgs
{
private
string
mMachineId =
string
.Empty;
private
string
mPacket =
string
.Empty;
private
string
mCommand =
string
.Empty;
public
TCPEventArgs(
string
packet)
{
mPacket = packet;
}
public
string
MachineId
{
get
{
return
mMachineId; }
}
public
string
Packet
{
get
{
return
mPacket; }
}
public
string
CommandCode
{
get
{
return
mCommand; }
}
}
}
Reply
Answers (
0
)
Read write xml
shortcut icon for customise file extension (help needed)