CommandText Property has not been initialized

Posted on 2007-10-16
Last Modified: 2008-01-09
Dear all,

I m trying to populate a datagridview with data from oracle like below.
Everytime i get the same error : CommandText Property has not been initialized
could anybody tell me what i do wrong here. thanks..

   Sub exe_sql()
            Dim sql, conn As String
            Dim dt As New DataTable
            objgrd = New Datagrid
            objsql = New SQL

            conn = "DATA SOURCE= " & txt_serv.Text & ";user id= " & txt_user.Text & ";password= " & txt_pass.Text & ";"
            sql = objsql.txt_sql.Text

            Dim oracon As New OracleConnection(conn)
            Dim oraadap As New OracleDataAdapter(sql, conn)
            With oraadap.SelectCommand
                .CommandType = CommandType.Text
                .CommandText = sql
                .Connection = oracon
            End With


            objgrd.Dock = DockStyle.Fill
            objgrd.grd_outcome.DataSource = dt


        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2, MessageBoxOptions.DefaultDesktopOnly)
        End Try

    End Sub
Question by:SES_Team
    LVL 20

    Accepted Solution

    this line

    .CommandText = sql

    and this line

    sql = objsql.txt_sql.Text

    make sure that sql is not null before you get into .CommandText = sql as it would appear that it is.  You need to set the variable sql with some commandtext like an SQL select statement or Stored procedure name.  With any necessary mods


    Author Comment

    Andrew you're right. I made a little mistake at this part:

       Sub exe_sql()
                Dim sql, conn As String
                Dim dt As New DataTable
                objgrd = New Datagrid
                objsql = New SQL

    because everytime it starts with a new objsql it cleares out the text box you fill in after pressing the execute button.

    I already found this out myself but since you give the good anwser i will give you the points.

    Thanks anyway

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now