Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我会随时打电话给他的startConn(),这可能是你向他展示的活动。现在我毕业的时候有这个评论老师吗???哦,顺便说一句,他甚至不需要在任何地方调用startConn(),因为他在更新后已经调用了这个方法。所以他的问题很明显是他看不到数据网格中的变化。首_C#_Mysql_Windows Forms Designer - Fatal编程技术网

C# 我会随时打电话给他的startConn(),这可能是你向他展示的活动。现在我毕业的时候有这个评论老师吗???哦,顺便说一句,他甚至不需要在任何地方调用startConn(),因为他在更新后已经调用了这个方法。所以他的问题很明显是他看不到数据网格中的变化。首

C# 我会随时打电话给他的startConn(),这可能是你向他展示的活动。现在我毕业的时候有这个评论老师吗???哦,顺便说一句,他甚至不需要在任何地方调用startConn(),因为他在更新后已经调用了这个方法。所以他的问题很明显是他看不到数据网格中的变化。首,c#,mysql,windows-forms-designer,C#,Mysql,Windows Forms Designer,我会随时打电话给他的startConn(),这可能是你向他展示的活动。现在我毕业的时候有这个评论老师吗???哦,顺便说一句,他甚至不需要在任何地方调用startConn(),因为他在更新后已经调用了这个方法。所以他的问题很明显是他看不到数据网格中的变化。首先,如果你觉得被冒犯了,很抱歉:),我的意思是,我的朋友需要自动更新它,因此计时器将是一个很好的解决方案。另一方面,关于连接,我更愿意按照我在回答中的注释中告诉他的那样,在read方法中声明产品列表。调用startConn()计时器将帮助他更新


我会随时打电话给他的startConn(),这可能是你向他展示的活动。现在我毕业的时候有这个评论老师吗???哦,顺便说一句,他甚至不需要在任何地方调用startConn(),因为他在更新后已经调用了这个方法。所以他的问题很明显是他看不到数据网格中的变化。首先,如果你觉得被冒犯了,很抱歉:),我的意思是,我的朋友需要自动更新它,因此计时器将是一个很好的解决方案。另一方面,关于连接,我更愿意按照我在回答中的注释中告诉他的那样,在read方法中声明产品列表。调用startConn()计时器将帮助他更新网格,而无需实际单击事件。无论如何编辑您的答案,以便我也删除-1:)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        MySqlConnection conn = new MySqlConnection();       
        String connectionString = "Server=127.0.0.1; Database=mydatabase; Uid=root; Pwd=;";    
        List<products> listproducts = new List<products>();

        public string product;
        public string quantity;


        public Form1()
        {
            InitializeComponent();            
            startConn(); 
        }
        private void startConn()
        {
            try
            {   
                conn.ConnectionString = connectionString;
                conn.Open();                
                textBox3.Text= "Correct connection";
                //we call the function READ
                read();
            }
            catch (MySqlException)
            {
               textBox3.Text="An error has ocurred";
            }
        }       
       public void read()
        { 
            MySqlCommand instruccio = conn.CreateCommand();
            instruccio.CommandText = "Select * from products";
            MySqlDataReader search = instruccio.ExecuteReader();
            while (search.Read())
            {
                products prod = new products();
                prod.IdProd = search["idProd"].ToString();                    
                prod.Name = search["nomProd"].ToString();
                prod.Quantity = Int32.Parse(search["quantitat"].ToString());
                listproducts.Add(prod);
            }
            dataGridView1.DataSource = listproducts;

            search.Close();
            search.Dispose();

        }    
        private void btnEnviar_Click(object sender, EventArgs e) //Button Send (Enviar in spanish)
        {
           try
            {           
                //before updating the stock in database we query the total quantity of the product selected
                MySqlCommand instruccio1 = connexio.CreateCommand();              
                instruccio1.CommandText = "Select quantitat from productes where `nomProd`='"+ this.product +"'";               
                MySqlDataReader read = instruccio1.ExecuteReader();               
                int result = 0;               
                while (read.Read())
                {
                    resultat=Int32.Parse(read["quantitat"].ToString());                       
                }
                read.Dispose();
                instruccio1.Dispose();

                if (this.quantity != 0)
                {                   
                    if (result > this.quantity)
                    {

                    int difference = result - this.quantity;
                    MySqlCommand instruccio2 = conn.CreateCommand();
                    instruccio2.CommandText = "UPDATE products set `quantitat`='" + this.difference + "' where products.nomProd='" + this.product + "'";
                    instruccio2.ExecuteNonQuery();                                
                    conn.Close();
                                    startConn();
                    textBox1.Text= "";
                    textBox2.Text = "";
                    this.quantity = "";
                    this.product = "";                              
                }
                else 
                {
                    MessageBox.Show("There's no quantity.");                
                }               
            }
            catch (Exception xe)
            {
                MessageBox.Show("",xe.Message);
            }         
        }
        private void btnEsborrar_Click(object sender, EventArgs e) //Erase button
        {
            this.quantity = "";
            this.product = "";
            this.aEnviar = 0;
            textBox1.Text = quantity;
            textBox2.Text = product;
        }

....
....
dataGridView1.DataSource = listproducts;
dataGridView1.DataSource = null;