我正在学习本教程,以获得一个使用数据库的下拉菜单:
http://www.c-sharpcorner.com/UploadFile/4d9083/binding-dropdownlist-in-mvc-in-various-ways-in-mvc-with-data/
然而,我遇到了一个问题。
在遵循本教程的同时,我完成了这一步:"对于Dapper用户,我将添加另一个名为MobileContext的类。"
在代码的第3行,它说:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MYConnector"].ToString());
^这是我得到运行时/编译错误的地方。
当我尝试在我的程序中实现相同的操作时,我收到以下错误:
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.
以下是我的相关文件:
Server Explorer视图:http://gyazo.com/288a07eb2ec5c2aa4ea25d1eb6eda187
FlightsTable的内容:http://gyazo.com/9d1b014ecdba1e244c2f6957b6d9397c
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 (基本上是我的HomeController):
FlightContext FCon = new FlightContext();
public ActionResult BookFlight()
{
FlightModel FD = new FlightModel();
FD.FlightList = new SelectList(FCon.GetFlightList(), "FlightID", "Departure");
return View();
}
编辑 Web.config(根据要求)但有两个:
请参见屏幕截图:http://gyazo.com/1bfb0886f82ac6dc2d1a739ddcb02999 https://gist.github.com/anonymous/e7552b6ee5609205ac18
有人能够指出我做错了什么/我搞砸了哪里?我错过了什么导致此错误继续出现?
编辑3 :但是我现在在这行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'.
编辑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
答案 0 :(得分:1)
在web.config中查找名为&#34; MYConnector&#34;
的连接字符串SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MYConnector"].ToString());
没有用名称&#34; MYConnector&#34;定义的连接字符串。在你的web.config中。 在web.config中创建连接字符串,如下所示
web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="MYConnector" providerName="System.Data.SqlClient" connectionString="Data Source=111.111.111.111;Initial Catalog=DatabaseName;user id=UserName;password=Password;" />
</connectionStrings>
答案 1 :(得分:0)
您的web.config没有名为&#34; MYConnector&#34;的连接字符串,请尝试将其添加到web.config中:
<connectionStrings>
<add name="MYConnector" providerName="System.Data.SqlClient" connectionString="ConnectionStringHere" />
</connectionStrings>
以下是构建连接字符串的参考:
http://www.connectionstrings.com/
在这种情况下,为什么不将连接字符串存储为字符串?
<configSections>
<appSettings>
<add key="MYConnector" value="ConnectionString"/>
</appSettings>
</configSections>
在您的代码中:
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["MYConnector"].ToString());