locked
ExecuteReader: Connection property has not been initialized RRS feed

  • Question

  • i need your help......

    what's wrong from my sintax below

    Public

    Class Form1

     

    Dim conn As SqlConnection

     

    Dim da As SqlDataAdapter

    Dim dr As SqlDataReader

     

    Dim dc As SqlCommand

     

    Dim ds As DataSet

     

    Dim str As String

     

     

    Sub CONNEC()

    str =

    "Data Source=T0015H;Initial Catalog=T0056AM;Integrated Security=True"
     

    conn =

    New SqlConnection(str)

    conn.Open()

     

    End Sub
     

     

    Private

    Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress

    TextBox2.MaxLength = 9

     

    If e.KeyChar = Chr(13) Then

     

             If TextBox2.Text = "" Then

             MsgBox(

    "XXXXXXXXX")

    TextBox2.Focus()

     

    Else

    dc =

    New SqlCommand("Select * from tableHR where Id='" & TextBox2.Text & "'", conn)

     

    dr = dc.ExecuteReader   '"the error code is here "ExecuteReader: Connection property has not been initialized"

     

    While (dr.Read())

    TextBox1.Text = dr.Item(

    "name")

     

    End While

     

    End If

    TextBox4.Focus()

     

    End If

     

    End Sub
    Thursday, June 30, 2011 6:46 AM

Answers

  • hi

    your problem is that u use a sub and you don call it to set the connection for the object command

    try to change the way like this 

     

     

     Dim con As New SqlConnection(GetConnectionString())
    
     Private Function GetConnectionString() As String
     Return System.Configuration.ConfigurationManager.ConnectionStrings("urconectionConnectionString").ConnectionString
     End Function
    
    Dim strQuery As String = ("Select * from tableHR where Id='" & TextBox2.Text & "'")
     Dim cmd As SqlCommand = New SqlCommand(strQuery)
     
     Dim con As New SqlConnection(GetConnectionStringI())
      cmd.Connection = con
    

     


    the getconnection functiion get the conection string from the app.config when you add a new datasource 

    so you will find it in this section

     

     <connectionStrings>
     <add name="urconectionConnectionString" connectionString="Data Source=pc;Initial Catalog=urbase;Integrated Security=True"
      providerName="System.Data.SqlClient" />
    


     




     

     


    Best Regards Please mark as answer if my post is helpful

    • Marked as answer by Jie Bao Thursday, July 7, 2011 9:12 AM
    Thursday, June 30, 2011 8:22 AM
  • Yes, you did not call CONNEC() to initiallize the connection object.

    YWeb shows the way retrieve connectionstring from App.Config using ConfigurationManager. You could also simply call CONNEC() in form load event.

     

    Best Regards,

     


    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jie Bao Thursday, July 7, 2011 9:12 AM
    Thursday, July 7, 2011 9:10 AM

All replies

  • hi

    your problem is that u use a sub and you don call it to set the connection for the object command

    try to change the way like this 

     

     

     Dim con As New SqlConnection(GetConnectionString())
    
     Private Function GetConnectionString() As String
     Return System.Configuration.ConfigurationManager.ConnectionStrings("urconectionConnectionString").ConnectionString
     End Function
    
    Dim strQuery As String = ("Select * from tableHR where Id='" & TextBox2.Text & "'")
     Dim cmd As SqlCommand = New SqlCommand(strQuery)
     
     Dim con As New SqlConnection(GetConnectionStringI())
      cmd.Connection = con
    

     


    the getconnection functiion get the conection string from the app.config when you add a new datasource 

    so you will find it in this section

     

     <connectionStrings>
     <add name="urconectionConnectionString" connectionString="Data Source=pc;Initial Catalog=urbase;Integrated Security=True"
      providerName="System.Data.SqlClient" />
    


     




     

     


    Best Regards Please mark as answer if my post is helpful

    • Marked as answer by Jie Bao Thursday, July 7, 2011 9:12 AM
    Thursday, June 30, 2011 8:22 AM
  • Yes, you did not call CONNEC() to initiallize the connection object.

    YWeb shows the way retrieve connectionstring from App.Config using ConfigurationManager. You could also simply call CONNEC() in form load event.

     

    Best Regards,

     


    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jie Bao Thursday, July 7, 2011 9:12 AM
    Thursday, July 7, 2011 9:10 AM