-5

System.IndexOutOfRangeException: index was outside the bounds of the array c#... the code is attached below.

        List<dbcategory> complainList1 = new List<dbcategory>();
        string q = "select * from comp_detail where Roll_no ='" + RollNo + "'";

        var cmd = new SqlCommand(q, con);
        SqlDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            dbcategory db = new dbcategory();
            db.id = sdr[0].ToString();
            db.subject = sdr[1].ToString();
            db.Description = sdr[2].ToString();
            db.invName = sdr[3].ToString();
            db.category = sdr[4].ToString();
            db.picture = sdr[5].ToString();
            db.teacherRemarks = sdr[6].ToString();
            db.status = sdr[7].ToString();

            String date = sdr[8].ToString().Split(' ').ElementAt(0);
            db.RollNo = sdr[9].ToString();
            db.date = date;
            complainList1.Add(db);
        }

        con.Close();
        return complainList1;
Anas Alweish
  • 2,818
  • 4
  • 30
  • 44
  • Please upload the code , don't post it as image. – SH7 Oct 25 '18 at 06:19
  • I'm unable to copy and paste your code into Visual Studio. It seems that you have uploaded an image, and not posted the relevant code (text) into your question (with 4 spaces before each line so that SO formats it nicely). Because of this, I'm unable to debug it and find the error. In turn, this means I can't help you. – ProgrammingLlama Oct 25 '18 at 06:19
  • Hi there and welcome to Stack Overflow. We would really appreciate it when you take the time to put the code in the question and not as an image. It makes it hard te react on. – Peter Bons Oct 25 '18 at 06:20
  • also please upload the table structure, since you are using "select * " query. – SH7 Oct 25 '18 at 06:20
  • ahh... whats going on here – TheGeneral Oct 25 '18 at 06:31
  • I am unable to copy all the code here :( since i'm new here – Shahroz Ashraf Oct 25 '18 at 06:37
  • @ShahrozAshraf Welcome you in SO website, please take a look at [how to ask](https://stackoverflow.com/help/how-to-ask), – Anas Alweish Oct 25 '18 at 06:41
  • [How to Ask a Question on Stack Overflow](https://www.wikihow.com/Ask-a-Question-on-Stack-Overflow) – Anas Alweish Oct 25 '18 at 06:47
  • You only have to copy the code sufficient to demonstrate the problem (i.e. the code in your screenshot). – ProgrammingLlama Oct 25 '18 at 07:09

2 Answers2

0

The array is out of range, so you're placing more values inside the array than you've formerly set.

Steven
  • 1,996
  • 3
  • 22
  • 33
0

Here the possible issue is what you are calling in the sdr[index], the index itself is not there in your sdr (Data reader)

Titi
  • 65
  • 7