InfalidOperationExeption : ExecuteNonQuery: Connection property has not been initialized.

Заблокировано InfalidOperationExeption : ExecuteNonQuery: Connection property has not been initialized.

  • 17 марта 2012 г. 2:32
     
      С кодом

    Hi there, i just learn about VB.Net in the last 3 months...when i try to create a Sql server to VB connection , that message appear..i've no idea what is that...this is my script:

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    
    
            Dim myConnection As SqlConnection = New SqlConnection
    
            myConnection = New SqlClient.SqlConnection()
    
            myConnection.ConnectionString = "Data Source=user-pc\sqlexpress; Initial Catalog=C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\SPEC_MEM.MDF;Integrated Security=True"
    
            myConnection.Open()
    
            Dim adapter1 As New SqlClient.SqlDataAdapter
            Dim command1 As New SqlClient.SqlCommand
                                                            
            command1 = New SqlClient.SqlCommand("Select * from dbo.member_A where Name = '__%'")
    
            If TextBox1.Text <> "" Then
                command1.ExecuteNonQuery()
            End If
            myConnection.Close()
    
        End Sub
    Need ur help please...thankyou

Все ответы

  • 17 марта 2012 г. 7:49
     
     Отвечено С кодом

    I don't see where you have tied the Connection to the Command object

    try this

    command1 = New SqlClient.SqlCommand("Select * from dbo.member_A where Name = '__%'", myConnection)
    

    also, normally when doing this in a TextBox TextChanged event you would use Where Name LIKE, and then you would add a command parameter using the TextBox Text. Something like:

     command1 = New SqlClient.SqlCommand("SELECT * FROM dbo.member_A WHERE Name LIKE @Name, myConnection)
    
     command1.Parameters.AddWithValue("@Name", TextBox1.Text & "%")

  • 17 марта 2012 г. 11:57
     
     

    check this link

    http://social.msdn.microsoft.com/forums/en-US/adodotnetdataproviders/thread/d6a74623-89b0-4b9a-ad07-4b8e1a79015c/