c# - 'ExecuteNonQuery : CommandText property has not been initialized' beginner here

标签 c# sql error-handling command-text

我是sql和C#的新手,并且im在executenonquery commandtex中出错。我不知道错误在哪里。你们可以帮我吗?

private void submitBtn_Click(object sender, EventArgs e)
{
    con.Open();

    string a = "Accept";
    string b = "Reject";
    string queryUpdate1 = "";
    string queryUpdate2 = "";
    int row = DGVLeaves.CurrentCell.RowIndex;


    if (accptBtn.Checked)
    {
        if (type_rdonly.Text == "SL")
        {
            if (ifEmployeeExist(con, emptime_rdonly.Text))
            {
                queryUpdate1 = @"UPDATE [LEAVE_EMP] SET EMP_STATUS ='" + a + "'WHERE [EMP_TIME] ='" + emptime_rdonly.Text + "'";
            }
            queryUpdate2 = "UPDATE LEAVE_ADMIN SET L_SPENT_SL = (L_SPENT_SL + 1), L_REM_SL = (L_REM_SL - 1)";
        }
        if (type_rdonly.Text == "VL")
        {
            if (ifEmployeeExist(con, emptime_rdonly.Text))
            {
                queryUpdate1 = @"UPDATE [LEAVE_EMP] SET EMP_STATUS ='" + a + "'WHERE [EMP_TIME] ='" + emptime_rdonly.Text + "'";
            }
            queryUpdate2 = "UPDATE LEAVE_ADMIN SET L_SPENT_VL = (L_SPENT_VL + 1),L_REM_VL = (L_REM_VL - 1)";
        }
        SqlCommand cmd1 = new SqlCommand(queryUpdate1, con);
        SqlCommand cmd2 = new SqlCommand(queryUpdate2, con);
        cmd2.ExecuteNonQuery();
        cmd1.ExecuteNonQuery();
    }
    if (rejBtn.Checked)
    {
        if (ifEmployeeExist(con, emptime_rdonly.Text))
        {
            queryUpdate1 = @"UPDATE [LEAVE_EMP] SET EMP_STATUS ='" + b + "'WHERE [EMP_TIME] ='" + emptime_rdonly.Text + "'";
        }
        SqlCommand cmd1 = new SqlCommand(queryUpdate1, con);
        cmd1.ExecuteNonQuery();
    }
    con.Close();
}

最佳答案

我认为if子句过多,您可能会在过程中错过某些内容。
在执行ExecuteNonQuery命令之前,尝试调试并查看变量queryUpdate1或queryUpdate2是否为空。如果它是空的,那应该是原因

我已经为您调整了代码,希望对您有所帮助

private void submitBtn_Click(object sender, EventArgs e)
    {

        string a = "Accept";
        string b = "Reject";
        string queryUpdate1 = "";
        string queryUpdate2 = "";
        int row = DGVLeaves.CurrentCell.RowIndex;

        if (accptBtn.Checked)
        {
            if (type_rdonly.Text == "SL")
            {
                if (ifEmployeeExist(con, emptime_rdonly.Text))
                {
                    queryUpdate1 = @"UPDATE [LEAVE_EMP] SET EMP_STATUS ='" + a + "'WHERE [EMP_TIME] ='" + emptime_rdonly.Text + "'";
                }
                queryUpdate2 = "UPDATE LEAVE_ADMIN SET L_SPENT_SL = (L_SPENT_SL + 1), L_REM_SL = (L_REM_SL - 1)";
            }
            if (type_rdonly.Text == "VL")
            {
                if (ifEmployeeExist(con, emptime_rdonly.Text))
                {
                    queryUpdate1 = @"UPDATE [LEAVE_EMP] SET EMP_STATUS ='" + a + "'WHERE [EMP_TIME] ='" + emptime_rdonly.Text + "'";
                }
                queryUpdate2 = "UPDATE LEAVE_ADMIN SET L_SPENT_VL = (L_SPENT_VL + 1),L_REM_VL = (L_REM_VL - 1)";
            }
        }

        else if (rejBtn.Checked)
        {
            if (ifEmployeeExist(con, emptime_rdonly.Text))
            {
                queryUpdate1 = @"UPDATE [LEAVE_EMP] SET EMP_STATUS ='" + b + "'WHERE [EMP_TIME] ='" + emptime_rdonly.Text + "'";
            }
        }

        con.Open();

        SqlCommand cmd = new SqlCommand() { Connection = con, CommandType = System.Data.CommandType.Text };
        if (!string.IsNullOrEmpty(queryUpdate1)) {
            cmd.CommandText = queryUpdate1;
            cmd.ExecuteNonQuery();
        }

        if (!string.IsNullOrEmpty(queryUpdate2))
        {
            cmd.CommandText = queryUpdate2;
            cmd.ExecuteNonQuery();
        }

        if (string.IsNullOrEmpty(queryUpdate1) && string.IsNullOrEmpty(queryUpdate2))
        {
            MessageBox.Show("Empty query");
        }

        con.Close();
    }

关于c# - 'ExecuteNonQuery : CommandText property has not been initialized' beginner here,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54319268/

相关文章:

javascript - 如何将调用者详细信息添加到 Node.js 中的错误堆栈跟踪中?

c++ - 做组合时被零除

c# - 调试IDE弹出错误 "Object Reference not set to instance of object"后

mysql - 如何在mysql中定义一个100亿行的key/value表?

c# - 为什么我的 OpenGL ES 应用程序在 glDrawElements 上崩溃?

PHP:未选择数据库

php - 查询生成器数字格式 codeigniter

swift - Swift 语言中的错误处理

c# - 在运行时动态调用 Web 服务

c# - 从具有整数值的 jwt 有效负载解码编码的 JSON 字符串时,Convert.FromBase64string 抛出无效的 Base-64 异常