0

I'm new in asp.net. The problem that I face that I am unable to make a booking from book button. All the data are stored in the table storage in azure which is no null value. But the problem that I get is NullReferenceException: Object reference not set to an instance of an object. AspNetCore.Views_Table_bookdata.ExecuteAsync() in bookdata.cshtml Code line: 59 @{ var displayresult = Model.Result as ddacassignment.Models.ServicesEntity;} Anyone know how to fix this?? Thanks

ServiceEntity File

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage.Table;

namespace ddacassignment.Models
{
    public class ServicesEntity : TableEntity
    {
        public ServicesEntity() { }
        public ServicesEntity(String service , string company)
        {
            this.PartitionKey = service;
            this.RowKey = company;
        }

        
       
        public DateTime Schedule { get; set; }
        public double Price { get; set; }

        public string customerUsername { get; set; }

        public bool isBooked { get; set; }

        public bool isConfirmed { get; set; }
    }
}

Table Controller (Book data) method

public ActionResult bookdata(string PartitionKey, string RowKey)
        {
            CloudTable table = getTableStorageInformation();
            string errormessage = null;

            //get current username 
            var myusername = this.userManager.GetUserName(HttpContext.User);
            try
            {
                TableOperation retrieveOperation = TableOperation.Retrieve<ServicesEntity>(PartitionKey, RowKey);

                //Execute the operation
                TableResult result = table.ExecuteAsync(retrieveOperation).Result;

                //asign the result to item objct
                ServicesEntity updateEntity = (ServicesEntity)result.Result;

                //change the description 
                updateEntity.isBooked = true;
                updateEntity.customerUsername = myusername;

                //create the inssertorreplace tableoperation
                TableOperation insertorReplaceOperation = TableOperation.InsertOrReplace(updateEntity);

                //execute the operation
                TableResult resultof = table.ExecuteAsync(insertorReplaceOperation).Result;
                ViewBag.Result = result.HttpStatusCode;
                return View(resultof);
            }
            catch (Exception ex)
            {
                ViewBag.msg = "Technical Error: " + ex.ToString();
            }
            ViewBag.msg = errormessage;

            return View();
        }

The view code

@model Microsoft.WindowsAzure.Storage.Table.TableResult


@{ ViewData["Title"] = "Bookdata"; }
<style>
    body {
        font-family: 'lato', sans-serif;
    }

    .container {
        max-width: 1000px;
        margin-left: auto;
        margin-right: auto;
        padding-left: 10px;
        padding-right: 10px;
    }

    th {
        background-color: #95A5A6;
        font-size: 14px;
        text-transform: uppercase;
        letter-spacing: 0.03em;
        padding: 20px;
    }

    tr {
        background-color: #ffffff;
        box-shadow: 0px 0px 9px 0px rgba(0,0,0,0.1);
    }

    td {
        padding: 20px;
    }
</style>
<h1> Summary of Booked Slot</h1>
@if (ViewBag.Message != null)
{

<p>@ViewBag.Message</p> }

            else
            {
            <table border="1">
                <tr>

                    <th style="width:15%">Service Name</th>
                    <th style="width:15%">Company name</th>
                    <th style="width:15%">username</th>
                    <th style="width:15%">Schedule</th>
                    <th style="width:15%">Price</th>
                    <th style="width:15%">isBooked</th>
                    <th style="width:15%">Customerusername</th>
                    <th style="width:15%">isConfirmed</th>


                </tr>

                <tr>
                    @{ var displayresult = Model.Result as ddacassignment.Models.ServicesEntity;}

                    <td style="width:15%">@displayresult.PartitionKey</td>
                    <td style="width:15%">@displayresult.RowKey</td>
                    <td style="width:15%">@displayresult.Schedule</td>
                    <td style="width:15%">@displayresult.Price</td>
                    <td style="width:15%">@displayresult.isBooked</td>
                    <td style="width:15%">@displayresult.customerUsername</td>
                    <td style="width:15%">@displayresult.isConfirmed</td>



                </tr>



</table>}
New user
  • 125
  • 1
  • 12

0 Answers0