0

Here in my app now, the program is running, the database rows name show in the grid, but there is no data on the datagrid where it suppose to show the data from database.

MVC Controller:

public ActionResult Index(int? pageNumber, int? pageSize, string filter, string sortColumn, string sortOrder, int? totalCount)

    {
         //List<UserActivityModels> userActivity = null;

        List<UserActivityModels> userActivity = new List<UserActivityModels>();


        //totalCount = 0;

        string sqlWhere = string.Empty;

        sqlWhere = string.IsNullOrEmpty(filter) ? sqlWhere : sqlWhere +
            " AND (Id LIKE @filter)  ";

        string sqlOrderBy = "ORDER BY " + sortColumn + " " + sortOrder;

        String sqlSelect = @"
           SELECT Id
            ,CreatedBy
            ,CreatedOn
            ,ModifiedBy
            ,ModifiedOn
            ,ContactId
            ,EntityName
            ,EntityId
            ,ActivityType
            ,ActivityStatus
            ,DueDate
            ,ActualEndDate
            ,MasqueradeOn
            ,MasqueradeBy 
        FROM UserActivity 
        ORDER BY CreatedOn DESC
       WHERE @Id = Id
       LIMIT @PageSize OFFSET @PageNumber
        ";
        //WHERE @Id = Id
        //LIMIT @PageSize OFFSET @PageNumber
        string sqlCount = @"
        SELECT COUNT(Id) TotalCount 
        FROM UserActivity
              ";


        try
        {
            using (IDbConnection db = new MySqlConnection(ConfigurationManager.ConnectionStrings["CRMPORTALSQLCONN"].ConnectionString))
            {
                userActivity = (List<UserActivityModels>)db.Query<UserActivityModels>(sqlSelect, new

                {
                    @filter = "%" + filter + "%",
                    @PageSize = pageSize,
                    @PageNumber = (pageNumber - 1) * pageSize

                });

                ViewData["totalCount"] = (int)db.ExecuteScalar<int>(sqlCount, new
                {
                    @filter = filter,
                    @PageNumber = pageNumber,
                });
                ViewData["filter"] = filter;
                ViewData["PageSize"] = 30;

                return RedirectToAction("Index");
            }
        }

        catch
        {
            //throw new Exception("Unable to retrieve data from DB.", ex);
            return View(userActivity);
        }

        //return View(userActivity);

        /* if (userActivity != null)
         {
             return RedirectToAction(userActivity);
         }*/
    }


    //POST: /UserActivity/Index
    [HttpPost]
    public ActionResult Index(FormCollection collection)
    {
        try
        {
            // TODO: Add insert logic here

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

but there is no error show on the visual studio

and here is index view:

    @*<p>
    @Html.ActionLink("Create New", "Create")
</p>*@

   <h3>User Activity</h3>
   @using (Html.BeginForm("index", null, FormMethod.Get))
   {
<div class="row">
    <div class="col-sm-8">
        <div class="input-group">

            <input type="text"
                   name="filter"
                   value="@ViewBag.filter"
                   class="form-control"
                   style="display: inline"
                   placeholder="Search by Contact Id" />
            <span class="input-group-btn">
                <button class="btn btn-default" type="submit">Go</button>
            </span>

        </div>
    </div>
    <div class="pull-right col-lg-1">
        <a class="btn btn-success" data-modal="" href="/UserActivity/Create" id="btnCreate">
            <span class="glyphicon glyphicon-plus"></span>
        </a>
    </div>
</div>

<div class="table-responsive" style="margin-top:5px;">
    @{
        var grid = new WebGrid(
                    Model,
                    canPage: true,
                    rowsPerPage: 5,
                    canSort: true);
        //ajaxUpdateContainerId: "grid");

        //grid.Bind(Model, rowCount: (int)ViewData["totalCount"], autoSortAndPage: false);

        grid.Pager(WebGridPagerModes.All);

        @grid.GetHtml(
       //htmlAttributes: new { id = "grid" },
    // id for ajaxUpdateContainerId parameter
    fillEmptyRows: false,
    tableStyle: "table table-striped",
    mode: WebGridPagerModes.All,
    columns: grid.Columns(
      //grid.Column("Id", "Id"),
      grid.Column("Id", "Id", style: "col-lg-1", canSort: true),
      grid.Column("CreatedBy", "CreatedBy", style: "col-lg-6"),
      grid.Column("CreatedOn", header: "CreatedOn", style: "col-lg-2", canSort: true),
      grid.Column("ModifiedBy", header: "ModifiedBy", style: "col-lg-2"),
      grid.Column("ModifiedOn", header: "ModifiedOn", style: "col-lg-2"),
      grid.Column("ContactId", header: "ContactId", style: "col-lg-2"),
      grid.Column("EntityName", header: "EntityName", style: "col-lg-2"),
      grid.Column("EntityId", header: "EntityId", style: "col-lg-2"),
      grid.Column("ActivityType", header: "ActivityType", style: "col-lg-2"),
      grid.Column("ActivityStatus", header: "ActivityStatus", style: "col-lg-2"),
      grid.Column("DueDate", header: "DueDate", style: "col-lg-2"),
      grid.Column("ActualEndDate", header: "ActualEndDate", style: "col-lg-2"),
      grid.Column("MasqueradeOn", header: "MasqueradeOn", style: "col-lg-2"),
      grid.Column("MasqueradeBy", header: "MasqueradeBy", style: "col-lg-2"),


      grid.Column(header: "Action", canSort: false, style: "action",
 format: @<text>
        @Html.Raw("<a data-modal='' href='/UserActivity/Details/" + item.Id + "' id='" + item.Id + "' title='Detail'> <span class='glyphicon glyphicon-search'> </span> </a>")
        @Html.Raw("<a data-modal='' href='/UserActivity/edit/" + item.Id + "' id='" + item.Id + "' title='Edit'> <span class='glyphicon glyphicon-edit'> </span> </a>")
        @Html.Raw("<a data-modal='' href='/UserActivity/delete/" + item.Id + "' id='" + item.Id + "' title='Delete'> <span class='glyphicon glyphicon-trash'> </span> </a>")
</text>)

    ));
    }

</div>
        }

is anyone coud tell me how to retrieve data from db and show it into gridview

Botski
  • 195
  • 2
  • 3
  • 13
  • Possible duplicate of [What does "Object reference not set to an instance of an object" mean?](http://stackoverflow.com/questions/779091/what-does-object-reference-not-set-to-an-instance-of-an-object-mean) – ste-fu Mar 24 '16 at 08:35
  • Near the top of your view put @model List then as a test in your view try accessing the model to ensure it is populated... something like @Html.Raw(Model.EntityName) – Paul Zahra Mar 24 '16 at 09:00

0 Answers0