Results 1 to 4 of 4

Thread: Dazed and Confused!

Hybrid View

  1. #1

    Thread Starter
    Junior Member w8taminute's Avatar
    Join Date
    Aug 2006
    Posts
    24

    Dazed and Confused!

    Hello everyone, and happy holidays,
    I have encountered a problem and I am trying to find a work around do to time constraints. Here is the code that I am running:

    strData = "Select Min(num1) as MinVal from Table1"
    strReader = strCmd.ExecuteReader
    While strReader.Read
    varMin = strReader.Item("MinVal")
    End While
    strReader.Close()



    When I run the code I get the IndexOutOfRange Exception. I get this exception due to the fact that Min(num1) is a negative value. It is supposed to be and will be negative the majority of the time. Does anyone know how I can get around this?

  2. #2
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Dazed and Confused!

    I don't see where you are declaring your variables and objects, so I will assume you have declared and initialized them correctly. Try this:

    VB Code:
    1. strData = "SELECT Min(num1) FROM Table1"
    2. strReader = strCmd.ExecuteReader
    3. While strReader.Read
    4. varMin = strReader.Item(0)
    5. ' If varMin is a string then use varMin = strReader.Item(0).ToString
    6. End While
    7. strReader.Close()

    More of your code is needed to help beyond that.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Dazed and Confused!

    You shouldn't be using a DataReader to get a single value. You should be calling the ExecuteScalar method of your Command. It will return an Object reference that you can cast as an Integer using CInt.

    Now, there is nothing in that code that will throw an IndexOutOfRangeException as far as I can see. Are you using varMin somewhere later in your code, as an index into an array or collection? That's where that type of exception is likely to be thrown. If that's the case than your logic must be flawed because you can't possibly have a negative index in an array or collection.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Junior Member w8taminute's Avatar
    Join Date
    Aug 2006
    Posts
    24

    Re: Dazed and Confused!

    Thanks Guys, it's working now. I greatly appreciate it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width