【问题标题】:error while executing Update query in the asp.net在 asp.net 中执行更新查询时出错
【发布时间】:2010-07-29 11:24:06
【问题描述】:

我使用了一个课程,在该课程中我有一个学生的付款费用更新查询,该学生的唯一 ID 为 UID 并且为他使用了帐户 ID,即 AccId

public string updatePays(string UID,int AccId,float hostel,float book,float exam,float reval,float studentDevelop,float misc,float reregistration,float late,float dues,float arrear,float grandtotal)
    {
        string SQLQuery = " UPDATE Pays SET ExamFees = " + exam + ",ReValuationFees = " + reval + ",Hostelfees = " + hostel + ",BookFees = " + book + ",StudentDevelopFees = " + studentDevelop + ",ReregistrationFees = " + reregistration + ",MiscFees = " + misc + ",LateFees=" + late + ",DuesFees=" + dues + ",Backfees=" + arrear + ",GrandFees=" + grandtotal + " WHERE UID = '" + UID + "' AND AccId = " + AccId + "";
        SqlCommand command = new SqlCommand(SQLQuery, sqlConnection);
        string flag = "";

        sqlConnection.Open();
        try
        {
            flag = command.ExecuteScalar().ToString();
            if (flag.ToString() != null)
            {
                sqlConnection.Close();
                return flag;
            }
            else
            {
                sqlConnection.Close();
                return "Student Fees has not been Updated ";
            }
        }
        catch (Exception exr)
        {
            sqlConnection.Close();
            return exr.Message;
        }
    }

现在我从按钮点击事件中调用这个函数

protected void Button1_Click(object sender, EventArgs e)
    {
        DatabaseLayer data = new DatabaseLayer();

        string UID = Session["UID"].ToString();
        int AccId = AccId = (int)(Session["Acc"]);
        string semfees = Session["semFees"].ToString();

        float hostel = 0;
        float book = 0;
        float exam = 0;
        float reval = 0;
        float studentDevelop = 0;
        float misc = 0;
        float reregistration = 0;
        float late = 0;
        float dues = 0;
        float arrear = 0;
        float grandtotal = 0;
        float sf = float.Parse(semfees.ToString());

        if (float.TryParse(TextBox2.Text, out hostel)) { }
        if (float.TryParse(TextBox3.Text, out book)) { }
        if (float.TryParse(TextBox4.Text, out exam)) { }
        if (float.TryParse(TextBox5.Text, out reval)) { }
        if (float.TryParse(TextBox12.Text, out studentDevelop)) { }
        if (float.TryParse(TextBox6.Text, out misc)) { }
        if (float.TryParse(TextBox7.Text, out reregistration)) { }
        if (float.TryParse(TextBox8.Text, out late)) { }
        if (float.TryParse(TextBox9.Text, out dues)) { }
        if (float.TryParse(TextBox10.Text, out arrear)) { }

        string flag = "";

        grandtotal = sf + hostel + book + exam + reval + studentDevelop + misc + reregistration + late + dues + arrear;

        flag = data.updatePays(UID, AccId, hostel, book, exam, reval, studentDevelop, misc, reregistration, late, dues, arrear, grandtotal);
        Literal1.Text = flag.ToString();
    }

我说错了

对象引用未设置为对象的实例

此错误以文字形式显示1

谁能告诉我哪里出错了?我的意思是我确保所有的声明都正确完成了

现在使用 (ctr+Alt+E) 后在

处出现错误
flag = command.ExecuteScalar().ToString();

发生了NullReferenceException

【问题讨论】:

  • 这个异常是在哪里抛出的?启用异常停止(VS.Net 中的 Ctrl+Alt+E - 选中相关框以停止框架异常)。
  • 你在哪里得到这个错误?
  • @Will A 我确实遵循 (Ctrl+Alt+E) 并在 flag = command.ExecuteScalar().ToString(); 处给出错误说发生了 NullReferenceException
  • flag.ToString() != null 也是错误的:如果 ToString() 不能为空并且如果标志为空,则会抛出异常
  • @Julien N 我对一个已经在他/她的 UID 下付款的学生感到异常...

标签: c# sql visual-studio


【解决方案1】:

这是一个潜在的问题:

  flag = command.ExecuteScalar().ToString();

如果 ExecuteScalar() 返回 null,那么您的 ToString() 将抛出 null-ref 异常。

编辑:既然好像是这一行,就改写成:

object tmp command.ExecuteScalar();
flag = tmp.ToString();

并使用调试器查看tmp是否为null。

编辑2:

它可能会是 NULL。 UPDATE ... sql 语句应该使用 ExecuteNonQuery 执行,而不是 ExecuteScalar

【讨论】:

  • 虽然你说它会导致错误是正确的,但它在 try catch 中,所以它不会导致 OP 的错误。
  • 但是我得到一个现有 UID 的错误,这意味着 ExecuteScalar() 应该返回一个值!!?
  • 我什至使用了刚刚从函数返回的 int flag = command.ExecuteNonQuery() ..
  • 如果想使用 select sql 语句并检索一个值,我仍然得到 NullRefrenceException,你觉得我应该更频繁地使用 Execute reader for select 查询吗?
  • 是的,获取记录的唯一方法是使用 ExecuteReader()。 ExecuteScalar() 用于SELECT MAX() 类型的查询。
猜你喜欢
  • 1970-01-01
  • 2015-08-13
  • 1970-01-01
  • 1970-01-01
  • 2018-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多