I have a C# ASP.NET web application an I am trying to populate an ASP:DropDownList with a column in a database table.
My Code:
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();
}
}
}
An when I run the page I get the following error
The ConnectionString property has not been initialized.
pointing to cmd.Connection.Open();
I can not understand why, the SQL connection is stored in my web.config file.
The 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>
Can anyone help?
Cheers
Justin
You need to retrieve the .ConnectionString
property :
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
}
What you're doing right now is just retrieving the whole entry under <connectionStrings>
by the name of CRM2Sage
.
The problem is, that you are accessing the AppSettings
, but you want to access the connection strings:
new SqlConnection(WebConfigurationManager.ConnectionStrings.ConnectionStrings["CRM2Sage"].ConnectionString)
you can use
System.Web.Configuration.WebConfigurationManager.ConnectionStrings["CRM2Sage"].ConnectionString;
Or
System.Configuration.ConfigurationManager.ConnectionStrings["CRM2Sage"].ConnectionString;
in asp.net. but not
WebConfigurationManager.AppSettings["CRM2Sage"]
精彩评论