2015-07-19 170 views
1

我能够连接到我的网络上的服务器上的数据库,没有任何问题。但是,当我尝试使用SQL Server Express(本地数据库)时,我不断收到相同的错误。我创建了一个只有按钮和标签的新解决方案,以消除任何其他潜在问题。我已经抬起头,尝试几乎每个场景在stackoverflow和谷歌无济于事......我决定发布一个问题,因为我花了太多的时间在试图解决这个问题。感谢您的任何方向使用c#asp.net连接到SQL Server Express数据库

错误:

Object reference not set to an instance of an object.

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.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
DelWebApplication1.WebForm1.Button1_Click(Object sender, EventArgs e) in c:\Users\Rozelle\Documents\Visual Studio 2013\Projects\DelWebApplication1\DelWebApplication1\WebForm1.aspx.cs:23
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9628614
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

C#代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Configuration; 
using System.Data.SqlClient; 
using System.Data.Sql; 

namespace DelWebApplication1 
{ 
    public partial class WebForm1 : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
     } 

     protected void Button1_Click(object sender, EventArgs e) 
     { 
      string connectionString = ConfigurationManager.ConnectionStrings["MYConnectionString"].ToString(); 
      SqlConnection conn = new SqlConnection(connectionString); 

      try 
      { 
       conn.Open(); 
       Label1.Text = "Database Connected!!"; 
       conn.Close(); 
      } 
      catch (Exception ex) 
      { 
       Label1.Text = ex.Message; 
      } 
     } 
    } 
} 

Web.Config中:

<?xml version="1.0"?> 

<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=169433 
    --> 

<configuration> 
    <configSections> 
    </configSections> 
    <connectionStrings> 
     <add name="DelWebApplication1.Properties.Settings.MyConnectionString" 
     connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Rozelle\Documents\My.mdf;Integrated Security=True;Connect Timeout=30" 
     providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
    <system.web> 
     <compilation debug="true" targetFramework="4.5" /> 
     <httpRuntime targetFramework="4.5" /> 
    </system.web> 

</configuration> 
+1

我建议你去寻找你的连接字符串,我相信有一些* fishy *连接字符串。 :-) –

+1

我想你的配置文件中没有名为'MYConnectionString'的连接字符串。 –

+0

是的,我想尽可能多,我尝试了不同的字段的数据库属性:名称,连接字符串等....,但仍然没有成功 – suffa

回答

1

替换

<add name="DelWebApplication1.Properties.Settings.MyConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Rozelle\Documents\My.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient" /> 

<add name="MYConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Rozelle\Documents\My.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient" /> 

您的连接字符串名ConfigurationManager.ConnectionStrings["MYConnectionString"].ToString();有资本Y。您的C#代码和Web.config中的连接字符串名称都区分大小写,并且必须相同。

+0

是的,我改变了原来的名字,因为这个名字对我的工作很敏感,并且可能在那个时候创造了一个错字。不管怎么说,多谢拉。 – suffa

相关问题