c# - 为什么我收到错误 "Input string was not in a correct format"

标签 c# .net datagridview

我正在将数据从 datagridview 插入数据库,我调试我的代码并发现错误 Input string was not in a correct format。在下一行中

Convert.ToInt32(dgvResult.Rows[i].Cells[9].Value.ToString())

此列是一个整数列,当我用整数值替换此行时,例如数字 3 插入完成而没有错误,其他整数列也以相同的方式插入而没有任何错误 这是我的代码:

    if (checkApproveResult.Checked == false && chkupdateApproved.Checked == false)
                {
                    for (int i = 0; i < dgvResult.Rows.Count; i++)
                    {
                        result.UPDATE_LAB_RESULTS(Convert.ToInt32(txtOrder.Text),
                                                  dgvResult.Rows[i].Cells[7].Value.ToString(),
                                                  5,
                                                  dgvResult.Rows[i].Cells[6].Value.ToString(),
                                                  txtExamUser.Text,
                                                  DateTime.Parse(DateTimeExamined.Value.ToString()),
                                                  Convert.ToInt32(dgvResult.Rows[i].Cells[2].Value),
                                                  dgvResult.Rows[i].Cells[4].Value.ToString(),
                                                  dgvResult.Rows[i].Cells[5].Value.ToString(),
                                                  Convert.ToInt32(dgvResult.Rows[i].Cells[9].Value.ToString()),
                                                  Convert.ToInt32(txtPno.Text),
                                                  Convert.ToInt32(txtcustid.Text),
                                                  txtReqForm.Text,
                                                  dgvResult.Rows[i].Cells[1].Value.ToString(),
                                                  Convert.ToInt32(dgvResult.Rows[i].Cells[8].Value.ToString()));
                    }
                    MessageBox.Show("Result Saved Successfully ", "Entering Result", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }

   else if (chkupdateApproved.Checked == true)
                {
                    for (int i = 0; i < dgvResult.Rows.Count; i++)
                    {
                        result.ADD_LAB_RESULTS_UPDATES(Convert.ToInt32(txtsampleid.Text),
                            Convert.ToInt32(txtOrder.Text),
                            Convert.ToInt32(dgvResult.Rows[i].Cells[2].Value),
                            Convert.ToInt32(txtgroupid.Text),
                            "YES",
                            6,
                            dgvResult.Rows[i].Cells[11].Value.ToString(),
                            DateTime.Parse(DateTimeExamined.Value.ToString()),
                            dgvResult.Rows[i].Cells[13].Value.ToString(),
                            DateTime.Parse(dateTimeApprove.Value.ToString()),
                            dgvResult.Rows[i].Cells[4].Value.ToString(),
                            dgvResult.Rows[i].Cells[5].Value.ToString(),
                            dgvResult.Rows[i].Cells[6].Value.ToString(),
                            Convert.ToInt32(txtpackageid.Text),
                            Convert.ToInt32(dgvResult.Rows[i].Cells[9].Value.ToString()),
                            2,
                            Convert.ToInt32(txtPno.Text),
                            Convert.ToInt32(txtcustid.Text), txtReqForm.Text, 
                            Convert.ToInt32(dgvResult.Rows[i].Cells[8].Value.ToString()),
                            txtupdatedby.Text,
                            DateTime.Parse(dateupdate.Value.ToString()));

                        }
                    update.UPDATE_LAB_RESULT_STATUS(Convert.ToInt32(txtOrder.Text), Convert.ToInt32(txtsampleid.Text), 2);
                    MessageBox.Show("Result Updated Successfully ", "Update Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

ADD_LAB_RESULT_UPDATES 的代码是:

public void ADD_LAB_RESULTS_UPDATES(int SAMPLE_ID, int ORDER_ID,int TESTID,int GROUPID, string NORMAL_RESULT, 
            int SAMPLE_STATUS,string EXAMINED_BY,DateTime EXAMINED_DATE, string APPROVED_BY, DateTime APPROVED_DATE,
            string RESULT_NUMBER, string RESULT_REPORT, string RESULT_NOTE,int packageid, int machine_id, int deptid, 
            int patient_no, int custid, string REQ_FORM_NO,int serial,string UPDATED_BY,DateTime UPDATED_DATE)
        {
            DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
            DAL.open();
            SqlParameter[] param = new SqlParameter[22];
            param[0] = new SqlParameter("@SAMPLE_ID", SqlDbType.Int);
            param[0].Value = SAMPLE_ID;

            param[1] = new SqlParameter("@ORDER_ID", SqlDbType.Int);
            param[1].Value = ORDER_ID;

            param[2] = new SqlParameter("@TESTID", SqlDbType.Int);
            param[2].Value = TESTID;

            param[3] = new SqlParameter("@GROUPID", SqlDbType.Int);
            param[3].Value = GROUPID;

            param[4] = new SqlParameter("@NORMAL_RESULT", SqlDbType.VarChar, 10);
            param[4].Value = NORMAL_RESULT;

            param[5] = new SqlParameter("@SAMPLE_STATUS", SqlDbType.Int);
            param[5].Value = SAMPLE_STATUS;

            param[6] = new SqlParameter("@EXAMINED_BY", SqlDbType.VarChar, 50);
            param[6].Value = EXAMINED_BY;

            param[7] = new SqlParameter("@EXAMINED_DATE", SqlDbType.DateTime);
            param[7].Value = EXAMINED_DATE;

            param[8] = new SqlParameter("@APPROVED_BY", SqlDbType.VarChar, 50);
            param[8].Value = APPROVED_BY;

            param[9] = new SqlParameter("@APPROVED_DATE", SqlDbType.DateTime);
            param[9].Value = APPROVED_DATE;

            param[10] = new SqlParameter("@RESULT_NUMBER", SqlDbType.VarChar, 50);
            param[10].Value = RESULT_NUMBER;

            param[11] = new SqlParameter("@RESULT_REPORT", SqlDbType.VarChar, 2000);
            param[11].Value = RESULT_REPORT;

            param[12] = new SqlParameter("@RESULT_NOTE", SqlDbType.VarChar, 200);
            param[12].Value = RESULT_NOTE;

            param[13] = new SqlParameter("@packageid", SqlDbType.Int);
            param[13].Value = packageid;

            param[14] = new SqlParameter("@machine_id", SqlDbType.Int);
            param[14].Value = machine_id;

            param[15] = new SqlParameter("@deptid", SqlDbType.Int);
            param[15].Value = deptid;

            param[16] = new SqlParameter("@patient_no", SqlDbType.Int);
            param[16].Value = patient_no;

            param[17] = new SqlParameter("@custid", SqlDbType.Int);
            param[17].Value = custid;

            param[18] = new SqlParameter("@REQ_FORM_NO", SqlDbType.VarChar, 50);
            param[18].Value = REQ_FORM_NO;

            param[19] = new SqlParameter("@serial", SqlDbType.Int);
            param[19].Value = serial;

            param[20] = new SqlParameter("@UPDATED_BY", SqlDbType.VarChar, 50);
            param[20].Value = UPDATED_BY;

            param[21] = new SqlParameter("@UPDATED_DATE", SqlDbType.DateTime);
            param[21].Value = UPDATED_DATE;

            DAL.ExecuteCommand("ADD_LAB_RESULTS_UPDATES", param);
            DAL.close();

        }

参数 14 machine_id 的错误 还有 database machine_id int 中的表。

错误是什么?

更多信息我有第一个if语句(更新语句) 结果。 UPDATE_LAB_RESULTS 包含相同的参数 machine_id 及其工作和插入没有错误。 第二个 if 语句(插入语句)并使用 machine_id 参数显示错误。

最佳答案

您必须检查以下内容:

1- 检查参数列表变量类型(string、int、...)并将其与您的过程进行比较,您会发现一个变量类型在存储过程整数和参数列表字符串中不同。

关于c# - 为什么我收到错误 "Input string was not in a correct format",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54634507/

相关文章:

c# - WPF数据绑定(bind)无法获取数据

.net - CopyPixelOperation.SourceInvert 不起作用

vb.net - 根据 DataGridView 上的选定单元格获取 RowIndex

c# - 使用 DataGridView.SelectedRows 作为另一个 DataGridView 的数据源

c# - 删除 datagridview 中选定的行并删除数据库中的行

c# - 随机 "Index was outside the bounds of the array"作为 SmtpException

c# - 未填充 Linq to Sql 对象

C# 应用程序将文件从 GAC 复制到执行文件夹

c# - 如何在访问多个不同的数据库时获得最佳性能

c# - 为什么 RequestAdditionalTime() 方法在 Vista/7 中重启时不起作用?