Reputation: 251
I have a grid which has a dasource selected sqlDataSource2. I have not built any query in the datasource. I have a dropdownlist with two items and I would like to select the query from the dropdownlist and after selection of the query update the grid to show the result. This is what I have tried so far:
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();
}
}
This is my connection string:
<add name="PMIcommConnectionString" connectionString="Data
Source=WIN-72PL3253COR\SQLEXPRESS;Initial Catalog=PMIcomm;Integrated
Security=True" providerName="System.Data.SqlClient" />
I get an error "Connection string has not been initialized" on the last line. How can I make this working? Beside the error I am getting, I am not sure if this is the correct way to do this. Sorry for asking such simple thing, I am a selflearning beginner.
Upvotes: 0
Views: 57
Reputation: 5211
Web.config:
<connectionStrings>
<add name="PMIcommConnectionString" connectionString="Data
Source=WIN-72PL3253COR\SQLEXPRESS;Initial Catalog=PMIcomm;Integrated
Security=True"/>
</connectionStrings>
Code Behind .cs:
using System.Configuration;
using System.Data.SqlClient;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["PMIcommConnectionString"].ConnectionString);
Upvotes: 1
Reputation: 11741
The connection string is not in AppSettings.
What you're looking for is in:
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["PMIcommConnectionString"].ConnectionString;
Upvotes: 1