LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 September 24 2015

scorz
Member

C#/mysql window problem

Hello, I am trying to write a small program in C# to store my stock's data into a mysql database.
I am using Window Form and using TextBox for I/O (I found it easier to the eyes than DataGrid since I won't be the only one to enter the data)

The problem I am facing is, when I insert a new row, and iterate in the database, the new entered rows won't show. I have to exit and run the program again.



This is the code for the "save" button (insert)

 
private void button7_Click(object sender, EventArgs e)
        {
            if (!pop)
            {
                MessageBox.Show("CLICK POPULATE FIRST", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                int vat;
                if (counter == count_product())
                {


                    if (checkBox1.Checked)
                    {
                        vat = 1;
                    }
                    else
                    {
                        vat = 0;

                    }
 try
                    {

                   
string save = "INSERT INTO `simba`.`product` (`Item_no`, `Description`, `Price`, `VAT`, `Quantity`, `Invoice_Number`, `Provider_Name`) VALUES(@ITEM, @DESC, @PRICE, @VAT,@QTY, @INVNUM, @PRONAME)";


                        database.OpenConnection();
                        MySqlCommand _save = new MySqlCommand(save, database.connection);
                        _save.Prepare();

                       _save.Parameters.AddWithValue("@ITEM", textBox1.Text);
                       _save.Parameters.AddWithValue("@DESC", richTextBox1.Text);
                       _save.Parameters.AddWithValue("@PRICE", float.Parse(textBox3.Text));
                       _save.Parameters.AddWithValue("@VAT", vat);
                       _save.Parameters.AddWithValue("@QTY", int.Parse(textBox5.Text));
                       _save.Parameters.AddWithValue("@INVNUM", textBox2.Text);
                       _save.Parameters.AddWithValue("@PRONAME", textBox4.Text);
                       _save.ExecuteNonQuery();
                       database.CloseConnection();

                    }
                    catch (MySql.Data.MySqlClient.MySqlException ex)
                    {
                        MessageBox.Show("Error " + ex.Number + " found: " + ex.Message,
                            "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                  }

              }

           }

       }

This is the code of the method I am using in the "NEXT" button:

 public void next()
        {

            if (!pop)
            {
                MessageBox.Show("CLICK POPULATE FIRST", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

            else
            {


                counter++; //inc for next row
                if (counter == count_product())
                {

                    MessageBox.Show("END OF DATABASE CLICK BACK TO CONTINUE");
                    counter = (count_product() - 1);
                }
                else
                {

                    textBox6.Text = (counter.ToString());
                    database.OpenConnection();
                    string select_all = "SELECT * from product";
                    MySqlDataAdapter _select = new MySqlDataAdapter();
                    _select.SelectCommand = new MySqlCommand(select_all, database.connection);
                    _select.Fill(_sel);
                    database.CloseConnection();
                    textBox1.Text = _sel.Rows[counter]["item_no"].ToString(); //item_no
                    richTextBox1.Text = _sel.Rows[counter]["description"].ToString(); //description
                    textBox3.Text = _sel.Rows[counter]["price"].ToString();//price
                    textBox2.Text = _sel.Rows[counter]["Invoice_number"].ToString();//invoice
                    textBox4.Text = _sel.Rows[counter]["provider_name"].ToString();//provider
                    textBox5.Text = _sel.Rows[counter]["quantity"].ToString();//quantity


                    if (_sel.Rows[counter]["VAT"].ToString() == "True")

                    {
                        checkBox1.Checked = true;

                    }
                    else
                    {
                        checkBox1.Checked = false;

                    }


                }

            }
        }

And this is the code of the method I am using for "BACK" button:

public void previous()
        {

            if (!pop)
            {
                MessageBox.Show("CLICK POPULATE FIRST", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            else
            {


                counter--;//dec for previous row
                if (counter < 0)
                {
                    MessageBox.Show("END OF DATABASE CLICK NEXT TO CONTINUE");
                    counter = 0;
                }
                else
                {
                    textBox6.Text = counter.ToString();
                    database.OpenConnection();
                    string select_all = "SELECT * from product";
                    MySqlDataAdapter _select = new MySqlDataAdapter();
                    _select.SelectCommand = new MySqlCommand(select_all, database.connection);
                    _select.Fill(_sel);
                    database.CloseConnection();
                    textBox1.Text = _sel.Rows[counter]["item_no"].ToString(); //item_no
                    richTextBox1.Text = _sel.Rows[counter]["description"].ToString(); //description
                    textBox3.Text = _sel.Rows[counter]["price"].ToString();//price
                    textBox2.Text = _sel.Rows[counter]["Invoice_number"].ToString();//invoice
                    textBox4.Text = _sel.Rows[counter]["provider_name"].ToString();//provider
                    textBox5.Text = _sel.Rows[counter]["quantity"].ToString();//quantity
                    if (_sel.Rows[counter]["VAT"].ToString() == "True")

                    {
                        checkBox1.Checked = true;

                    }
                    else
                    {
                        checkBox1.Checked = false;

                    }

                }
            }

        }

It's been a long time since I've coded so there may be some stupids mistakes I've made.
Any help/suggestion is appreciated.

Thanks!

Offline

Board footer