13 Replies - 1994 Views - Last Post: 18 August 2011 - 10:53 AM Rate Topic: -----

#1 madmorgan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 07-May 10

Index is out of range when its not

Posted 18 August 2011 - 10:00 AM

Hello,

I have a data base with 10 colloms.
0= ID
1= Title
2= Year
3= dir
4= action
5= plot
6= runtime
7= imagelocation
8= image
9= star

SqlDataReader read = new SqlDataReader();
index number = read[9].tostring();

when I try to read collom 9 to get an it I get index out of range. im using SQL 2008 Express if that makes any differeance.
Is This A Good Question/Topic? 0
  • +

Replies To: Index is out of range when its not

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9223
  • View blog
  • Posts: 34,630
  • Joined: 12-June 08

Re: Index is out of range when its not

Posted 18 August 2011 - 10:05 AM

Just a heads up - the indexing of the reader is for each SQL statement that is processed.

Since you only had one select statement you'll need reader[0] and then index your columns from that.
Was This Post Helpful? 0
  • +
  • -

#3 madmorgan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 07-May 10

Re: Index is out of range when its not

Posted 18 August 2011 - 10:08 AM

index 0 only returns the id number i want to read from index 9 and get that number
Was This Post Helpful? 0
  • +
  • -

#4 paradoxia  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 163
  • Joined: 02-July 11

Re: Index is out of range when its not

Posted 18 August 2011 - 10:09 AM

I can't see the query

I got this from the msdn library, it uses sqldatareader object.


public void ReadMyData(string myConnString) {
    string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
    SqlConnection myConnection = new SqlConnection(myConnString);
    SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
    myConnection.Open();
    SqlDataReader myReader;
    myReader = myCommand.ExecuteReader();
    // Always call Read before accessing data.
    while (myReader.Read()) {
       Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
    }
    // always call Close when done reading.
    myReader.Close();
    // Close the connection when done with it.
    myConnection.Close();
 }



This post has been edited by paradoxia: 18 August 2011 - 10:10 AM

Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Index is out of range when its not

Posted 18 August 2011 - 10:09 AM

I have no idea what modi123_1 is talking about.

Given that, you do need to actually read first, as in
SqlDataReader read = new SqlDataReader();
read.Read();
index number = read[9].tostring();

And you do need to attach it to a database.

This post has been edited by Momerath: 18 August 2011 - 10:10 AM

Was This Post Helpful? 0
  • +
  • -

#6 paradoxia  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 163
  • Joined: 02-July 11

Re: Index is out of range when its not

Posted 18 August 2011 - 10:13 AM

View Postmodi123_1, on 18 August 2011 - 10:05 AM, said:

Since you only had one select statement you'll need reader[0] and then index your columns from that.


Where is that select statement?
Was This Post Helpful? 0
  • +
  • -

#7 madmorgan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 07-May 10

Re: Index is out of range when its not

Posted 18 August 2011 - 10:14 AM

ok where is my select code.

public void SelectAll(FlowLayoutPanel flowLayoutPanel)
        {
try
            {
                Connection = new SqlConnection();
                Connection.ConnectionString = DatabaseString;

                //Command = new SqlCommand("(SELECT * FROM COLLECTION)", Connection);
                Command = new SqlCommand("(SELECT ID, TITLE, YEAR, DIRECTOR, FILMGENRE, PLOT, RUNTIME, IMAGEURL, IMAGE FROM COLLECTION)", Connection);

                if (Connection.State != ConnectionState.Open)
                {
                    Connection.Open();
                }
                
                SQLRead = Command.ExecuteReader();
                while (SQLRead.Read())
                {
                    AddDVDItem(flowLayoutPanel);
                }
            }
            catch (Exception EX)
            {
                Exceptionerror(EX);
            }
            Connection.Close();
}


private void AddDVDItem(FlowLayoutPanel flowLayoutPanel)
        {
            DVDItem Item = new DVDItem();

            Item.ID = Convert.ToInt32(SQLRead[0].ToString());
            Item.Name = SQLRead[1].ToString();
            Item.DVDTitle.Text = SQLRead[1].ToString();
            Item.DVDYear.Text = "Year: " + SQLRead[2].ToString();
            Item.DVDRunTime.Text = "Length: " + SQLRead[6].ToString() + " Mins";
            Item.DVDPlot.Text = SQLRead[5].ToString();

            if (!DBNull.Value.Equals(SQLRead[8]))
            {
                byte[] picbyte = (byte[])SQLRead[8];
                MemoryStream ms = new MemoryStream(picbyte);
                Item.DVDImage.Image = Image.FromStream(ms);
                ms.Close();
            }
            else
            {
                Item.DVDImage.ImageLocation = SQLRead[7].ToString();
            }

            int stars = SQLRead[9].ToString();
            MessageBox.Show(SQLRead[9].ToString());

            if (stars == 0)
            {
                
            }
            else if (stars == 1)
            {
                Item.Star1.Image = Properties.Resources.knewstuff_32;
            }
            else if (stars == 2)
            {
                Item.Star1.Image = Properties.Resources.knewstuff_32;
                Item.Star2.Image = Properties.Resources.knewstuff_32;
            }
            else if (stars == 3)
            {
                Item.Star1.Image = Properties.Resources.knewstuff_32;
                Item.Star2.Image = Properties.Resources.knewstuff_32;
                Item.Star3.Image = Properties.Resources.knewstuff_32;
            }
            else if (stars == 4)
            {
                Item.Star1.Image = Properties.Resources.knewstuff_32;
                Item.Star2.Image = Properties.Resources.knewstuff_32;
                Item.Star3.Image = Properties.Resources.knewstuff_32;
                Item.Star4.Image = Properties.Resources.knewstuff_32;
            }
            else if (stars == 5)
            {
                Item.Star1.Image = Properties.Resources.knewstuff_32;
                Item.Star2.Image = Properties.Resources.knewstuff_32;
                Item.Star3.Image = Properties.Resources.knewstuff_32;
                Item.Star4.Image = Properties.Resources.knewstuff_32;
                Item.Star5.Image = Properties.Resources.knewstuff_32;
            }
flowLayoutPanel.Controls.Add(Item);
        }


This post has been edited by madmorgan: 18 August 2011 - 10:20 AM

Was This Post Helpful? 0
  • +
  • -

#8 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Index is out of range when its not

Posted 18 August 2011 - 10:22 AM

Count your select columns, there are only 9.
Was This Post Helpful? 1
  • +
  • -

#9 madmorgan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 07-May 10

Re: Index is out of range when its not

Posted 18 August 2011 - 10:29 AM

im only pulling out a few bits of information but there are 10 colloms as I told you at the top but I cant read index 9, index 9 would = collom 10 (Star)
how can I upload a Image and ill show you my data.

ok here is a image of my database

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#10 Bloosdhed  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 22
  • Joined: 18-August 11

Re: Index is out of range when its not

Posted 18 August 2011 - 10:31 AM

Seems you don't retrieve your action er star column.

SELECT
(0)ID,
(1)TITLE,
(2)YEAR,
(3)DIRECTOR,
(4)FILMGENRE,
(5)PLOT,
(6)RUNTIME,
(7)IMAGEURL,
(8)IMAGE
<(9) MISSING>

FROM COLLECTION

This post has been edited by Bloosdhed: 18 August 2011 - 10:34 AM

Was This Post Helpful? 1
  • +
  • -

#11 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 267
  • View blog
  • Posts: 1,476
  • Joined: 07-April 08

Re: Index is out of range when its not

Posted 18 August 2011 - 10:32 AM

View Postmadmorgan, on 18 August 2011 - 12:29 PM, said:

//Command = new SqlCommand("(SELECT * FROM COLLECTION)", Connection);  

Command = new SqlCommand("(SELECT ID, TITLE, YEAR, DIRECTOR, FILMGENRE, PLOT, RUNTIME, IMAGEURL, IMAGE FROM COLLECTION)", Connection);  



ok lets take a look at your select statement.

First lets count the columns
1. ID
2. Title
3. Year
4. Director
5. FilMGenre
6. Plot
7. RunTime
8. ImageURL
9. Image

You are missing a column in this statment which is why it is saying index out of range. Add in your missing column to the select statement and it should pull it just fine.
Was This Post Helpful? 1
  • +
  • -

#12 madmorgan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 07-May 10

Re: Index is out of range when its not

Posted 18 August 2011 - 10:34 AM

rgfirefly24 you are right I completly over looked that line. I feel so bad because of this silly error.
Was This Post Helpful? 0
  • +
  • -

#13 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 267
  • View blog
  • Posts: 1,476
  • Joined: 07-April 08

Re: Index is out of range when its not

Posted 18 August 2011 - 10:39 AM

don't feel bad, we all make these kinds of mistakes. Sometimes it just takes a 2nd set of eyes to find it.
Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: Index is out of range when its not

Posted 18 August 2011 - 10:53 AM

This is why I hate dicking around with index numbers. Use the column header name and simplify your life.

var connStr = Settings.Default.TestConnectionString;
var select = "select Id, Name, Created from Person";
using (var conn = new SqlConnection(connStr))
using (var cmd = new SqlCommand(select, conn)) {
    conn.Open();
    var reader = cmd.ExecuteReader();
    while(reader.Read()) {
        Console.WriteLine((string)reader["Name"]);
        Console.WriteLine((DateTime)reader["Created"]);
    }
}



Notice on lines 8 and 9, I'm using the column names rather than their ordinal number. Much, much simpler. Of course, this is a "magic string", but you could handle that with some config values.
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1