Click here to Skip to main content
15,916,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
  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;
           // progressBar1.Visible = true;

           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)
                      // machId = xlRange.Cells[i, j].Value.ToString();


                   //upload1 = xlRange.Cells[i, j].Value.ToString();
                   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");
                   //MessageBox.Show("lastpaid:"+lastPaid);

                   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();
        //   run.Set();


          // MessageBox.Show("current thread:"+Thread.CurrentContext);
        // MessageBox.Show("current thread123:" + Thread.CurrentThread);
           //run.WaitOne();
           //run.Set();

       }
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;
          // progressBar1.Visible = true;

           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)
                       // machId = xlRange.Cells[i, j].Value.ToString();


                       //upload1 = xlRange.Cells[i, j].Value.ToString();
                       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");
                       //MessageBox.Show("lastpaid:"+lastPaid);

                       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
                   {//here i need to send the current record 3 times each time check the responce
                       if (count == 3)
                           return;
                       count++;

                       i = rowno;


                   }   // progressBar1.Invoke((MethodInvoker)delegate { progressBar1.Value += 100; });


       }
Posted

1 solution

it's simple set 1 counter and check how much time it sent
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900