Advertisement
Guest User

Untitled

a guest
Dec 11th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. Server Error in '/' Application.
  2.  
  3. Object reference not set to an instance of an object.
  4.  
  5. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
  6.  
  7. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
  8.  
  9. Source Error:
  10.  
  11.  
  12.  
  13. Line 241: lobbyViewModel.lobby.name = lobbyViewModel.name;
  14. Line 242: lobbyViewModel.lobby.category.id = categoryId;
  15. Line 243: lobbyViewModel.lobby.isStarted = false;
  16. Line 244: lobbyClient.CreateLobby(lobbyViewModel.lobby);
  17.  
  18. [DataContract(IsReference = true)]
  19. public class Category
  20. {
  21. [DataMember]
  22. public int id { get; set; }
  23. [DataMember]
  24. public string name { get; set; }
  25. [DataMember]
  26. public int amount { get; set; }
  27. [DataMember]
  28. public Quiz quiz { get; set; }
  29. [DataMember]
  30. public List<Question> question { get; set; }
  31.  
  32. public Category()
  33. {
  34. question = new List<Question>();
  35. }
  36. }
  37.  
  38. public class Lobby
  39. {
  40. [DataMember]
  41. public int id { get; set; }
  42. [DataMember]
  43. public string name { get; set; }
  44. [DataMember]
  45. public Category category { get; set; }
  46. [DataMember]
  47. public bool isStarted { get; set; }
  48. }
  49.  
  50. public class LobbyViewModel
  51. {
  52. public LobbyReference.Lobby lobby { get; set; }
  53. public List<LobbyReference.Lobby> lobbies { get; set; }
  54.  
  55. public LobbyViewModel()
  56. {
  57. lobby = new LobbyReference.Lobby();
  58. lobbies = new List<LobbyReference.Lobby>();
  59. }
  60.  
  61. public int id { get; set; }
  62. public string name { get; set; }
  63. public int categoryId { get; set; }
  64. public bool isStarted { get; set; }
  65. }
  66.  
  67. public void CreateLobby(Lobby lobby)
  68. {
  69. TransactionOptions options = new TransactionOptions();
  70. options.IsolationLevel = System.Transactions.IsolationLevel.Serializable;
  71.  
  72. using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options))
  73. {
  74. using (var conn = new SqlConnection(connectionString))
  75. {
  76. using (SqlCommand command = conn.CreateCommand())
  77. {
  78. conn.Open();
  79. command.CommandText = "INSERT INTO Lobby(name, categoryId, isStarted)VALUES(@name, @categoryId, @isStarted)";
  80. command.Parameters.Add("name", SqlDbType.NVarChar).Value = lobby.name;
  81. command.Parameters.Add("categoryId", SqlDbType.Int).Value = lobby.category.id;
  82. command.Parameters.Add("isStarted", SqlDbType.Bit).Value = lobby.isStarted;
  83. command.ExecuteNonQuery();
  84. }
  85. scope.Complete();
  86. conn.Close();
  87. }
  88. }
  89. }
  90.  
  91. // THIS IS MY WCF REFERENCE WHERE I CAN CALL MY METHODS FROM
  92. private LobbyServiceClient lobbyClient = new LobbyServiceClient();
  93.  
  94. public ActionResult Lobby(int categoryId)
  95. {
  96. LobbyViewModel lobbyViewModel = new LobbyViewModel();
  97. lobbyViewModel.lobbies = lobbyClient.GetAllLobbiesWithCategoryId(categoryId);
  98. lobbyViewModel.categoryId = categoryId;
  99. return View(lobbyViewModel);
  100. }
  101.  
  102. public ActionResult CreateLobby(int categoryId)
  103. {
  104. LobbyViewModel lobbyViewModel = new LobbyViewModel();
  105. lobbyViewModel.categoryId = categoryId;
  106. return View(lobbyViewModel);
  107. }
  108.  
  109. [HttpPost]
  110. public ActionResult CreateLobby(LobbyViewModel lobbyViewModel, int categoryId)
  111. {
  112. lobbyViewModel.lobby.name = lobbyViewModel.name;
  113. lobbyViewModel.lobby.category.id = categoryId;
  114. lobbyViewModel.lobby.isStarted = false;
  115. lobbyClient.CreateLobby(lobbyViewModel.lobby);
  116. return RedirectToAction("Lobby");
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement