[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6881
  • Last Modified:

MSDE - Error - ExecuteReader: CommandText property has not been initialized

"An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll

Additional information: ExecuteReader: CommandText property has not been initialized"

Very confusing to me, as I am new to this database business!


here's the code: The error is reached on the ExecuteReader() line.



        'SQL junk
        startTime = DateTime.Now
        results.Text = results.Text & _
        "SQL ---- 6 queries of 6 stocks between 85 & 04: " & vbCrLf

        Dim connect_string As String = "User Id=sa;Password=password;Data Source=ULTRA\NET"
        db_connection = New SqlConnection(connect_string)
        db_connection.Open()

        Dim sql_command As New SqlCommand
        sql_command.CommandType = CommandType.StoredProcedure
        sql_command.Connection = db_connection


        Dim Par As SqlParameter = New SqlParameter("@symbol", SqlDbType.VarChar, 6)
        Par.Value = Trim("INTC")
        sql_command.Parameters.Add(Par)

        SqlDR = sql_command.ExecuteReader()
        Dim s As String

        While dr.Read()
            s = dr(0)
            f = dr(1)
        End While




My Stored Procedure looks like this in Enterprise Manager:

"CREATE PROCEDURE prs_GetData
            @symbol varchar(6)
AS
          SELECT * FROM SOMETHINg WHERE SYMBOL  = @symbol
Return
GO"



Any ideas? I imagine I'm missing a line of code in the ExecuteReader() area

Thanks!
0
oxygen_728
Asked:
oxygen_728
  • 2
3 Solutions
 
Bob LearnedCommented:
sql_command.CommandText = "<stored procedure name goes here>"

Bob
0
 
Bob LearnedCommented:
A better way to read data is this, so you can move fields around and not worry about ordinal position.

While dr.Read()
            s = dr("symbol")
            f = dr("field")
        End While

Bob
0
 
Level10AccessCommented:
Just do this:

 Dim sql_command As New SqlCommand([Stored Procedure Name],db_connection)

        sql_command.CommandType = CommandType.StoredProcedure
0
 
oxygen_728Author Commented:
The problem was me not specifying which command procedure for the sql_command to use!

Thanks for the help both of you!

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now