locked
VB: ExecuteReader: CommandText property has not been initialized RRS feed

  • Question

  • User1210869336 posted

    I went through many links and still stuck.  Can someone help me with the codes?

    1st question.  In code behind I want to check whether letters already exist.  So when a user enters a letter in the textbox "Letters" if a letter exists they'll get a warning from the label message "lblMsg".  If it doesn't exists then they'll get a congratulations. 

     

                cmdReader.Connection = con
                Dim myReader As SqlDataReader = cmdReader.ExecuteReader(CommandBehavior.CloseConnection)
                While myReader.Read()
                    Console.WriteLine(myReader.GetString(0))
                End While
                If myReader.HasRows Then
                    myReader = cmdReader.ExecuteReader()
                    Session("Letters") = Letters.Text
                    lblMsg.Text = "The letter already exists!"
                Else
                    lblMsg.Text = "Congratulations! The letter does NOT exist!"
                End If
                cmdReader.ExecuteReader.Close()
                cmdReader.ExecuteNonQuery()


    2nd question. On a separate note, is this a safe way of setting parameters so there's no sql injection?

     

    cmdReader.Parameters.Add("@Letters", SqlDbType.NVarChar).Value = Letters.Text.Trim()

    Thank you.

    Friday, April 5, 2013 6:41 PM

Answers

All replies

  • User-1716253493 posted
    you dont have sqlcommand yet, select * from yourtable where letter =@letter
    Friday, April 5, 2013 7:16 PM
  • User1210869336 posted

    Hi, I readjusted the code and still getting the error:

     

                cmdReader.Connection = con
                Dim myReader As SqlDataReader = cmdReader.ExecuteReader(CommandBehavior.CloseConnection)
                Dim mySQL As String = ""
                While myReader.Read()
                    Console.WriteLine(myReader.GetString(0))
                End While
                If myReader.HasRows Then
                    myReader = cmdReader.ExecuteReader()
                    mySQL = "SELECT Letters FROM Alphabets WHERE Letters = @Letters"
                    Session("Letters") = Letters.Text
                    lblMsg.Text = "The letter already exists!"
                Else
                    lblMsg.Text = "Congratulations! The letter does NOT exist!"
                End If
                cmdReader.ExecuteReader.Close()
                cmdReader.ExecuteNonQuery()

    Friday, April 5, 2013 7:28 PM
  • User1210869336 posted

    Still having issues

    Sunday, April 7, 2013 6:24 PM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 7, 2013 10:20 PM