hi
in my c# winforms , am strucked at ,to send a record 3 times.
i have data as:$M121,responce(0/1/ anything)
if(responce==0)
//send record from excel
else if(responce==1)
//stop sending records
else
(for anything)
send the current record 3 times ,for each time need to check the responce whether it's 0/1/anything.
after 3 times if again responce is anything then skip that record sending,and continue with next record in excel.
here is the code:
private void Upload_Click(object sender, EventArgs e)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(textBox1.Text, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
int k = 100 / rowCount;
int i = 2;
PostCmdStart = 1;
rspCode=0;
if (rspCode == 0 )
{
PostCmdStart = 0;
for (int j = 1; j <= colCount; j++)
{
if (j == 1)
custId = xlRange.Cells[i, j].Value.ToString();
if (j == 2)
name = xlRange.Cells[i, j].Value.ToString();
if (j == 3)
totalBal = xlRange.Cells[i, j].Value.ToString();
if (j == 4)
paid = xlRange.Cells[i, j].Value.ToString();
if (j == 5)
lastPaid = xlRange.Cells[i, j].Value.ToString("dd/MM/yyyy");
if (j == 6)
due = xlRange.Cells[i, j].Value.ToString();
final = "$ POST," + textBox3.Text.ToString() + "," + i + "," + rowCount + "," + custId + "," + name + "," + totalBal + "," + paid + "," + lastPaid + "," + due + " # ";
}
if (serialPort1.IsOpen)
{
String machId = textBox3.Text.ToString();
if (machId.Length <= 5 && custId.Length <= 10 && name.Length <= 15 && totalBal.Length <= 7 && paid.Length <= 7 && lastPaid.Length <= 14 && due.Length <= 7)
serialPort1.Write(final);
}
progressBar1.Invoke((MethodInvoker)delegate
{
if (progressBar1.Value < 100)
progressBar1.Value += k;
});
}
Thread uploadThread = new Thread(new ThreadStart
(threadUpload));
uploadThread.Start();
}
public void threadUpload()
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(textBox1.Text, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
string[] lines;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
int k = 100 / rowCount;
int i = 3;
int count = 0;
run.WaitOne();
if (PostCmdStart==0)
{
s = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
lines = Regex.Split(s, "[$#,]");
rspCode=Convert.ToInt32(lines[2]);
}
if (rspCode == 0 && i <= rowCount+3)
{
PostCmdStart = 0;
for (int j = 1; j <= colCount; j++)
{
if (j == 1)
custId = xlRange.Cells[i, j].Value.ToString();
if (j == 2)
name = xlRange.Cells[i, j].Value.ToString();
if (j == 3)
totalBal = xlRange.Cells[i, j].Value.ToString();
if (j == 4)
paid = xlRange.Cells[i, j].Value.ToString();
if (j == 5)
lastPaid = xlRange.Cells[i, j].Value.ToString("dd/MM/yyyy");
if (j == 6)
due = xlRange.Cells[i, j].Value.ToString();
final = "$ POST," + textBox3.Text.ToString() + "," + i + "," + rowCount + "," + custId + "," + name + "," + totalBal + "," + paid + "," + lastPaid + "," + due + " # ";
}
if (serialPort1.IsOpen)
{
String machId = textBox3.Text.ToString();
if (machId.Length <= 5 && custId.Length <= 10 && name.Length <= 15 && totalBal.Length <= 7 && paid.Length <= 7 && lastPaid.Length <= 14 && due.Length <= 7)
serialPort1.Write(final);
}
progressBar1.Invoke((MethodInvoker)delegate
{
if (progressBar1.Value < 100)
progressBar1.Value += k;
});
rowno= i++;
}
else
if (rspCode == 1 && i <= rowCount)
return;
else
{
if (count == 3)
return;
count++;
i = rowno;
}
}