2011-03-28 76 views
3

我有一個C#ASP.NET Web應用程序我想填充一個ASP:DropDownList與數據庫表中的列。C#SqlCommand Connection.Open()問題

我的代碼:

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

namespace CRM2Sage 
{ 
    public partial class SOPOrderEntry : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      Fill1(); 
     } 

     public void Fill1() 
     { 
      SqlCommand cmd = new SqlCommand("SELECT * FROM Products", new SqlConnection(WebConfigurationManager.AppSettings["CRM2Sage"])); 
      //cmd.Connection.Open(); 
      cmd.Connection.Open(); 

      SqlDataReader ddlValues; 
      ddlValues = cmd.ExecuteReader(); 

      vproduct.DataSource = ddlValues; 
      vproduct.DataValueField = "theName"; 
      vproduct.DataTextField = "theName"; 
      vproduct.DataBind(); 

      cmd.Connection.Close(); 
      cmd.Connection.Dispose(); 
     } 
    } 
} 

的,當我運行頁面,我得到以下錯誤

ConnectionString屬性已 尚未初始化。

指向cmd.Connection.Open();我不明白爲什麼,SQL連接存儲在我的web.config文件中。

web.config

<?xml version="1.0"?> 
<configuration> 

    <appSettings /> 
    <connectionStrings> 
     <add name="CRM2Sage" connectionString="Data Source=W2003CRMDEMO; Initial Catalog=CRM2Sage; User ID=newSA; Password=password;" providerName="System.Data.SqlClient" /> 
    </connectionStrings> 

    <system.web> 
     <compilation debug="true"> 

     </compilation> 
     <!-- 
      The <authentication> section enables configuration 
      of the security authentication mode used by 
      ASP.NET to identify an incoming user. 
     --> 
     <authentication mode="Windows" /> 
     <!-- 
      The <customErrors> section enables configuration 
      of what to do if/when an unhandled error occurs 
      during the execution of a request. Specifically, 
      it enables developers to configure html error pages 
      to be displayed in place of a error stack trace. 

     <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> 
      <error statusCode="403" redirect="NoAccess.htm" /> 
      <error statusCode="404" redirect="FileNotFound.htm" /> 
     </customErrors> 
     --> 
    </system.web> 
</configuration> 

誰能幫助?

乾杯

賈斯汀

+0

我沒有看到任何錯誤? – 2011-03-28 14:45:29

回答

6

您需要檢索.ConnectionString屬性:

string connectionString = WebConfigurationManager.ConnectionStrings["CRM2Sage"].ConnectionString; 

using(SqlConnection _con = new SqlConnection(connectionString)) 
using(SqlCommand cmd = new SqlCommand("SELECT * FROM Products", _con)) 
{ 
    // do your stuff here 
} 

你是什麼現在正在執行的是<connectionStrings>下的CRM2Sage這個名字。

+0

+1添加代碼示例 – IAbstract 2011-03-28 14:51:55

3

的問題是,你正在訪問AppSettings,但你要訪問的連接字符串:

new SqlConnection(WebConfigurationManager.ConnectionStrings.ConnectionStrings["CRM2Sage"].ConnectionString) 
0

你可以在asp.net使用

System.Web.Configuration.WebConfigurationManager.ConnectionStrings["CRM2Sage"].ConnectionString; 

Or 
System.Configuration.ConfigurationManager.ConnectionStrings["CRM2Sage"].ConnectionString; 

。但不是

WebConfigurationManager.AppSettings["CRM2Sage"]