The ConnectionString property has not been initialized

Last post 02-05-2006 7:21 PM by Motley. 6 replies.

Sort Posts:

  • The ConnectionString property has not been initialized

    02-05-2006, 11:18 AM
    • Contributor
      4,291 point Contributor
    • Peter Smith
    • Member since 03-19-2005, 9:58 AM
    • Posts 1,608
    I have installed SQL Server 2005 Express Edition.
    I have defined the connectionstring in my web.config as follows:

    <connectionStrings>
    <
    add name="MyDB" connectionString="Server=local;Provider=SQLOLEDB;Initial Catalog=Shop;Trusted_Connection=Yes;DataSource==.\SQLExpress;AttachDBFilename=Data/MyDB.mdf" providerName="System.Data.SqlClient"/>
    </
    connectionStrings>
    Here's my vb code:

    Dim connStr As String = ConfigurationManager.ConnectionStrings("MyDB").ToString()
    Dim DBConnection As New SqlConnection
    Dim SQLCmd As New SqlCommand("SELECT * FROM tblMember WHERE UserName=@UserName", DBConnection)
    SQLCmd.Parameters.Add(New SqlParameter("@UserName", tbUserName.Text))
    DBConnection.Open()
    and on that last line I receive the error: The ConnectionString property has not been initialized


    Now, I have seen SO many different versions of a connectionstring that im totally confused!!
    I want to use windows authentication (I know thats better for security reasons).
    But I have difficulties understanding the attributes required (AND THEIR MEANING!) of the connectionstring. (e.g. do I need to define DataSource or Initial Catalog or AttachDBFileName and the rest? What does it do exactely?)

    Also: I have registered my server as "MyServer" is that the alias I need/can use in my connectionstring?

    If someone could explain me the above questions it would be of GREAT help!!

    please mark answers as 'Answered' and post back solutions when you figure stuff out that isnt in the post already.
  • Re: The ConnectionString property has not been initialized

    02-05-2006, 11:22 AM
    • All-Star
      41,904 point All-Star
    • tmorton
    • Member since 08-06-2002, 9:37 PM
    • SE Pennsylvania, USA
    • Posts 6,974
    • ASPInsiders
      Moderator
    Hi Peter, you have populated connStr with your desired connection string, but you do not use this variable anywhere in your code.  Try altering this line with the code in red:
    Dim DBConnection As New SqlConnection(connStr)
    Terri Morton
    ASP.NET Website Manager, Neudesic
  • Re: The ConnectionString property has not been initialized

    02-05-2006, 11:31 AM
    • Contributor
      4,291 point Contributor
    • Peter Smith
    • Member since 03-19-2005, 9:58 AM
    • Posts 1,608
    haha, ok, that was stupid of me ;) but thanks. I altered it and changed my connectionstring setting in my web.config to:

    <add name="MyDB" connectionString="Server=local;Initial Catalog=Dating;Trusted_Connection=Yes;" providerName="System.Data.SqlClient"/>But now I receive the following error:

    Server Error in '/' Application.

    An error has occurred while establishing a connection to the server.  When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

    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.Data.SqlClient.SqlException: An error has occurred while establishing a connection to the server.  When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

    Source Error:

    Line 17:             'Dim SQLCmd As New OleDbCommand("SELECT * FROM tblMember WHERE UserName=@UserName", DBConnection)
    Line 18:             SQLCmd.Parameters.Add(New SqlParameter("@UserName", tbUserName.Text))
    Line 19:             DBConnection.Open()
    Line 20:             reader = SQLCmd.ExecuteReader
    Line 21:             If reader.Read Then

    please mark answers as 'Answered' and post back solutions when you figure stuff out that isnt in the post already.
  • Re: The ConnectionString property has not been initialized

    02-05-2006, 11:47 AM
    • All-Star
      41,904 point All-Star
    • tmorton
    • Member since 08-06-2002, 9:37 PM
    • SE Pennsylvania, USA
    • Posts 6,974
    • ASPInsiders
      Moderator
    I am not going to be able to help you debug that error, but I have seen it come up a bunch of times in these forums.  Check out this post for some pointers: http://forums.asp.net/1110412/ShowPost.aspx, and if that doesn't help, use this search string to look for other posts on the topic:
        "Named Pipes Provider, error: 40 - Could not open a connection"
    Terri Morton
    ASP.NET Website Manager, Neudesic
  • Re: The ConnectionString property has not been initialized

    02-05-2006, 7:04 PM
    • Star
      12,857 point Star
    • Motley
    • Member since 10-14-2005, 1:26 PM
    • West Chicago, IL
    • Posts 2,297
    • TrustedFriends-MVPs

    That error message is saying that you couldn't even make a low level connection at all to the server you are requesting.  It's a little confusing, but I guess the logic goes that because you are requesting a connection to a SQL Express machine, the problem is most likely because SQL Express doesn't allow remote connections.

    Your problem however, is that your connection string is wrong.

  • Re: The ConnectionString property has not been initialized

    02-05-2006, 7:10 PM
    • Star
      12,857 point Star
    • Motley
    • Member since 10-14-2005, 1:26 PM
    • West Chicago, IL
    • Posts 2,297
    • TrustedFriends-MVPs

    You seem to have a bunch of stuff specified using the old OLEDB syntax mixed in with some newer style SQL native syntax.  Try this:

    <add name="MyDB" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyDB.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>

    If your registered your SQL Instance as MyServer instead of the default SQLEXPRESS, you'll need to change that as well to:

    <add name="MyDB" connectionString="Data Source=.\MyServer;AttachDbFilename=|DataDirectory|\MyDB.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>

    Please note that it expects the database in the App_Data directory.  Please put it there until you get it working and have a reason to put it elsewhere.

  • Re: The ConnectionString property has not been initialized

    02-05-2006, 7:21 PM
    • Star
      12,857 point Star
    • Motley
    • Member since 10-14-2005, 1:26 PM
    • West Chicago, IL
    • Posts 2,297
    • TrustedFriends-MVPs

    Here's a quick walkthrough for VS 2005:

    In your window titled "Solution Explorer", right click the folder "App_Data". Select "Add new item".  When the dialog appears, select the SQL Database, and change the name from "Database.mdf" to whatever you want (I'm guessing MyDB.mdf).  Click ok.

    Go to a webpage you have (Default.aspx if you want), and drag a sqldatasource onto the page.  A smart tag should pop up with "Configure data source", click that.  If you didn't get one, or some how missed the option right click the sqldatasource, and select "Configure data source".  A dialog box will pop up.  It will ask what connection string you want to use.  Select "MyDB.mdf" from the dropdown (Or whatever you called your database), and click next.  Then it will ask you if you want to place the connection string in the web.config file.  Leave the checkbox checked, and change the name from ConnectionString to MyDB (That's what you had before, but I usually leave it as ConnectionString).

    Finish the wizard by telling it what you want to select from the database, or if there is nothing in the database yet, then just tell it to select something bogus for now, like "SELECT 1" as the custom sql string, click next, next, finish.  The correct connection string is now in your web.config file.

    To test that it is actually working, go ahead, and toss down a gridview on the page, tell it the datasource, and run the page.  You should see one row, one column, with a "1" in it (Ok, there are actually two rows, the header row which should be called "Column1", and the data row).  That verified that the connection string is working, you connected, asked for data, the data was returned and displayed.

Page 1 of 1 (7 items)