Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

         private void btnCus_save_Click(object sender, EventArgs e)
         {
             string stockoutID;

             config.singleResult("SELECT concat(STRT,END) FROM tblautonumber WHERE ID = 5");
             stockoutID = config.dt.Rows[0].Field(0);

             if (txt_cusid.Text == "")
             {
                 MessageBox.Show("There are empty fields left that must be fill up!", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                 return;
             }
             if(dtCus_addedlist.RowCount == 0)
             {
                 MessageBox.Show("Cart is empty!", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                 return;
             }

             sql = "SELECT ITEMID,`QTY` FROM `tblitems`";
             config.singleResult(sql);

             foreach(DataRow row in config.dt.Rows)
             {
                 for(int i = 0;i < dtCus_addedlist.Rows.Count; i++)
                 {
                     if (dtCus_addedlist.Rows[i].Cells[0].Value.ToString() == row.Field(0))
                     {
                         if(int.Parse( dtCus_addedlist.Rows[i].Cells[4].Value.ToString()) > row.Field(1))
                         {
                             MessageBox.Show("The Quantity of the item ( " + dtCus_addedlist.Rows[i].Cells[1].Value.ToString() + " ) is greater than the available quantity of it.", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                             return;
                         }
                     }
                     if (dtCus_addedlist.Rows[i].Cells[4].Value.ToString() == "")
                     {
                         MessageBox.Show("Set your purpose.", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                         return;
                     }
                 }
             }

             foreach(DataGridViewRow r in dtCus_addedlist.Rows)
             {
                 sql = "INSERT INTO `tblstock_in_out` ( `TRANSACTIONNUMBER`, `ITEMID`, `TRANSACTIONDATE`, `QTY`, `TOTALPRICE`, `SUPLIERCUSTOMERID`,REMARKS)" +
                 " VALUES ('" + stockoutID + "','" + r.Cells[0].Value + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "','" + r.Cells[4].Value +
                 "','" + r.Cells[5].Value + "','" + txt_cusid.Text + "','StockOut')";
                 config.Execute_Query(sql);

                 sql = "UPDATE `tblitems` SET `QTY`= QTY - '" + r.Cells[4].Value + "' WHERE ITEMID='" + r.Cells[0].Value + "'";
                 config.Execute_Query(sql);
             }


             sql = "INSERT INTO `tbltransaction` (`TRANSACTIONNUMBER`, `TRANSACTIONDATE`, `TYPE`, `SUPLIERCUSTOMERID`)" +
                    " VALUES ('" + stockoutID + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "','StockOut','" + txt_cusid.Text + "')";
             config.Execute_Query(sql);

             // '-----------------------------------------------update autonumber
             config.Execute_Query("UPDATE tblautonumber SET END= END + INCREMENT WHERE ID = 5");

             // '------------------------------------------------------------
             MessageBox.Show("Item(s) has been save in the database.");
             // '------------------------------------------------------------clearing
             funct.clearTxt(Panel1);
             dtCus_addedlist.Rows.Clear();

             frmStockOut_Load(sender, e);
         }