我有一个选择了sqlDataSource2的源码的网格。我没有在数据源中构建任何查询。我有一个包含两个项目的下拉列表,我想从下拉列表中选择查询,并在选择查询后更新网格以显示结果。 这是我到目前为止所尝试的:
protected void Page_Load(object sender, EventArgs e)
{
Query1();
}
protected void Query1()
{
//if (this.IsPostBack)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["PMIcommConnectionString"].ConnectionString;
SqlDataSource2.SelectCommand = @"SELECT YEAR(custDecDate), SUM(valueXX), SUM(valueYY)
FROM bids
WHERE forBid ='"+ DropDownList3.SelectedValue +"'GROUP BY YEAR(custDecDate)'";
SqlDataSource2.DataBind();
RadGrid1.DataBind();
}
}
这是我的连接字符串:
<add name="PMIcommConnectionString" connectionString="Data
Source=WIN-72PL3253COR\SQLEXPRESS;Initial Catalog=PMIcomm;Integrated
Security=True" providerName="System.Data.SqlClient" />
我在最后一行收到错误“连接字符串尚未初始化”。我怎样才能使这个工作?除了我得到的错误,我不确定这是否是正确的方法。很抱歉问这么简单,我是一个自学初学者。
答案 0 :(得分:1)
连接字符串不在AppSettings中。
您正在寻找的是:
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["PMIcommConnectionString"].ConnectionString;
答案 1 :(得分:1)
的Web.config:
<connectionStrings>
<add name="PMIcommConnectionString" connectionString="Data
Source=WIN-72PL3253COR\SQLEXPRESS;Initial Catalog=PMIcomm;Integrated
Security=True"/>
</connectionStrings>
代码背后.cs:
using System.Configuration;
using System.Data.SqlClient;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["PMIcommConnectionString"].ConnectionString);