0

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 i.e. 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

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

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

saying NullReferenceException occured

5
  • Where does this exception get thrown? Enable stop-on-exceptions (Ctrl+Alt+E in VS.Net - check the relevant box to stop on framework exceptions). Commented Jul 29, 2010 at 11:27
  • 1
    Where do you get that error? Commented Jul 29, 2010 at 11:28
  • @Will A I did follow (Ctrl+Alt+E) and it gave error at flag = command.ExecuteScalar().ToString(); saying NullReferenceException occured Commented Jul 29, 2010 at 11:48
  • flag.ToString() != null is also wrong : if ToString() can't be null and if flag is null is would throw an Exception Commented Jul 29, 2010 at 12:29
  • @Julien N i am getting exaception for a student who already has a payment under his/her UID... So it cant be null atleast for the update for UID which has a payment .. :( Commented Jul 29, 2010 at 12:48

1 Answer 1

1

This is a potential problem:

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

If ExecuteScalar() returns null then your ToString() will throw the null-ref exception.

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.

Edit2:

And it probably will be NULL. An UPDATE ... sql statement should be executed with ExecuteNonQuery, not with ExecuteScalar.

Sign up to request clarification or add additional context in comments.

5 Comments

Although you're correct that it'd cause an error, it's in a try catch, so it wouldn't cause the OP's error.
but i got error for an existing UID, which means ExecuteScalar() should return a value!!?
I had even used int flag = command.ExecuteNonQuery() which just returned from the function ..
if want to use a select sql statement and retrieve a value , I still got NullRefrenceException, Do you feel i should use more often either Execute reader for select query?
Yes, the only way to get records back is to use ExecuteReader(). ExecuteScalar() is for SELECT MAX() kind of queries.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.