MessageBox.Show不适用于我的Visual Studio

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MessageBox.Show不适用于我的Visual Studio相关的知识,希望对你有一定的参考价值。

您好,我正在为pos软件的产品库存系统键入代码。我正在做类似这里的事情:

Click here查看我的产品清单草图。

并且在单击保存按钮以获取数据库结果后,它应显示为“已添加记录...”窗口。而且即使我在Visual Studio Pro软件上看不到任何错误,也看不到结果。

并且请让我知道哪一部分不对以及如何解决。

这是我的salesn.cs文件的代码

using mysql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace superpos3

    public partial class salesn : Form
    
        public salesn()
        
            InitializeComponent();
        

        int totalPrice = 0;

        MySqlConnection con = new MySqlConnection("server= localhost; database =superpos;  username= root; password=; ");
        private void richTextBox1_TextChanged(object sender, EventArgs e)
        

        

        private void txtno_KeyPress(object sender, KeyPressEventArgs e)
        
            if (e.KeyChar == 13)
            
                txtqty.Enabled = true;
                txtqty.Focus();
            
        

        private void txtqty_KeyPress(object sender, KeyPressEventArgs e)
        
            if (e.KeyChar == 13)
            

                try
                

                    string txt = "select * from products where id='" + txtno.Text + "'";
                    MySqlCommand cmd = new MySqlCommand(txt, con);
                    con.Open();
                    MySqlDataReader r = cmd.ExecuteReader();
                    while (r.Read())
                    
                        int price = int.Parse(txtqty.Text.ToString()) * int.Parse(r[4].ToString());
                        totalPrice = totalPrice + price;
                        //discount
                        // totalPrice = totalPrice - totalPrice* Payment.discount/100;

                        dataGridView1.Rows.Add(dataGridView1.RowCount, r[0], r[1], txtqty.Text.Trim(), r[4], price);

                    
                    lbitems.Text = " " + (dataGridView1.RowCount - 1) + "";
                    lbtotal.Text = " " + totalPrice + " ";

                    con.Close();

                






                catch (Exception ee)
                
                    MessageBox.Show(ee.Message, "Error From Database");
                

                txtno.Focus();
                txtno.Clear();

                txtqty.Enabled = false;
                txtqty.Clear();

            

        


        private void txtqty_TextChanged(object sender, EventArgs e)
        

        

        private void salesn_Load(object sender, EventArgs e)
        
            lbldate.Text = DateTime.Today.ToString("dd/MM/yyyy");
            lbltime.Text = DateTime.Now.ToShortTimeString();

            con.Open();
            string query = "select max(id) from salesmain ";
            MySqlCommand cmd2 = new MySqlCommand(query, con);
            MySqlDataReader dr;
            dr = cmd2.ExecuteReader();
            if (dr.Read())
            
                string val = dr[0].ToString();
                if (val == "")
                
                    lbinvoice.Text = "1";

                
                else
                
                    int a;

                    a = int.Parse(dr[0].ToString());
                    a = a + 1;
                    lbinvoice.Text = a.ToString();



                
                con.Close();
            

        

        private void button1_Click(object sender, EventArgs e)
        
            try
            

                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                

                    MySqlCommand cmd = new MySqlCommand();
                    cmd.Connection = con;

                    cmd.CommandText = "Insert into salesproducts(saleid,productname,qty,price,grosstotal)values(@salesid,@productname,@qty,@price,@grosstotal)";
                    cmd.Parameters.AddWithValue("@saleid", lbinvoice.Text);
                    cmd.Parameters.AddWithValue("@productname", dataGridView1.Rows[i].Cells[2].Value);
                    cmd.Parameters.AddWithValue("@qty", dataGridView1.Rows[i].Cells[3].Value);
                    cmd.Parameters.AddWithValue("@price", dataGridView1.Rows[i].Cells[4].Value);
                    cmd.Parameters.AddWithValue("@grosstotal", dataGridView1.Rows[i].Cells[5].Value);

                    MySqlCommand cmd1 = new MySqlCommand();
                    cmd1.Connection = con;
                    cmd1.CommandText = "insert into salesmain(id,date,time,qty,grosstotal)values(@id,@date,@time,@qty,@grosstotal)";
                    cmd1.Parameters.AddWithValue("@id", lbinvoice.Text);
                    cmd1.Parameters.AddWithValue("@date", lbldate.Text);
                    cmd1.Parameters.AddWithValue("@time", lbltime.Text);
                    cmd1.Parameters.AddWithValue("@qty", lbitems.Text);

                    cmd1.Parameters.AddWithValue("@grosstotal", lbtotal.Text);

                    con.Open();
                    cmd.ExecuteNonQuery();
                    cmd1.ExecuteNonQuery();
                    MessageBox.Show("Record added ..........");
                    con.Close();












                
               
            catch (Exception ex)
            
                //MessageBox.Show(ex.Message);
            

            finally
            
                con.Close();
            
           
          

答案

我想您应该检查dataGridView1.Rows.Count是否不为零,因为如果为零,则for循环甚至不会运行,这意味着您的代码和消息框show方法都不会运行。

其他MessageBox.Show()方法在catch块内部,这显然意味着,如果catch块未捕获任何错误,它们将不会运行。

以上是关于MessageBox.Show不适用于我的Visual Studio的主要内容,如果未能解决你的问题,请参考以下文章

您好,“messagebox.show弹出框不显示”这个问题您解决了吗?

当用户输入整数时,MessageBox.Show 不会出现

didSelectRowAt 不适用于我的 UITableViewController

为啥我的 strassen 算法不适用于 3x3 矩阵?

为啥我的 NYC 代码覆盖范围不适用于 ESM?

为啥我的模拟器不适用于 NativeScript?