0

I am from Classic ASP background. I am just trying to declare a variable int TOTAL_ROWS at the top and after I know how many rows in my MySQL database at the bottom, I will assign it to the variable and pass the value to another Action/Method to be used. But I keep getting this An object reference is required for the non-static field I am a beginner for OOP. Please help.

CODE

namespace BRO.Controllers
{
    public class PasswordController : Controller
    {
        int TOTAL_ROWS;

        //private const int TOTAL_ROWS = 2;
        private static readonly List<DataItem> _data = CreateData();

    private static List<DataItem> CreateData()
    {

        List<DataItem> list = new List<DataItem>();

        string mainconn = ConfigurationManager.ConnectionStrings["MySQLConnection"].ConnectionString;
        MySqlConnection mysqlconn = new MySqlConnection(mainconn);
        string sSQL = " SELECT * FROM mainpass ";
        MySqlCommand comm = new MySqlCommand(sSQL);
        comm.Connection = mysqlconn;

        MySqlDataAdapter adapter = new MySqlDataAdapter(comm);
        DataTable dt = new DataTable();
        adapter.Fill(dt);

        //===Try 1 Not working === An object reference is required for the non-static field,
        //============ method, or property ControllerBase.TempData
        TempData["TOTAL_ROWS"] = dt.Rows.Count;

        int iTOTAL_ROWS = dt.Rows.Count;
        //===Try 2 Not Working=== An object reference is required for the non-static field,
        //============ method, or property PasswordController.TOTAL_ROWS
        TOTAL_ROWS = iTOTAL_ROWS; //=== 

    public ActionResult AjaxGetJsonData(int draw, int start, int length)
    {
        string search = Request.QueryString["search[value]"];
        int sortColumn = -1;
        string sortDirection = "asc";
        if (length == -1)
        {
            length = TOTAL_ROWS; //*** Pass the value here
        }

        // note: we only sort one column at a time
        if (Request.QueryString["order[0][column]"] != null)
        {
            sortColumn = int.Parse(Request.QueryString["order[0][column]"]);
        }
        if (Request.QueryString["order[0][dir]"] != null)
        {
            sortDirection = Request.QueryString["order[0][dir]"];
        }

        DataTableData dataTableData = new DataTableData();
        dataTableData.draw = draw;
        dataTableData.recordsTotal = TOTAL_ROWS; //**** Pass the value here
        int recordsFiltered = 0;
        dataTableData.data = FilterData(ref recordsFiltered, start, length, search, sortColumn, sortDirection);
        dataTableData.recordsFiltered = recordsFiltered;

        return Json(dataTableData, JsonRequestBehavior.AllowGet);
    }
1
  • means you need static int TOTAL_ROWS; Static items can only access other static items Commented Apr 4, 2019 at 7:22

1 Answer 1

1

You have to make the TOTAL_ROWS a static variable. Only then you will be able to set it from the static method.

But doing so will create a different problem if your system is used by multiple users at a time. Users data would overlap - Concurrency issue.

If you just want to pass data from one action to another then look in to the use of TempData object in Asp.net MVC here.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.