locked
ExecuteReader: CommandText property has not been initialized RRS feed

  • Question

  • User-1892921829 posted

     I am sure this is something very simple that I have been staring at too long and am missing... Please help!!!

     

    getting error "ExecuteReader: CommandText property has not been initialized"

            If IsValid Then
                Dim strSQL As String
                Dim strSalesPersonSelect As String
                Dim strNoteTypeSelect As String
                Dim Conn As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("DBConnString").ConnectionString)
                Dim Cmd As New Data.SqlClient.SqlCommand(strSQL, Conn)
                Dim Dr As Data.SqlClient.SqlDataReader
    
                If Me.ddlNoteType.SelectedValue <> "" Then
                    strNoteTypeSelect = " AND n.NoteTypeId = @NoteTypeId"
                    Cmd.Parameters.Add("@NoteTypeId", Data.SqlDbType.Int).Value = Me.ddlNoteType.SelectedValue
                Else
                    strNoteTypeSelect = ""
                End If
    
    
                Dim mu As String = HttpContext.Current.User.Identity.Name
                Dim pc As ProfileCommon = Profile.GetProfile(mu)
    
                If pc.SalesPersonId = "na" Then
                    If Me.ddlSalesPerson.SelectedValue <> "" Then
                        strSalesPersonSelect = " AND c.SalesPersonId = @SalesPersonId"
                        Cmd.Parameters.Add("@SalesPersonId", Data.SqlDbType.Int).Value = Me.ddlSalesPerson.SelectedValue
                    Else
                        strSalesPersonSelect = ""
                    End If
                Else
                    Cmd.Parameters.Add("@SalesPersonId", Data.SqlDbType.Int).Value = Me.lblSalesPersonId.Text
                End If
    
                Cmd.Parameters.Add("@BeginingDate", Data.SqlDbType.SmallDateTime).Value = dateBegining.Date
                Cmd.Parameters.Add("@EndingDate", Data.SqlDbType.SmallDateTime).Value = dateEnding.Date
    
                strSQL = "SELECT n.*, c.CompanyName, sp.FirstName + ' ' + sp.LastName AS SalesPersonName FROM CustomerNote n INNER JOIN Customer c ON n.CustomerId = c.CustomerId INNER JOIN SalesPerson sp ON c.SalesPersonId = sp.SalesPersonId WHERE n.NoteDate BETWEEN @BeginingDate AND @EndingDate" & strNoteTypeSelect & strSalesPersonSelect
    
                Conn.Open()
                Dr = Cmd.ExecuteReader
                gv.DataSource = Dr
                gv.DataBind()
                Dr.Close()
                Conn.Close()
            End If


     

    Friday, October 16, 2009 11:11 AM

Answers

  • User-1360095595 posted

    You need to set the CommandText property of your command properly.  You seem to be setting it to strSql, but then you change strSql later in your code.  So I suggest you set the Commandtext Property just before you open the connection.  Set it to strSql.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 16, 2009 1:15 PM

All replies

  • User-1360095595 posted

    You need to set the CommandText property of your command properly.  You seem to be setting it to strSql, but then you change strSql later in your code.  So I suggest you set the Commandtext Property just before you open the connection.  Set it to strSql.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 16, 2009 1:15 PM
  • User-1892921829 posted

    you are awesome - thank you so much!!! 

    Friday, October 16, 2009 2:12 PM