【问题标题】:How to Display Warning message if quantity is less than 5 items如果数量少于 5 件,如何显示警告消息
【发布时间】:2014-03-15 19:21:06
【问题描述】:

我正在创建一个库存系统,该系统应显示需要补货的产品。如果产品的数量只有 5 个,它应该会显示一条消息,说您现在需要补货。但我不知道该怎么做,因为我只创建库存系统而不是销售和库存,所以如果我没有销售系统,我应该如何扣除它?对不起,希望大家理解。

这是我的代码:

public void all()
{
        SqlConnection MySqlConnection;
        DataTable p_table = new DataTable();

        MySqlConnection = new SqlConnection("Data Source=christina\\sqlexpress;Initial Catalog=cafe_inventory;User ID=sa;Password=tina;");

        MySqlConnection.Open();
        SqlCommand command1 = new SqlCommand("Select * from inventory", MySqlConnection);

        //Clear the datatable to prevent duplicate generation of data in gridview.
        p_table.Clear();
        SqlDataAdapter m_da = new SqlDataAdapter("Select * from inventory", MySqlConnection);
        //DataSet ds = new DataSet();
        //DataTable dtable = ds.Tables["empinfo1"];
        m_da.Fill(p_table);
        // Clear the ListView control
        //listView3.Items.Clear();

        // Display items in the ListView control
        for (int i = 0; i < p_table.Rows.Count; i++)
        {
            DataRow drow = p_table.Rows[i];

            // Only row that have not been deleted
            if (drow.RowState != DataRowState.Deleted)
            {
                // Define the list items
                //(drow["bnum"].ToString());
                ListViewItem lvi = new ListViewItem(drow["pnum"].ToString());
                lvi.SubItems.Add(drow["pname"].ToString());
                lvi.SubItems.Add(drow["descr"].ToString());
                lvi.SubItems.Add(((DateTime)drow["dater"]).ToShortDateString());
                //lvi.SubItems.Add(drow["exp"].ToString());
                lvi.SubItems.Add(((DateTime)drow["exp"]).ToShortDateString());
                lvi.SubItems.Add(drow["qt"].ToString());

                // Add the list items to the ListView
                listView2.Items.Add(lvi);
            }
        }
    }

编辑代码:

public void stock()
    {

        SqlConnection MySqlConnection;
        DataTable p_table = new DataTable();

        MySqlConnection = new SqlConnection("Data Source=christina\\sqlexpress;Initial Catalog=cafe_inventory;User ID=sa;Password=tina;");

        MySqlConnection.Open();
        SqlCommand command1 = new SqlCommand("Select pname from inventory where qt < 5", MySqlConnection);

        //Clear the datatable to prevent duplicate generation of data in gridview.
        p_table.Clear();
        SqlDataAdapter m_da = new SqlDataAdapter("Select pname from inventory where qt < 5", MySqlConnection);
        m_da.Fill(p_table);

        SqlDataReader reader;
        reader = command1.ExecuteReader();

        StringBuilder productNames = new StringBuilder();

        while (reader.Read())
        {
            productNames.Append(reader["pname"].ToString() + Environment.NewLine);
        }

        MySqlConnection.Close();

        MessageBox.Show("There are products that needs restocking, check to restock now." + productNames);

        // Display items in the ListView control
        for (int i = 0; i < p_table.Rows.Count; i++)
        {

            DataRow drow = p_table.Rows[i];

            // Only row that have not been deleted
            if (drow.RowState != DataRowState.Deleted)
            {
                // Define the list items
                //ListViewItem lvi = new ListViewItem(drow["bnum"].ToString());
                ListViewItem lvi = new ListViewItem(drow["pnum"].ToString());
                lvi.SubItems.Add(drow["pname"].ToString());
                lvi.SubItems.Add(drow["descr"].ToString());
                lvi.SubItems.Add(((DateTime)drow["dater"]).ToShortDateString());
                //lvi.SubItems.Add(drow["exp"].ToString());
                lvi.SubItems.Add(((DateTime)drow["exp"]).ToShortDateString());
                lvi.SubItems.Add(drow["qt"].ToString());
                lvi.SubItems.Add(drow["interval"].ToString());

                // Add the list items to the ListView
                listView4.Items.Add(lvi);
            }
        }

    }

【问题讨论】:

  • 在下面查看我的答案
  • @SudhakarTillapudi 再次感谢您。但是真的有可能吗,因为我没有销售系统,所以没有什么可以扣除数量。我要做的是每次添加产品时,我都会将 5 作为数量。
  • 不客气 :),如果您的项目中没有销售系统,则无法扣除产品数量。所以现在它不会有用。但是,如果您在项目中实施了销售系统(每次销售都需要将数量减 1),那么它会对您有所帮助。
  • @SudhakarTillapudi 非常感谢!!! :) 我欠你很多。 :D
  • 没问题 :) 快乐编码 :)

标签: c# mysql


【解决方案1】:

试试这个:

void DisplayLowQuantityItems()
{
    MySqlConnection con = new MySqlConnection("Data Source=christina\\sqlexpress;
    Initial Catalog=cafe_inventory;User ID=sa;Password=tina;");
    MySqlCommand command = new MySqlCommand("Select pname from inventory 
                                                  where qt < 5", con);
    con.Open();
    MySqlDataReader reader = commad.ExecuteReader();
    StringBuilder productNames= new StringBuilder();
    while(reader.Read())
    {
        productNames.Append(reader["pname"].ToString()+Environment.NewLine);
    }
    con.Close();
    MessageBox.Show("Following Products quantity is lessthan 5\n"+productNames);
}

解决方案 2:

编辑代码:

public void stock()
    {

        MySqlConnection con;
        DataTable p_table = new DataTable();

        con = new MySqlConnection("Data Source=christina\\sqlexpress;Initial 
                             Catalog=cafe_inventory;User ID=sa;Password=tina;");

        con.Open();
        MySqlCommand command1 = new MySqlCommand("Select pname from inventory where
                                                         qt < 5", con);

        //Clear the datatable to prevent duplicate generation of data in gridview.
        p_table.Clear();
        MySqlDataAdapter m_da = new MySqlDataAdapter("Select * from inventory where
                                                          qt < 5", con);
        m_da.Fill(p_table);

        MySqlDataReader reader;
        reader = command1.ExecuteReader();

        StringBuilder productNames = new StringBuilder();

        while (reader.Read())
        {
            productNames.Append(reader["pname"].ToString() + Environment.NewLine);
        }

        con.Close();

        MessageBox.Show("There are products that needs restocking, 
                                            check to restock now." + productNames);

        // Display items in the ListView control
        for (int i = 0; i < p_table.Rows.Count; i++)
        {

            DataRow drow = p_table.Rows[i];

            // Only row that have not been deleted
            if (drow.RowState != DataRowState.Deleted)
            {
                // Define the list items
                //ListViewItem lvi = new ListViewItem(drow["bnum"].ToString());
                ListViewItem lvi = new ListViewItem(drow["pnum"].ToString());
                lvi.SubItems.Add(drow["pname"].ToString());
                lvi.SubItems.Add(drow["descr"].ToString());
                lvi.SubItems.Add(((DateTime)drow["dater"]).ToShortDateString());
                //lvi.SubItems.Add(drow["exp"].ToString());
                lvi.SubItems.Add(((DateTime)drow["exp"]).ToShortDateString());
                lvi.SubItems.Add(drow["qt"].ToString());
                lvi.SubItems.Add(drow["interval"].ToString());

                // Add the list items to the ListView
                listView4.Items.Add(lvi);
            }
        }

    }

【讨论】:

    【解决方案2】:

    另一种检查数据的方式

        DataTable p_table = new DataTable();
    
        con = new MySqlConnection("Data Source=christina\\sqlexpress;Initial 
                             Catalog=cafe_inventory;User ID=sa;Password=tina;");
    
        con.Open();
        MySqlCommand command1 = new MySqlCommand("Select pname from inventory where
                                                         qt < 5", con);
        p_table.load(command1.ExecuteReader());
    

    【讨论】:

      猜你喜欢
      • 2021-03-25
      • 1970-01-01
      • 2016-05-03
      • 1970-01-01
      • 1970-01-01
      • 2020-11-20
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多