The ConnectionString property has not been initialized error

Last Reply on Aug 01, 2012 10:54 AM By Mudassar

Posted on Aug 01, 2012 09:54 AM

Hi folks,

I am getting above mentioned error when I try to run the following code:

 Private strConnString As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
    Private con As New SqlConnection(strConnString)
    Private pID As Integer
'FIRST SECTION Dim query As String = "query" Dim cmd As New SqlCommand(query) cmd.Parameters.Add("@pID", SqlDbType.Int).Value =pID Using con Using sda As New SqlDataAdapter() Try cmd.Connection = con Catch ex As Exception Response.Write("Error" & ex.Message) Finally con.Close() End Try sda.SelectCommand = cmd Using ds As New DataSet() sda.Fill(ds) 'output End Using End Using End Using

'SECOND SECTION Dim cmd2 As New SqlCommand("query", con) Try con.Open() cmd2.ExecuteNonQuery() Dim reader As SqlDataReader = cmd2.ExecuteReader() While reader.Read() Dim rr As String = reader.Item("dID").ToString Dim returnedRows As String = reader.FieldCount.ToString() If rr = "True" Then 'output ElseIf rr = "False" Then 'output End If End While reader.Close() Catch ex As Exception Response.Write("error" & ex.Message) 'Error is beign displayed here: The ConnectionString property has not been initialized Finally con.Close() End Try

The error is coming from the second SQL cmd but if I comment out the first section; the second section runs fine.

I must have made another basic error but I just can't figure out what it is. Any help would be great. Thanks.

Posted on Aug 01, 2012 10:54 AM

You will have to again do

con As New SqlConnection(strConnString)

before you intialize cmd2 as you are using USING which diposes the connection object