Click here to Skip to main content
15,922,630 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have uploaded my project with database files and also attached both database files. But when I try to do anything it gives me following error.


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 149:    public int userid()
Line 150:    {
Line 151:        con.Open();
Line 152:        cmd3 = new SqlCommand("select max(usrid) from register", con);
Line 153:        id1 = Convert.ToString(cmd3.ExecuteScalar());

Source File: c:\MemberSites\MemberSites_AspSpider_Info\projectdemo\webroot\App_Code\class1.cs    Line: 151 

Stack Trace: 


[InvalidOperationException: The ConnectionString property has not been initialized.]
   System.Data.SqlClient.SqlConnection.PermissionDemand() +5092770
   System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection) +20
   System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +126
   System.Data.SqlClient.SqlConnection.Open() +125
   class1.userid() in c:\MemberSites\MemberSites_AspSpider_Info\projectdemo\webroot\App_Code\class1.cs:151
   register.Page_Load(Object sender, EventArgs e) in c:\MemberSites\MemberSites_AspSpider_Info\projectdemo\webroot\register.aspx.cs:32
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
Posted
Comments
King Fisher 2-Apr-14 7:51am    
connectionstring ?

Looks Like you Haven't Initialized Connection String

C#
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MEMBERSConnectionString2"].ConnectionString);
 
Share this answer
 
v3
Add Connection string in web.config file.

XML
<connectionStrings>
    <add name="Conn" connectionString="Data Source=Server Name;Initial Catalog=DB Name;User id=userid;Password=password;Persist Security Info=true;Pooling=true" providerName="System.Data.SqlClient"/>
  </connectionStrings>

Add Namespaces in C#,
C#
using System.Data.SqlClient;
using System.Configuration;

In your C# code, Initialize connection string.
 SqlConnection con = new SqlConnection();
        string cnstr = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString.ToString();
con.ConnectionString = cnstr;
                con.Open();
//your code
con.Close();
 
Share this answer
 
v2
The error message clearly states that you have not set the ConnectionString property of your SqlConnection object "con". So check the code where you are setting the value of this property and make sure that the code to set this property is called with a proper value of the connection string.
 
Share this answer
 
Silly mistake!! Solved it.
Thank you.

"Data Source=HOME\SQLEXPRESS;Initial Catalog=cloudimg;Integrated Security=True;Pooling=False"
 
Share this answer
 
The error is self-explanatory: The ConnectionString property has not been initialized.

So apart from above solution, you need to assign connection to your SqlCommand as well.
Like this,
C#
SqlConection con = new SqlConnection("YourDatabaseConnectionString");
con.Open();
SqlCommand cmd = new SqlCommand("YourQuery"); 
cmd.Connection = con;
// or, new SqlCommand(""YourQuery", con);
/*
...
...
...
*/
con.Close();


-KR
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900