MVC failing to show data on view page

I am writing to seek help, in regards debugging my code.  when I render my mvc view page, it shows no data and throws javascript alert box "error".  

Here is my repository class:

    public class MessagesRepository
    {
        readonly string _connString = ConfigurationManager.ConnectionStrings["#######"].ConnectionString;

        public IEnumerable<Messages> GetAllMessages()
        {
            var messages = new List<Messages>();
            using (var connection = new SqlConnection(_connString))
            {
                connection.Open();
                using (var command = new SqlCommand(@"SELECT [role],[description] FROM [dbo].[User]", connection))
                {
                    command.Notification = null;

                    var dependency = new SqlDependency(command);
                    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);

                    if (connection.State == ConnectionState.Closed)
                        connection.Open();

                    var reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        messages.Add(item: new Messages { Role = (string)reader["role"], Descrip = (string)reader["description"] });
                    }
                    connection.Close();
                }
            }
            return messages;
        }

        private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            if (e.Type == SqlNotificationType.Change)
            {
                MessagesHub.SendMessages();
            }
        }
    }

In my ‘Home’ controller, I call the repository class:

   public ActionResult GetMessages()
        {
            MessagesRepository _messageRepository = new MessagesRepository();
            return PartialView("_MessagesList", _messageRepository.GetAllMessages());
        }

I am using SignalR script, on client-side to render my data:

    ViewBag.Title = "Home Page";
}
<div class="row">
    <div class="col-md-12">
       <div id="messagesTable"></div>
    </div>
</div>

@section Scripts{
<script src="~/Scripts/jquery.signalR-2.1.2.js"></script>

 <!--Reference the autogenerated SignalR hub script. -->
    <script src="/signalr/hubs"></script>

<script type="text/javascript">
    $(function () {
        // Declare a proxy to reference the hub.
        var notifications = $.connection.messagesHub;

        //debugger;
        // Create a function that the hub can call to broadcast messages.
        notifications.client.updateMessages = function () {
            getAllMessages()

        };
        // Start the connection.
        $.connection.hub.start().done(function () {
            alert("connection started")
            getAllMessages();
        }).fail(function (e) {
            alert(e);
        });
    });


    function getAllMessages() {
        var tbl = $('#messagesTable');
        $.ajax({
            url: '/home/GetMessages',
            contentType: 'application/html ; charset:utf-8',
            type: 'GET',
            dataType: 'html'
        }).success(function (result) {

            alert("showing result...")
            tbl.empty().append(result);
        }).error(function () {
            alert("error")

        });
    }
</script>

}

When, i run the code, the client-sides currently showing me a blank screen and also showing "error" alert window, from the javascript.  I would like to seek help, in to find, what parameters to look for when debuging this code, to figure out where the code
may be going wrong.  

Please help.

use the browsers network trace, or fiddler to see the error response text. 

Hi, thank you for your suggestion.  I manage to find out, I am receiving
error 500
for the method "getMessage", which is located in my Home controller:

  public ActionResult GetMessages()
        {
            MessagesRepository _messageRepository = new MessagesRepository();
            return PartialView("_MessagesList", _messageRepository.GetAllMessages());
        }

I tired adding try..catch block to the MessageRespository class, but I am still failing to catch 500 error, to figure where, I am going in the code. 

 public class MessagesRepository
    {
        readonly string _connString = ConfigurationManager.ConnectionStrings["##########"].ConnectionString;

        public IEnumerable<Messages> GetAllMessages()
        {
            var messages = new List<Messages>();
            try
            {
            using (var connection = new SqlConnection(_connString))
            {
                connection.Open();
                using (var command = new SqlCommand(@"SELECT [role],[description] FROM [dbo].[User]", connection))
                {
                    command.Notification = null;
                    var dependency = new SqlDependency(command);
                    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);

                    if (connection.State == ConnectionState.Closed)
                        connection.Open();
                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        messages.Add(item: new Messages { Role = (string)reader["role"], Descrip = (string)reader["description"] });
                    }
                    connection.Close();
                }       
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
             return messages;
            }

Please advice further, if possible.  Thanks

Leave a Reply