简体   繁体   English

在asp.net中执行更新查询时出错

[英]error while executing Update query in the asp.net

I have used a class in which i have this update query for Payment Fees of a student whose Unique ID is UID and an Account ID is used for him ie AccId 我使用了一个班级,在该班级中,我有这个更新查询,以查询唯一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;
        }
    }

Now I call this function from a button click event 现在,我从按钮单击事件中调用此函数

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();
    }

I am getting eror saying 我说错了

Object reference not set to an instance of an object 你调用的对象是空的

This error was shown in the literal1 此错误显示在literal1中

Can any one tell where am i going wrong? 谁能告诉我我要去哪里错了? I mean i saw to it that all the delcaration is done properly 我的意思是我看到所有的托运工作都做好了

Now after using (ctr+Alt+E) it gave error at 现在在使用(ctr + Alt + E)之后它给出了错误

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

saying NullReferenceException occured 发生了NullReferenceException

This is a potential problem: 这是一个潜在的问题:

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

If ExecuteScalar() returns null then your ToString() will throw the null-ref exception. 如果ExecuteScalar()返回null,则您的ToString()将引发null-ref异常。

Edit: since it seems to be this line, rewrite it as: 编辑:由于它似乎是这一行,因此将其重写为:

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

And use the debugger to see if tmp is null. 并使用调试器查看tmp是否为null。

Edit2: 编辑2:

And it probably will be NULL. 可能为NULL。 An UPDATE ... sql statement should be executed with ExecuteNonQuery , not with ExecuteScalar . UPDATE ... sql语句应使用ExecuteNonQuery而不是ExecuteScalar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM