Results 1 to 4 of 4

Thread: [RESOLVED] [2005] The ConnectionString property has not been initialized

  1. #1

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Resolved [RESOLVED] [2005] The ConnectionString property has not been initialized

    I'm getting this weird error part of the way through the execution of my page, I know that the connection string exists and that earlier in the execution the connection opened successfully.

    The ConnectionString property has not been initialized

    Log File -
    Code:
    At the top of my page (page load)
    Connection String: server=localhost;uid=myuid;pwd=mypwd;database=mydb
    Connection State: Closed
    In using block: Connection State: Open
    Out Using block Connection State: Closed
    Submission page: 3
    before GetLabelsAndDefaults
    Successful DB access here
    after GetLabelsAndDefaults
    before LoadAuthors
    In LoadAuthors
    In Using block
    Connection state: Closed
    Connection String: server=localhost;uid=myUID;pwd=myPwd;database=mydb
    Attempt to open connection Failed (The ConnectionString property has not been initialized)
    Here is the offending subs -

    vb Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         sSubPage = clAct.Decrypt(clAct.DeNull(Request.QueryString("pg")))
    3.         sMS_No = clAct.Decrypt(clAct.DeNull(Request.QueryString("ms_no")))
    4.         sMtg_No = clAct.DeNull(Request.QueryString("Meeting_No"))
    5.         sDoc_Type = clAct.DeNull(Request.QueryString("dt"))
    6.  
    7.         Call clAct.WriteToFile("Connection String: " & ConfigurationManager.ConnectionStrings("Sirius").ConnectionString & vbNewLine)
    8.         Call clAct.WriteToFile("Connection State: " & myCon.State.ToString & vbNewLine)
    9.         Using myCon
    10.             If myCon.State = ConnectionState.Closed Then myCon.Open()
    11.             Call clAct.WriteToFile("In using block: Connection State: " & myCon.State.ToString & vbNewLine)
    12.         End Using
    13.         Call clAct.WriteToFile("Out Using block Connection State: " & myCon.State.ToString & vbNewLine)
    14.  
    15.         If Page.IsPostBack = False Then
    16.             clAct.LoginCheck() ' Ensure that the user is logged in.
    17.  
    18.             ' Get the menu
    19.             litMenu.Text = menu.GetMenu(sSubPage.ToString).ToString
    20.  
    21.         Call clAct.WriteToFile("before GetLabelsAndDefaults" & vbNewLine)
    22.         GetLabelsAndDefaults()
    23.         Call clAct.WriteToFile("after GetLabelsAndDefaults" & vbNewLine)
    24.         Call clAct.WriteToFile("before LoadAuthors" & vbNewLine)
    25.         LoadAuthors() ' binds and loads the authors Datagrid
    26.         Call clAct.WriteToFile("after LoadAuthors" & vbNewLine)
    27.         End If
    28.     End Sub
    29.  
    30.     Private Sub LoadAuthors()
    31.         Call clAct.WriteToFile("In LoadAuthors" & vbNewLine)
    32.         Try
    33.             Using myCon
    34.                 Call clAct.WriteToFile("In Using block" & vbNewLine)
    35.                 Call clAct.WriteToFile("Connection state: " & myCon.State.ToString & vbNewLine)
    36.                 If myCon.State = ConnectionState.Closed Then
    37.                     Call clAct.WriteToFile("Connection String: " & ConfigurationManager.ConnectionStrings("Sirius").ConnectionString & vbNewLine)
    38.                     myCon.InitializeLifetimeService()
    39.                     myCon.Open()
    40.                 End If
    41.                 Call clAct.WriteToFile("after connection state check" & vbNewLine)
    42.                 Call clAct.WriteToFile("before CreateDataTable" & vbNewLine)
    43.                 Dim dt As DataTable = CreateDataTable()
    44.                 Call clAct.WriteToFile("after CreateDataTable" & vbNewLine)
    45.                 Dim dr As DataRow
    46.                 Dim myReader As SqlDataReader
    47.                 Dim myCmd As New SqlCommand
    48.                 With myCmd
    49.                     .CommandType = CommandType.StoredProcedure
    50.                     .CommandText = "up_gGetMS_Authors"
    51.                     .Connection = myCon
    52.                     .Parameters.AddWithValue("@MS_No", sMS_No.ToString)
    53.                     .Parameters.AddWithValue("@Version_No", Convert.ToInt16(1))
    54.                     myReader = .ExecuteReader() ' Open the DataReader
    55.                 End With
    56.  
    57.                 If myReader.HasRows = True Then
    58.                     Do While myReader.Read()
    59.                         dr = dt.NewRow()
    60.                         dr(0) = Convert.ToString(clAct.DeNull(myReader("Author_ID")))
    61.                         dr(1) = Convert.ToString(clAct.DeNull(myReader("User_ID")))
    62.                         dr(2) = Convert.ToString(clAct.DeNull(myReader("First_Name")))
    63.                         dr(3) = Convert.ToString(clAct.DeNull(myReader("Middle_Name")))
    64.                         dr(4) = Convert.ToString(clAct.DeNull(myReader("Last_Name")))
    65.                         dr(5) = Convert.ToString(clAct.DeNull(myReader("Affiliation")))
    66.                         dr(6) = Convert.ToBoolean(clAct.DeNull(myReader("Presenting_Auth")))
    67.                         dt.Rows.Add(dr)
    68.                     Loop
    69.                     myReader.Close()
    70.                     myReader = Nothing
    71.                 Else
    72.                     ' This page has not been saved before; add the submitting authors details into the Author datagrid by default
    73.                     Dim bUCaseLName As Boolean = clAct.GetJnl_MS_Authors_UCase_Last_Name()
    74.  
    75.                     myReader = clAct.Find_Author(clAct.GetCookie("UserDetails", "Email").ToString)
    76.                     If myReader.Read() Then
    77.                         dr = dt.NewRow()
    78.                         dr(0) = ""
    79.                         dr(1) = Convert.ToString(clAct.DeNull(myReader("User_ID")))
    80.                         dr(2) = Convert.ToString(clAct.DeNull(myReader("First_Name")))
    81.                         dr(3) = Convert.ToString(clAct.DeNull(myReader("Middle_Name")))
    82.                         dr(4) = IIf(bUCaseLName = True, Convert.ToString(clAct.DeNull(myReader("Last_Name"))).ToUpper, Convert.ToString(clAct.DeNull(myReader("Last_Name"))).ToString)
    83.                         dr(5) = Convert.ToString(clAct.DeNull(myReader("Affiliation")))
    84.                         dr(6) = Convert.ToBoolean(True)
    85.                         dt.Rows.Add(dr)
    86.                     End If
    87.                     myReader.Close()
    88.                     myReader = Nothing
    89.                 End If
    90.                 dgAuthors.DataSource = dt
    91.                 dgAuthors.DataBind()
    92.             End Using
    93.         Catch ex As Exception
    94.             Throw ex
    95.         End Try
    96.     End Sub

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2005] The ConnectionString property has not been initialized

    Hello there,

    Where do you set up myCon?

    I am not one hundred percent sure about this, but it is my belief that the using, once it finishes it;s execution calls the dispose on the object in question, i.e. to tidy up resources. So when you try and access the myCon a second time, it no longer no what the connection string is.

    The way that I approach this is to explicity state the connection string each time I connect to the database, the following code is for connecting to a MySql Database, but the theory is the same (oh, and it is in C# ):

    Code:
    public override List<CategoryDetails> GetCategories()
            {
                using (MySqlConnection cn = new MySqlConnection(this.ConnectionString))
                {
                    MySqlCommand cmd = new MySqlCommand("ARTICLES_GetCategories", cn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cn.Open();
                    return GetCategoryCollectionFromReader(ExecuteReader(cmd));
                }
            }
    Hope this helps!!

    Gary

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2005] The ConnectionString property has not been initialized

    gep is right. the point of the Using syntax is to create a variable, instanciate it... then use it within that block, and when the End Using it encountered, the object is released and dispose is called on the object. This renders it useless.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: [2005] The ConnectionString property has not been initialized

    Gep and Techgnome,

    Thanks a bunch that's sorted it

    Much appreciated

    Cheers Al

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width