Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 956681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:34:43+00:00 2026-05-16T00:34:43+00:00

I have used a class in which i have this update query for Payment

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:34:43+00:00Added an answer on May 16, 2026 at 12:34 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which contains a static collection that can be used across
I have a singleton class which is being used throughout the app. I am
I have a library which has a basic class which is used extensively by
Let's say I have a list in a class which will be used in
I have a file attachment class (FileAttachment) which is used in several other classes.
I have a silly, little class FileSystemSize which can be used both as an
This should be a fundamental C# question. Suppose I have a Person class which
I have a class ShipmentsCollection that inherits ObservableCollection which contains shipment objects (entities). This
I have never used DBIx::Class until today, so I'm completely new at it. I'm
I have two Java interfaces and one implementing class. (I have used Eclipse to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.