I was following this tutorial in order to get a dropdown menu working with a database:
http://www.c-sharpcorner.com/UploadFile/4d9083/binding-dropdownlist-in-mvc-in-various-ways-in-mvc-with-data/
However, I encountered a problem.
Whilst following the tutorial, I got to this step: "For a Dapper User I am adding another class with the name MobileContext."
On line 3 of the code, it says:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MYConnector"].ToString());
^ This is where I get my runtime/ compile error.
When I try to implement the same into my program, I get the following error:
An exception of type 'System.NullReferenceException' occurred in Project_v3.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
Here are my relevant files:
Server Explorer view: http://gyazo.com/288a07eb2ec5c2aa4ea25d1eb6eda187
Contents of FlightsTable: http://gyazo.com/9d1b014ecdba1e244c2f6957b6d9397c
Table Layout of FlightsTable: http://gyazo.com/b8a25fd48e725dc4690ed54bb3b0cca2
FlightModel:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Project_v3.Models
{
[Table("FlightsTable")]
public class FlightModel
{
[Key]
public int FlightID { set; get; }
public string Departure { set; get; }
public string Arrival { set; get; }
public int NumberOfSeats { set; get; }
public int NumberOfFlights { set; get; }
[NotMapped]
public SelectList FlightList { get; set; }
}
}
FlightContext:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using Dapper;
using System.Data;
namespace Project_v3.Models
{
public class FlightContext
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MYConnector"].ToString());
public IEnumerable<FlightModel> GetFlightList()
{
string query = "SELECT [FlightID],[Departure]FROM [MyMainDBEntities2].[dbo].[FlightsList]";
var result = con.Query<FlightModel>(query);
return result;
}
}
}
MyTemplateController (basically my HomeController):
FlightContext FCon = new FlightContext();
public ActionResult BookFlight()
{
FlightModel FD = new FlightModel();
FD.FlightList = new SelectList(FCon.GetFlightList(), "FlightID", "Departure");
return View();
}
EDIT Web.config (as requested) however there are two:
Please See screenshot: http://gyazo.com/1bfb0886f82ac6dc2d1a739ddcb02999 https://gist.github.com/anonymous/e7552b6ee5609205ac18
Would somebody be able to point out what I'm doing wrong/ where I've messed up? What have I missed that's causing this error to continue appearing?
EDIT 3: It works however I now get this error on this line of FlightContext: var result = con.Query<FlightModel>(query)
;
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
Additional information: Invalid object name 'MyMainDBEntities2.dbo.FlightsList'.
EDIT 4:
AttachDbFilename="C:\Users\[MyName]\Desktop\ASP.NET Project v3 - (Original - Edit Version)\Project v3\Project v3\App_Data\MyMainDB.mdf";Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework