峰峦@叠嶂 2024-05-23 11:04 采纳率: 97.6%
浏览 4
已结题

为空点按钮,增加容错提示功能

以下代码,如果什么都不做,只是空点一下,就会提示“未将对象引入到对应的实例”,能否优化一下,以增加容错,实现,若是空点该“保存”按钮,就提示,“请您修改后,再做保存”;


   private void btnmodify_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("server=192.168.100.247;database=WHMesInfo;user=sa;password=whyy@2021");

            conn.Open();
            try
            {
                
                // 遍历每一行数据,并进行保存
                for (int i = 0; i < resultTable.Rows.Count; i++)
                {
                    if (comprondate.Text == "请选择")//先判断汇总日期是否选择正常日期了,正常再继续执行;
                    {
                        MessageBox.Show("请您选择汇总日期");

                        return;
                    }

                    if (resultTable.Rows[i].RowState == DataRowState.Deleted)
                    {
                        continue; // 跳过已删除的行
                    }

                    else
                    {

                        //DateTime date = Convert.ToDateTime(resultTable.Rows[i]["date"]); //标准做法,用数据表名resultTable来定义
                        //DateTime sumdate = Convert.ToDateTime(comprondate.Text);
                        string sumdate = comprondate.Text;
                        string batchNo = Convert.ToString(resultTable.Rows[i]["batchNo"]);
                        //string probatchNo = Convert.ToString(resultTable.Rows[i]["probatchNo"]);
                        string proname = Convert.ToString(resultTable.Rows[i]["proname"]);
                        string unit = Convert.ToString(resultTable.Rows[i]["unit"]);
                        string teams = Convert.ToString(resultTable.Rows[i]["teams"]);
                        string process = Convert.ToString(resultTable.Rows[i]["process"]);
                        string spec = Convert.ToString(resultTable.Rows[i]["spec"]);
                        decimal ulh = Convert.ToDecimal(resultTable.Rows[i]["ulh"]);
                        int instock = Convert.ToInt32(resultTable.Rows[i]["instock"]);
                        int sample = Convert.ToInt32(resultTable.Rows[i]["sample"]);
                        int checkNum = Convert.ToInt32(resultTable.Rows[i][9]);
                        
                        decimal redquan;//1217,增加了“折合数量”的计算和数据库字段;
                        if (dataGridView1.Rows[i].Cells["折合数量"].Value.ToString() != string.Empty)
                            redquan = Convert.ToDecimal(dataGridView1.Rows[i].Cells["折合数量"].Value);
                        else
                            redquan = 0;
                        
                        decimal uniPrice = Convert.ToDecimal(resultTable.Rows[i][10]);//因之前减少了产品批号列,20231217,将列由11改为了10;;
                      
                        decimal proSalary = Convert.ToDecimal(resultTable.Rows[i]["产量薪资"]);

                        decimal proReward, packReward;
                        if (dataGridView1.Rows[i].Cells["出成奖励"].Value.ToString() != string.Empty)
                            proReward = Convert.ToDecimal(dataGridView1.Rows[i].Cells["出成奖励"].Value);
                        else
                            proReward = 0;
                        if (dataGridView1.Rows[i].Cells["包材奖励"].Value.ToString() != string.Empty)
                            packReward = Convert.ToDecimal(dataGridView1.Rows[i].Cells["包材奖励"].Value.ToString());
                        else
                            packReward = 0;

                        decimal toSalary = Convert.ToDecimal(resultTable.Rows[i]["总薪资"]);
                        decimal dayPrice = Convert.ToDecimal(resultTable.Rows[i]["工日单价"]);
                        decimal boxWorkhours = Convert.ToDecimal(resultTable.Rows[i]["万盒工时"]);


                        //以下检验插与数据库内的内容,是否有重复,无重复,便插入;

                        string str = @"select count(*) from w_protran where dateinfo='" + sumdate.ToString() + "' and batchNo='" + batchNo.ToString() + "' and proname='" + proname.ToString() + "' and teams='" + teams.ToString() + "' and process='" + process.ToString()
                            + "'and checkNum='" + checkNum.ToString() + "'and toSalary='" + toSalary.ToString() + "'";


                        SqlCommand com = new SqlCommand(str, conn);
                        int intcont = Convert.ToInt32(com.ExecuteScalar());

                        if (intcont > 0)//判断数据库中是否有相同的内容;
                        {
                            MessageBox.Show("对不起!新增内容,和历史记录有重复,请检查确认!");

                            return;
                        }
                        else
                        {

                            string strsql = @"INSERT INTO [dbo].[w_protran]
                            ([dateInfo],[batchNo],[proname],[unit],[teams],[process],[spec],[ulh],[instock],[sample],[checkNum],[redquantity],[uniPrice],[proSalary],[proReward],[packReward],[toSalary],[dayPrice],[boxWorkhours])
                            VALUES (@dateInfo, @batchNo, @proname, @unit, @teams, @process, @spec, @ulh, @instock, @sample,@checkNum,@redquantity, @uniPrice, @proSalary,@proReward,@packReward,@toSalary,@dayPrice,@boxWorkhours)";

                            using (SqlCommand comm = new SqlCommand(strsql, conn))
                            {
                                comm.Parameters.AddWithValue("@dateInfo", sumdate);
                                comm.Parameters.AddWithValue("@batchNo", batchNo);
                                //comm.Parameters.AddWithValue("@probatchNo", probatchNo);
                                comm.Parameters.AddWithValue("@proname", proname);
                                comm.Parameters.AddWithValue("@unit", unit);
                                comm.Parameters.AddWithValue("@teams", teams);
                                comm.Parameters.AddWithValue("@process", process);
                                comm.Parameters.AddWithValue("@spec", spec);
                                comm.Parameters.AddWithValue("@ulh", ulh);
                                comm.Parameters.AddWithValue("@instock", instock);
                                comm.Parameters.AddWithValue("@sample", sample);
                                comm.Parameters.AddWithValue("@checkNum", checkNum);

                                comm.Parameters.AddWithValue("@redquantity", redquan);
                                comm.Parameters.AddWithValue("@uniPrice", uniPrice);
                                comm.Parameters.AddWithValue("@proSalary", proSalary);
                                comm.Parameters.AddWithValue("@proReward", proReward);
                                comm.Parameters.AddWithValue("@packReward", packReward);
                                comm.Parameters.AddWithValue("@toSalary", toSalary);
                                comm.Parameters.AddWithValue("@dayPrice", dayPrice);
                                comm.Parameters.AddWithValue("@boxWorkhours", boxWorkhours);

                                int rowsAffected = comm.ExecuteNonQuery();
                                if (rowsAffected <= 0)
                                {
                                    MessageBox.Show("数据修改失败!");
                                    return;
                                }
                            }
                        }
                    }
                }
                    MessageBox.Show("数据修改成功!");
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据修改出错:" + ex.Message);
            }

            finally { conn.Close(); }
        } 
  • 写回答

2条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-05-23 11:04
    关注

    引自免费微信小程序:皆我百晓生

    在您的代码中,可以通过添加额外的检查来确保用户对表格中的某些关键字段进行了更改。这里是一个改进后的版本,它会检查是否有任何单元格发生变化,如果没有变化,则显示提示信息。

    private void btnmodify_Click(object sender, EventArgs e)
    {
        // ... 其他代码保持不变 ...
    
        bool anyChangesMade = false;
    
        // 检查是否有任何单元格被更改
        for (int i = 0; i < resultTable.Rows.Count; i++)
        {
            if (dataGridView1.Rows[i].IsNewRow) continue; // 跳过新行
    
            if (dataGridView1.Rows[i].Cells["date"].Value != null && dataGridView1.Rows[i].Cells["date"].Value.ToString() != resultTable.Rows[i]["date"].ToString())
            {
                anyChangesMade = true;
                break;
            }
    
            // 添加其他你需要检查的单元格,例如:batchNo、proname等...
        }
    
        if (!anyChangesMade)
        {
            MessageBox.Show("请您修改后,再做保存");
            return;
        }
    
        // ... 原始的保存逻辑保持不变 ...
    }
    

    这个改动会在点击保存按钮前检查dataGridView1中的每个单元格,如果发现有任何单元格的值与resultTable中的值不同,那么anyChangesMade变量会被设置为true。如果没有找到任何更改,程序会显示一个消息框,告诉用户需要先进行修改。

    请注意,这段代码仅检查了"date"这一列是否有更改。您可能需要根据实际情况添加更多的条件,检查其他的列(比如"batchNo"、"proname"等)以确保覆盖所有关键的数据项。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月14日
  • 已采纳回答 6月6日
  • 创建了问题 5月23日

悬赏问题

  • ¥20 公众号如何实现点击超链接后自动发送文字
  • ¥15 用php隐藏类名和增加类名
  • ¥15 算法设计与分析课程的提问
  • ¥15 用MATLAB汇总拟合图
  • ¥15 智能除草机器人方案设计
  • ¥15 对接wps协作接口实现消息发送
  • ¥15 SQLite 出现“Database is locked” 如何解决?
  • ¥15 已经加了学校的隶属邮箱了,为什么还是进不去github education?😭
  • ¥15 求会做聚类,TCN的朋友有偿线上指导。以下是目前遇到的问题
  • ¥100 无网格伽辽金方法研究裂纹扩展的程序