1

enter image description here

I have been trying to get it working for almost an hour now and cannot make any sense why it is not working. Please help!! This is my code

SqlDataReader rs = cmdGet.ExecuteReader();
rs.Read();
var id = Convert.ToInt32(rs["id"]);
var mt = rs["mt"].ToString();

The SQL statement is this:

  SELECT TOP 1 
      id = 0,
      mt = MT
  FROM 
      MyTable
  ORDER BY 
      2

I id = 0 and the code breaks at mt line. Throws:

System.IndexOutOfRangeException

What's wrong? I looked at a lot of posts but could not resolve it.

10
  • id = 0? Huh? What is that doing? Have you tried running that query in something like SSMS to see what you get? Commented Feb 10, 2017 at 22:35
  • if you run the query what do you will get? Commented Feb 10, 2017 at 22:38
  • What does your table actually look like? It difficult to discern exactly what you are trying to do in your current query. Commented Feb 10, 2017 at 22:38
  • which one if field in you table mt or MT? Commented Feb 10, 2017 at 22:41
  • MT is the table Commented Feb 10, 2017 at 22:41

1 Answer 1

1

the exception said that you have no field named mt so you need to change your query into this and it should work:

SELECT TOP 1 id, mt
FROM MyTable
 WHERE id = 0
ORDER BY 2
Sign up to request clarification or add additional context in comments.

1 Comment

my pleasure to help you :)

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.