none
ConnectionString property has not been initialized in C# With Asp.net 2.0

    Question

  • The ConnectionString property has not been initialized.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: The ConnectionString property has not been initialized.

    Source Error:

    Line 23: 
    Line 24:             con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnString"]);
    Line 25:             con.Open();
    

     

     code behind :-

     

    con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnString"]);

    con.Open();

     

    Web.Config :-

     

    <add name="ConnString" connectionString="Data Source=HOMEPC;Initial Catalog=Test;User ID=sa;Password=m19" providerName="System.Data.SqlClient" />

     

    Please reply

     

    Thanks

    Sunday, May 13, 2007 5:52 AM

Answers

All replies

  • You probably need to use

    System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"]

    Sunday, May 13, 2007 6:06 AM
  • Thanks ,

    New error is coming  in

     

    SqlConnection con;

     

    con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings ["ConnString"]);

    con.Open();

     

    Error 2 The best overloaded method match for 'System.Data.SqlClient.SqlConnection.SqlConnection(string)' has some invalid arguments 

    Error 3 Argument '1': cannot convert from 'System.Configuration.ConnectionStringSettings'

    Sunday, May 13, 2007 6:57 AM
  • System.Configuration.ConfigurationManager.ConnectionStrings ["ConnString"].ConnectionString
    Sunday, May 13, 2007 7:08 AM
  • Thanks,

     Error is removed but data not showing  in GridView .

     

    if (!Page.IsPostBack)

    {

    SqlConnection con;

    SqlCommand cmdselect;

    SqlDataReader dtcountry;

    con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings ["ConnString"].ConnectionString);

    con.Open();

    cmdselect = new SqlCommand("Select Country_Name From country", con);

    dtcountry = cmdselect.ExecuteReader();

    GridView1.DataSource = dtcountry;

    GridView1.DataBind();

    dtcountry.Close();

    con.Close();

    }

     

    Please reply

     

    Thanks

    Sunday, May 13, 2007 7:51 AM
  • datasource usually is datatable or dataset. Please refer to http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource.aspx
    Sunday, May 13, 2007 8:10 AM