簡體   English   中英

在asp.net中執行更新查詢時出錯

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

我使用了一個班級,在該班級中,我有這個更新查詢,以查詢唯一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();
    }

我說錯了

你調用的對象是空的

此錯誤顯示在literal1中

誰能告訴我我要去哪里錯了? 我的意思是我看到所有的托運工作都做好了

現在在使用(ctr + Alt + E)之后它給出了錯誤

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

發生了NullReferenceException

這是一個潛在的問題:

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

如果ExecuteScalar()返回null,則您的ToString()將引發null-ref異常。

編輯:由於它似乎是這一行,因此將其重寫為:

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

並使用調試器查看tmp是否為null。

編輯2:

可能為NULL。 UPDATE ... sql語句應使用ExecuteNonQuery而不是ExecuteScalar

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM