0

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.

4
  • use var string = ConfigurationManager.ConnectionStrings["PMIcommConnectionString"].ConnectionString; Commented Apr 3, 2014 at 9:48
  • Can you show how you have specified your connectionstring in web.config? Commented Apr 3, 2014 at 9:50
  • @RKS I have updated my question with the connection string Commented Apr 3, 2014 at 9:53
  • 1
    @Nullbyte: You have specified this within <connectionString>. Just follow the answer provided by Neel. That is correct. Commented Apr 3, 2014 at 9:57

2 Answers 2

1

The connection string is not in AppSettings.

What you're looking for is in:

SqlConnection conn = new SqlConnection();

conn.ConnectionString = ConfigurationManager.ConnectionStrings["PMIcommConnectionString"].ConnectionStri‌​ng;
Sign up to request clarification or add additional context in comments.

3 Comments

I have update my question with the code as it is now after your answer. But I still have the same problem.
what exact error you are getting and at which step after the change @Nullbyte
hey btw I have just given the example I mean I have just noticed you have placed exact code in your code..just make neccessory changes instead of putting exact code @Nullbyte
1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.