private void button2_Click(object sender, System.EventArgs e)
        {
            string zliczono = "0";

            SqlCeCommand cmd = cn.CreateCommand();

            cmd.CommandText = "SELECT kod, dokid, ilosc FROM bufor WHERE kod = ? and dokid = ?";
            cmd.Parameters.Add("@k", SqlDbType.NVarChar, 15);
            cmd.Parameters.Add("@d", SqlDbType.Int, 10);
            cmd.Parameters["@k"].Value = kodbuf;
            cmd.Parameters["@d"].Value = int.Parse(index);
            cmd.Prepare();
            SqlCeDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                zliczono = ((decimal.Parse(zliczono) + dr.GetSqlDecimal(2)).ToString());;
            }
            zliczono_t.Text = zliczono;
        }
        private void FindIndex()
        {
            string kodbuf   = kod_t.Text;
            int    wagaflag = 0;
            string czywag   = kodbuf.Substring(0, 2);
            string waga     = "";
            string kodwag   = "";
            string kodwag2  = "";

            if (czywag == "27" || czywag == "28" || czywag == "29")
            {
                if (kodbuf.Length == 13)
                {
                    waga     = kodbuf.Substring(kodbuf.Length - 6, 5);
                    kodwag   = kodbuf.Substring(0, 6);
                    kodwag2  = kodbuf.Substring(2, 4);
                    wagaflag = 1;
                }
            }

            //int rowqty = 0;
            kod_t.Text = "SZUKAM TOWARU W BAZIE";
            kod_t.Refresh();
            //SqlCeCommand cmd2 = cn.CreateCommand();
            //cmd2.CommandText = "SELECT kod, COUNT(nazwa) FROM dane WHERE kod = ? GROUP BY kod";
            //cmd2.Parameters.Add("@k", SqlDbType.NVarChar, 20);
            //cmd2.Parameters["@k"].Value = kodbuf;
            //cmd2.Prepare();
            //SqlCeDataReader dr1 = cmd2.ExecuteReader();

            //while (dr1.Read())
            //{
            //	rowqty = dr1.GetInt32(1);
            //}

            //if (rowqty > 0)
            //{
            if (wagaflag == 0)
            {
                SqlCeCommand cmd = cn.CreateCommand();
                cmd.CommandText = "SELECT kod, nazwa, stan, cenazk, cenasp, vat FROM dane WHERE kod = ?";
                cmd.Parameters.Add("@k", SqlDbType.NVarChar, 20);
                cmd.Parameters["@k"].Value = kodbuf;

                cmd.Prepare();
                SqlCeDataReader dr = cmd.ExecuteReader();


                while (dr.Read())
                {
                    nazwa_t.Text  = dr.GetString(1);
                    stan_t.Text   = dr.GetString(2);
                    cena_t.Text   = dr.GetString(3);
                    cenasp_t.Text = dr.GetString(4);
                    vat_t.Text    = dr.GetString(5);
                }
                cmd.Dispose();
                dr.Dispose();
                string zliczono = "0";
                cmd = cn.CreateCommand();

                cmd.CommandText = "SELECT kod, dokid, ilosc FROM bufor WHERE kod = ? and dokid = ?";
                cmd.Parameters.Add("@k", SqlDbType.NVarChar, 15);
                cmd.Parameters.Add("@d", SqlDbType.Int, 10);
                cmd.Parameters["@k"].Value = kodbuf;
                cmd.Parameters["@d"].Value = int.Parse(index);
                cmd.Prepare();
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    zliczono = ((decimal.Parse(zliczono) + dr.GetSqlDecimal(2)).ToString());;
                }
                zliczono_t.Text = zliczono;
                kod_t.Text      = kodbuf;
                ilosc_t.Focus();
            }
            else if (wagaflag == 1)
            {
                string       like = "kod LIKE '" + kodwag + ".......'";
                SqlCeCommand cmd  = cn.CreateCommand();
                cmd.CommandText = "SELECT kod, nazwa, stan, cenazk, cenasp, vat FROM dane WHERE " + like;
                cmd.Prepare();
                SqlCeDataReader dr = cmd.ExecuteReader();


                while (dr.Read())
                {
                    nazwa_t.Text  = dr.GetString(1);
                    stan_t.Text   = dr.GetString(2);
                    cena_t.Text   = dr.GetString(3);
                    cenasp_t.Text = dr.GetString(4);
                    vat_t.Text    = dr.GetString(5);
                    ilosc_t.Text  = (int.Parse(waga.Substring(0, 2))).ToString() + "." + waga.Substring(2, 3);
                }
                cmd.Dispose();
                dr.Dispose();
                string zliczono = "0";
                cmd = cn.CreateCommand();

                cmd.CommandText = "SELECT kod, dokid, ilosc FROM bufor WHERE dokid = ? and " + like;
                cmd.Parameters.Add("@d", SqlDbType.Int, 10);
                cmd.Parameters["@d"].Value = int.Parse(index);
                cmd.Prepare();
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    zliczono = ((decimal.Parse(zliczono) + dr.GetSqlDecimal(2)).ToString());;
                }
                zliczono_t.Text = zliczono;
                kod_t.Text      = kodwag;
                ilosc_t.Focus();
            }
            if (nazwa_t.Text == null || nazwa_t.Text == "")
            {
                DialogResult dialog = MessageBox.Show("Nie znaleziono kodu towaru czy? dodaæ - Tak, dodaæ bez nazwy - Anuluj, Nie dodawaæ - Nie", "Brak towaru", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (dialog == DialogResult.Yes)
                {
                    kod_t.Text       = kodbuf;
                    nazwa_t.ReadOnly = false;
                    cena_t.ReadOnly  = false;
                    nazwa_t.Focus();
                    cena_t.Text   = "0";
                    cenasp_t.Text = "0";
                    stan_t.Text   = "0";
                    vat_t.Text    = "0";
                    if (wagaflag == 1)
                    {
                        ilosc_t.Text = waga.Substring(0, 2) + "." + waga.Substring(2, 3);
                    }
                }
                else if (dialog == DialogResult.No)
                {
                    kod_t.Text = null;
                    kod_t.Focus();
                }
                else if (dialog == DialogResult.Cancel)
                {
                    nazwa_t.ReadOnly = true;
                    cena_t.ReadOnly  = true;
                    kod_t.Text       = kodbuf;
                    if (wagaflag == 1)
                    {
                        ilosc_t.Text = waga.Substring(0, 2) + "." + waga.Substring(2, 3);
                    }
                    ilosc_t.Focus();
                    cena_t.Text   = "0";
                    cenasp_t.Text = "0";
                    stan_t.Text   = "0";
                    vat_t.Text    = "0";
                }
            }
        }
        public Form4(int rownumber, int dokrow, int licence)
        {
            //
            // Required for Windows Form Designer support
            //
            lic    = licence;
            rownum = dokrow;
            InitializeComponent();
            this.Height = Screen.PrimaryScreen.Bounds.Height;
            this.Width  = Screen.PrimaryScreen.Bounds.Width;
            Update();
            rownumb = rownumber;
            string connectionString;

            connectionString = "DataSource=Baza.sdf; Password=matrix1";
            SqlCeConnection cn = new SqlCeConnection(connectionString);

            cn.Open();

            SqlCeDataAdapter da    = new SqlCeDataAdapter("SELECT * FROM dok", cn);
            DataTable        table = new DataTable();

            da.Fill(table);
            index = table.Rows[rownum][0].ToString();
            SqlCeDataAdapter db     = new SqlCeDataAdapter("SELECT * FROM bufor", cn);
            DataTable        table2 = new DataTable();

            db.SelectCommand = new SqlCeCommand("SELECT * FROM bufor WHERE dokid =  ?", cn);
            db.SelectCommand.Parameters.Add("@k", SqlDbType.Int, 10);
            db.SelectCommand.Parameters["@k"].Value = int.Parse(index);
            db.SelectCommand.ExecuteNonQuery();
            db.Fill(table2);
            index2 = table2.Rows[rownumb][0].ToString();
            SqlCeCommand cmd = cn.CreateCommand();

            cmd.CommandText = "SELECT kod, nazwa, cenazk, ilosc, stan, cenasp, vat FROM bufor WHERE id = ?";
            cmd.Parameters.Add("@k", SqlDbType.Int, 10);
            cmd.Parameters["@k"].Value = int.Parse(index2);
            cmd.Prepare();
            SqlCeDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                kod_t.Text    = dr.GetString(0);
                nazwa_t.Text  = dr.GetString(1);
                cena_t.Text   = dr.GetString(2);
                ilosc_t.Text  = Convert.ToString(dr.GetSqlDecimal(3));
                stan_t.Text   = dr.GetString(4);
                cenasp_t.Text = dr.GetString(5);
                vat_t.Text    = dr.GetString(6);
            }


            string       index3 = table2.Rows[rownumb][2].ToString();
            SqlCeCommand cmd1   = cn.CreateCommand();

            cmd1.CommandText = "SELECT kod, dokid, SUM(ilosc) AS zliczono FROM bufor GROUP BY kod, dokid HAVING kod = ? and dokid = ?";
            cmd1.Parameters.Add("@k", SqlDbType.NVarChar, 15);
            cmd1.Parameters.Add("@d", SqlDbType.Int, 10);
            cmd1.Parameters["@k"].Value = index3;
            cmd1.Parameters["@d"].Value = int.Parse(index);
            cmd1.Prepare();
            dr = cmd1.ExecuteReader();

            while (dr.Read())
            {
                zliczono_t.Text = Convert.ToString(dr.GetSqlDecimal(2));
            }


            ilosc_t.Focus();
            ilosc_t.SelectAll();
            cn.Close();



            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }
        private void FindIndex()
        {
            string kodbuf   = kod_t.Text;
            int    wagaflag = 0;
            string czywag   = kodbuf.Substring(0, 2);
            string waga     = "";
            string kodwag   = "";
            string kodwag2  = "";

            if (czywag == "27" || czywag == "28" || czywag == "29")
            {
                if (kodbuf.Length == 13)
                {
                    waga     = kodbuf.Substring(kodbuf.Length - 6, 5);
                    kodwag   = kodbuf.Substring(0, 6);
                    kodwag2  = kodbuf.Substring(2, 4);
                    wagaflag = 1;
                }
            }

            //int rowqty = 0;
            kod_t.Text = "SZUKAM TOWARU W BAZIE";
            kod_t.Refresh();
            //SqlCeCommand cmd2 = cn.CreateCommand();
            //cmd2.CommandText = "SELECT kod, COUNT(nazwa) FROM dane WHERE kod = ? GROUP BY kod";
            //cmd2.Parameters.Add("@k", SqlDbType.NVarChar, 20);
            //cmd2.Parameters["@k"].Value = kodbuf;
            //cmd2.Prepare();
            //SqlCeDataReader dr1 = cmd2.ExecuteReader();

            //while (dr1.Read())
            //{
            //	rowqty = dr1.GetInt32(1);
            //}

            //if (rowqty > 0)
            //{
            if (wagaflag == 0)
            {
                SqlCeCommand cmd = cn.CreateCommand();
                cmd.CommandText = "SELECT kod, nazwa, stan, cenazk, cenasp, vat, bad_cena, bad_stan, zliczono, cenapolka FROM dane WHERE kod = ?";
                cmd.Parameters.Add("@k", SqlDbType.NVarChar, 20);
                cmd.Parameters["@k"].Value = kodbuf;

                cmd.Prepare();
                SqlCeDataReader dr = cmd.ExecuteReader();


                while (dr.Read())
                {
                    nazwa_t.Text       = dr.GetString(1);
                    stan_t.Text        = dr.GetString(2);
                    cenazk_t.Text      = dr.GetString(3);
                    cenasp_t.Text      = dr.GetString(4);
                    vat_t.Text         = dr.GetString(5);
                    bad_cena_c.Checked = dr.GetBoolean(6);
                    bad_stan_c.Checked = dr.GetBoolean(7);
                    ilosc_t.Text       = Convert.ToString(dr.GetSqlDecimal(8));
                    cena_t.Text        = Convert.ToString(dr.GetSqlDecimal(9));
                }
                cmd.Dispose();
                dr.Dispose();


                kod_t.Text = kodbuf;
                //	ilosc_t.Focus();
            }
            else if (wagaflag == 1)
            {
                string       like = "kod LIKE '" + kodwag + ".......'";
                SqlCeCommand cmd  = cn.CreateCommand();
                cmd.CommandText = "SELECT kod, nazwa, stan, cenazk, cenasp, vat, bad_cena, bad_stan, zliczono, cenapolka FROM dane WHERE " + like;
                cmd.Prepare();
                SqlCeDataReader dr = cmd.ExecuteReader();


                while (dr.Read())
                {
                    nazwa_t.Text       = dr.GetString(1);
                    stan_t.Text        = dr.GetString(2);
                    cena_t.Text        = dr.GetString(3);
                    cenasp_t.Text      = dr.GetString(4);
                    vat_t.Text         = dr.GetString(5);
                    bad_cena_c.Checked = dr.GetBoolean(6);
                    bad_stan_c.Checked = dr.GetBoolean(7);
                    ilosc_t.Text       = Convert.ToString(dr.GetSqlDecimal(8));
                    cena_t.Text        = Convert.ToString(dr.GetSqlDecimal(9));
                }
                cmd.Dispose();
                dr.Dispose();


                kod_t.Text = kodwag;
                //	ilosc_t.Focus();
            }
            if (nazwa_t.Text == null || nazwa_t.Text == "")
            {
                DialogResult dialog = MessageBox.Show("Nie znaleziono kodu towaru");
                kod_t.Focus();
            }
        }