我使用了一个课程,其中我有一个学生的付款费用的更新查询,其唯一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
答案 0 :(得分:1)
这是一个潜在的问题:
flag = command.ExecuteScalar().ToString();
如果ExecuteScalar()返回null,那么你的ToString()将抛出null-ref异常。
编辑:因为它似乎是这一行,所以将其重写为:
object tmp command.ExecuteScalar();
flag = tmp.ToString();
使用调试器查看tmp是否为null。
它可能是NULL。应使用 UPDATE ...
执行 ExecuteNonQuery
sql语句,而不是 ExecuteScalar
。