I am getting Error in Asp.Net MVC "Cannot perform runtime binding on a null reference"

HASAN P. 212802045 6 Reputation points

The project was working correctly with no exception, then i started to get this error suddenly. I searched this error on forums and figured something about Razor so i updated Visual Studio but nothing changed. Details as below:


<div class="small-box bg-success">
<div class="inner">

                    <h3> @ViewBag.countActivityFinish </h3>  

                    <p>Tamamlanan Aktivite Sayısı</p>  
                <div class="icon">  
                    <i class="ion ion-pie-graph"></i>  
                <a href="/Activity" class="small-box-footer">Aktiviteleri Gör <i class="fas fa-arrow-circle-right"></i></a>  


public ActionResult Index()
if (Session["user"] == null) return RedirectToAction("Index", "Login");


    User withEmailToUser = conn.Query<User>("SELECT * FROM [User] WHERE Email = @Email", new User() { Email = Session["user"].ToString() }).FirstOrDefault();  

    List<UserWrongLoginLog> userWrongLoginLogs = conn.Query<UserWrongLoginLog>("SELECT * FROM [UserWrongLoginLog] WHERE UserId = @UserId", new UserWrongLoginLog() { UserId = (Guid)withEmailToUser.Id }).ToList();      
    foreach (var item in userWrongLoginLogs)  
        conn.Execute("DELETE FROM [UserWrongLoginLog] WHERE Id=@Id", item);  

    int countCompany = conn.Query<int>("SELECT COUNT(*) FROM Company WHERE IsDelete = @IsDelete", new Company() { IsDelete = false }).FirstOrDefault();  
    int countContact = conn.Query<int>("SELECT COUNT(*) FROM Contact WHERE IsDelete = @IsDelete", new Contact() { IsDelete = false }).FirstOrDefault();  
    int countActivityWaiting = conn.Query<int>("SELECT COUNT(*) FROM Activity WHERE Status = @Status", new Activity() { Status = 0 }).FirstOrDefault();  
    int countActivityFinish = conn.Query<int>("SELECT COUNT(*) FROM Activity WHERE Status != @Status", new Activity() { Status = 0 }).FirstOrDefault();  


    withEmailToUser.UserWrongLoginLogs = userWrongLoginLogs;  

    ViewBag.countCompany = countCompany;  
    ViewBag.countContact = countContact;  
    ViewBag.countActivityWaiting = countActivityWaiting;  
    ViewBag.countActivityFinish = countActivityFinish;  
    ViewBag.user = withEmailToUser;  
    return View();  

my error: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference

A set of technologies in the .NET Framework for building web applications and XML web services.
4,022 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,976 questions
{count} votes

2 answers

Sort by: Most helpful
  1. HASAN P. 212802045 6 Reputation points

    After deleting and recreating my view page, pasting the same codes fixed my problem.

    1 person found this answer helpful.
    0 comments No comments

  2. Bruce (SqlWork.com) 52,826 Reputation points

    you don't show the code with the error, but most likely a null view bag object you reference. user is the only view bag object so, likely there is

    @ViewBag.user.<Some prop>

    and user is null.

    0 comments No comments