Fill: SelectCommand.Connection property has not been initialized. RRS feed

  • Question

  • please i need help
    i wrote the below code and gave me an fill:selectCommand.connection property has not been initializded.
    please help me urgent :(

    cmd =

    New OleDbCommand("select pid as [Patient ID], pname as [Patient Name], pgender as Gender, premarks as Remarks from patient_tb , con")

    myda =

    New OleDbDataAdapter(cmd)

    mydataset =

    New DataSet()


    "MyTable")                          (the error is in this line)

    DataGridView1.DataSource = mydataset.Tables(


    Fill: SelectCommand.Connection property has not been initialized.
    Tuesday, August 25, 2009 10:26 PM


  • As From the code snippet , it is obvious that the connection string is not properly initialized. from the above OleDbCommand your passing the connection as a string with Query that is wrong.
    there are two ways to initialize connection string pass the oledbConnection or connection string in OleDbCommand with two constructor parameters one is Query and the other is connection.
    If you don't pass the connection in oleDbCommand the pass the Connection in OleDbAdapter with OleDbCommand in Adapter Connection i thing it working fine.
    before executing the command first check the connection state is opened successfully , executed command and close the connection..


    • Marked as answer by Kira Qian Tuesday, September 1, 2009 7:16 AM
    Wednesday, August 26, 2009 12:30 AM