Example #1
0
        private void button5_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                DialogResult result = folderBrowserDialog1.ShowDialog();
                if (result == DialogResult.OK)
                {
                    WebApplication1.Class1.USBSerialNumber usb = new WebApplication1.Class1.USBSerialNumber();
                    string path = folderBrowserDialog1.SelectedPath;
                    string serial = usb.getSerialNumberFromDriveLetter(path.Remove(path.Length - 1));

                    SqlConnection connection = new SqlConnection(connectionString);
                    connection.Open();
                    SqlCommand command = connection.CreateCommand();
                    SqlDataReader reader;

                    command.CommandText = "SELECT * FROM farmer where uuid=@p3";
                    command.Prepare();

                    command.Parameters.AddWithValue("@p3", listView1.SelectedItems[0].Text);
                    reader = command.ExecuteReader();

                    string farmer_serial = "";
                    if (reader.Read())
                    {
                        farmer_serial = reader["usb"].ToString();
                    }

                    reader.Close();

                    if (serial == farmer_serial)
                    {
                        //string key = "12";

                        command.Parameters.Clear();

                        command.CommandText = "UPDATE farmer SET units= units + @p1 WHERE uuid=@p2";
                        command.Prepare();
                        command.Parameters.AddWithValue("@p1", Convert.ToInt32(numericUpDown1.Text));
                        command.Parameters.AddWithValue("@p2", listView1.SelectedItems[0].Text);
                        command.ExecuteNonQuery();

                        command.Parameters.Clear();

                        command.CommandText = "SELECT * FROM farmer where uuid=@p3";
                        command.Prepare();

                        command.Parameters.AddWithValue("@p3", listView1.SelectedItems[0].Text);
                        reader = command.ExecuteReader();

                        string test = "";
                        string text = "";
                        string text1 = "";
                        int units = 0;
                        int uuid = 0;
                        try
                        {
                            test = File.ReadLines(folderBrowserDialog1.SelectedPath + "units.txt", Encoding.Default).First().Trim();
                            //MessageBox.Show(test);
                            if (reader.Read())
                            {
                                if (test != "")
                                {
                                    //char[] array3 = test.ToCharArray();
                                    //char[] array4 = key.ToCharArray();
                                    //char[] array5 = WebApplication1.Class1.XORENC(test);
                                    string test1 = WebApplication1.Class1.XORENC(test);

                                    text = test1.Substring(0, test1.IndexOf(","));
                                    text1 = test1.Substring(test1.IndexOf(" "));

                                    text = text + "," + reader["units"].ToString() + "," + reader["uuid"].ToString() + text1;
                                    //DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") 
                                    units = Convert.ToInt32(reader["units"]);
                                    uuid = Convert.ToInt32(reader["uuid"]);
                                }
                                else
                                {
                                    text = "NA," + reader["units"].ToString() + "," + reader["uuid"].ToString() + " ";

                                    units = Convert.ToInt32(reader["units"]);
                                    uuid = Convert.ToInt32(reader["uuid"]);
                                }
                            }

                            reader.Close();
                           
                            //char[] array = text.ToCharArray();
                            //key = "12";
                            //char[] array1 = key.ToCharArray();
                            //char[] array2 = WebApplication1.Class1.XORENC(array, array1);
                            string encrypt = WebApplication1.Class1.XORENC(text);

                            File.WriteAllText(folderBrowserDialog1.SelectedPath + "units.txt", encrypt, Encoding.Default);
                            

                            fillFarmer();

                            command.Parameters.Clear();

                            command.CommandText = "INSERT into fill (uuid,date,adding,units) values (@p1,@p2,@p3,@p4)";
                            command.Prepare();
                            command.Parameters.AddWithValue("@p1", uuid);
                            command.Parameters.AddWithValue("@p2", DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"));
                            command.Parameters.AddWithValue("@p3", "+" + numericUpDown1.Text);
                            command.Parameters.AddWithValue("@p4", units);
                            command.ExecuteNonQuery();

                            MessageBox.Show("Η Προσθήκη Μονάδων στο USB ολοκληρώθηκε");
                            connection.Close();
                    }
                        catch (Exception)
                    {
                        if (reader.Read())
                        {
                            text = "NA," + reader["units"].ToString() + "," + reader["uuid"].ToString() + " ";
                                //char[] array = text.ToCharArray();
                                //key = "12";
                                //char[] array1 = key.ToCharArray();
                                //char[] array2 = WebApplication1.Class1.XORENC(text);
                                string encrypt = WebApplication1.Class1.XORENC(text);

                                units = Convert.ToInt32(reader["units"]);
                                uuid = Convert.ToInt32(reader["uuid"]);

                                reader.Close();

                                File.WriteAllText(folderBrowserDialog1.SelectedPath + "units.txt", encrypt, Encoding.Default);

                                fillFarmer();

                                command.Parameters.Clear();

                                command.CommandText = "INSERT into fill (uuid,date,adding,units) values (@p1,@p2,@p3,@p4)";
                                command.Prepare();
                                command.Parameters.AddWithValue("@p1", uuid);
                                command.Parameters.AddWithValue("@p2", DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"));
                                command.Parameters.AddWithValue("@p3", "+" + numericUpDown1.Text);
                                command.Parameters.AddWithValue("@p4", units);
                                command.ExecuteNonQuery();

                                MessageBox.Show("Η Προσθήκη Μονάδων στο USB ολοκληρώθηκε");
                                connection.Close();
                            }

                        }
                }
                    else
                    {
                        MessageBox.Show("Το Serial Number του αγρότη και του Usb δεν ταιριάζουν");
                    }
                    
                }
            }
            else
            {
                MessageBox.Show("Επιλέξτε έναν αγρότη");
            }
        }
Example #2
0
        private void usb_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                WebApplication1.Class1.USBSerialNumber usb1 = new WebApplication1.Class1.USBSerialNumber();
                string path = folderBrowserDialog1.SelectedPath;
                string serial = usb1.getSerialNumberFromDriveLetter(path.Remove(path.Length - 1));

                if (serial != null)
                {
                    SqlConnection connection = new SqlConnection(connectionString);
                    connection.Open();
                    SqlCommand command = connection.CreateCommand();
                    SqlDataReader reader;

                    command.CommandText = "SELECT * FROM farmer where usb=@p3";
                    command.Prepare();

                    command.Parameters.AddWithValue("@p3", serial);
                    reader = command.ExecuteReader();

                    File.Create(path + "farmer.txt").Close();

                    if (reader.Read())
                    {
                        MessageBox.Show("Το συγκεκριμένο Usb χρησιμοποιείται από άλλο αγρότη");
                        File.Delete(path + "farmer.txt");
                    }
                    else
                    {
                        usb.Text = serial;
                    }

                    reader.Close();
                    connection.Close();
                }
                else
                {
                    MessageBox.Show("Η διαδρομή που επιλέξατε δεν είναι σωστή");
                }
            }
        }