-1

i know its been asked alot and i have looked alot into this but all the solution that are given i have used but nothing seems to work for my case ,, so i thought to post my solution that is not working here .. i have comboboxes on form and i am selecting the difficulty level for questions to display from datatable ,, it seems to be working fine then suddenly message box pops up showing there is no row position at 0 , then sometimes 7 15 etc .. the datatable is filled then why my array is not positioning at rows. plz help

private void Diff_SelectedIndexChanged_1(object sender, EventArgs e)
    {
          using (var conn = new SqlConnection(constring))
        {
            SqlCommand cmd = new SqlCommand(cmdText, conn);

            try
            {
                conn.Open();
                SqlDataAdapter sd = new SqlDataAdapter(cmdText, conn);
                sd.Fill(quest_);



               if (quest_.Rows.Count > 0)


                {
                    PopulateQuestion(0);








           catch (Exception exp)
            {

                MessageBox.Show(exp.ToString());
            }
            finally
            {
                //con.Close();
            }

}

exception "An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll

Additional information: There is no row at position 16."it runs fine for sometime but then this exception

S B
  • 9
  • 5
  • Might be typo, but you're missing an leading '@' in value-assignment for `subj`. On next-click you're using `q+1`, but `q` is not incremented and on difficulty or subject change its not reset to 0. – Michael Oct 01 '17 at 13:34
  • i have canged a code letme post that one now – S B Oct 01 '17 at 13:36
  • 1
    It would help a lot if you could say exactly where this error is happening. Also if this is an exception being thrown then you should give details of the exception (eg the type and possibly the stack trace) not just the message. – Chris Oct 01 '17 at 13:41
  • ok exception is over where i am loading question in textbox exeception says"system.index out of rangeexception thre is no wor position at 10" at system.data.rbtree1.getnodebyindex(int32 user Index)\at difflevel_selected index changed_1 – S B Oct 01 '17 at 13:43
  • i have done debugging .. visualizer shows the rows it has rows but error popping up ..count is set there is data – S B Oct 01 '17 at 13:45
  • Your code changes do not make any sense. There is a `catch` without a `try`. Please format your code and add the exception from your comment formatted to your post. Have you verified that your `DataTable`s `DataRows` are containing valid data at the requested index in your `PopulateQuestion` method? – Michael Oct 01 '17 at 14:18
  • o .. i will format it accordingly .. but it is returning rows ,, rows count says that thre r 10 rows to return but i see only the exception it doesnt display the rows sometimes it happens on b some times i and the suddenly on A ..(these r levels in my combo) – S B Oct 01 '17 at 14:26
  • Why are you using var dr = cmd.ExecuteReader();? You should be using a DataAdapter. See msdn : https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter(v=vs.110).aspx – jdweng Oct 01 '17 at 14:27
  • i was using adapter but exception was there i have used it again it works fine for sometime then again the same thing – S B Oct 01 '17 at 14:48
  • I think that you sql-data might be faulty so I suggest that you fill your `DataTable` instance manually by iterating the result in the `IDataReader`. That way you get the specific `DataRow` (and more error details) that causes this trouble and can correct the data. – Michael Oct 01 '17 at 15:26
  • and now in addition to the present problem it shows 10 rows but return only 4 same rows again and again..... with a exception in bonus its messed up .. plz help i am devastated – S B Oct 01 '17 at 15:36
  • how to do that manually .. actually i am stuck on this from couple of days and posted everywhere .. now i dont have time to apply any method just few hours left for presentation .. n m all screwd up – S B Oct 01 '17 at 15:38
  • Well, if it sometimes works and sometimes not i think the problem is not the code we can see here... Might be the data in the table, might be something else. You can read this on how to fill a DataTable manually: https://stackoverflow.com/questions/8370927/how-do-i-loop-through-rows-with-a-data-reader-in-c – Michael Oct 01 '17 at 16:42
  • ohkk this method... but this on my code it doesnt do anything at all as i cant navigate throuh by using this method that is why i ahve used the other one mentioned above .. but .. alas| – S B Oct 01 '17 at 16:47

1 Answers1

0

i have solved it .. just incremented the index from 0 to one n=and it worked

S B
  • 9
  • 5