0

Trying to get response from my azure db I am using the recommended microsoft snippet

Dim sb As StringBuilder = New StringBuilder()
sb.Append("select top 1 runname from vNameCode")
'sb.Append("where untgscod = ")
Dim sql As String = sb.ToString()
Using command As SqlCommand = New SqlCommand(sql, connection)
    Using reader As SqlDataReader = command.ExecuteReader()
        While reader.Read()
            Console.WriteLine("{0} {1}", reader.GetString(0), reader.GetString(1))
        End While

but as soon as I try to read the values I get the error:

System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

I get an object back but just cannot read it in the recommended way. Any ideas what I am doing wrong?

1 Answer 1

1

Since you are getting runname from the table vNameCode but you are trying to access two values from the reader.

Do either of one changes:

  1. Include the corresponding values in the SELECT statement.
  2. Remove reader.GetString() from the Console.WriteLine

Try Below

Dim sb As StringBuilder = New StringBuilder()
sb.Append("select top 1 runname from vNameCode")     
Dim sql As String = sb.ToString()
Using command As SqlCommand = New SqlCommand(sql, connection)
    Using reader As SqlDataReader = command.ExecuteReader()
        While reader.Read()
            Console.WriteLine("{0}", reader.GetString(0))
        End While
    End Using
End Using
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.