Results 1 to 8 of 8

Thread: Connection property has not been initialized

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    469

    Connection property has not been initialized

    Hello

    In some posts I have read on other forums, those with database connection problems (I am thinking about Access) sometimes get the error: 'Connection property has not been initialized' which might translate as 'you have not kick-started your connection'. One reason for that, according to a number of replies I have come across, is that 'you have not set the connection property of the command object' ('you have not told command how to connect').

    So would something like this:

    Code:
    Dim conn As New OleDbConnection
    Dim OleDbConnection As New OleDbConnection
    Dim cmd As New OleDbCommand
    
    conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|myDatabase.mdb;"
    
    conn.Open()
    
    cmd = New OleDbCommand("SELECT strName FROM school WHERE strName=@strName", conn)
    
    cmd.Parameters.AddWithValue("@strName", UserName.Text)
    
    cmd.Connection = conn
    
    cmd.ExecuteNonQuery()
    
    conn.Close()
    a) initialise the connection property and b) set the connection property of the command object?

    Many thanks.

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Connection property has not been initialized

    a) yes, b) yes, but since you passed it to the constructor there's no need to set it again.
    BUT...

    I wouldn't use ExecuteNonQuery in this case. The ExecuteNonQuery is for action non-query queries... Inserts, Updates, Deletes... queries that do not return data. Your query does, so it should use the Execute method and return the data.

    Also, look into using the Using clause....

    Code:
    Using conn As New OleDbConnection
        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|myDatabase.mdb;"
        Using cmd As New OleDbCommand("SELECT strName FROM school WHERE strName=@strName", conn)
            cmd.Parameters.AddWithValue("@strName", UserName.Text)
            conn.open()
            cmd.ExecuteNonQuery() <-- obviously, change this as previously mentioned.
        End Using
    End Using
    -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??? *

  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: Connection property has not been initialized

    @TG: Execute? Shouldn't it be ExecuteReader?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Connection property has not been initialized

    Probably. I don't use ADO.NET directly much any more - I have to go through our own data access assemblies. And as I think about it, Execute is off the Adaptor isn't it?

    -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??? *

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    469

    Re: Connection property has not been initialized

    Many thanks, TG for showing the Using clause. I will try it.

    Is it really correct to say that the code I posted (as you did with 'Using') 'returns data'? Doesn't it just check verify that one email address (the one typed in by the user) corresponds to another email address (the one the user registered with and lives in the database table).

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Connection property has not been initialized

    Of course it returns data ... that's what the SELECT statement does... selects data out of a table and returns it.

    -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??? *

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    469

    Re: Connection property has not been initialized

    I know what you mean - it selects data and uses that data to perform other actions such as, in this case, to send an email. I suppose I meant 'display it'.

  8. #8
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Connection property has not been initialized

    The display of data has nothing to do with selecting it from a database.

    What TG was saying was if you are running a query against a database which uses a SELECT clause e.g.

    SELECT COUNT(c.email) From Contact c WHERE c.email = 'emailaddress'

    Then you would use the ExecuteReader command as , if on the other hand you are carrying out Delete, Update or Insert command then you use ExecuteNonQuery as these operations do not return you a dataset. (although they can return you a return parameter which some use to return a record count or the inserted record id for instance.)
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



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