Sunday 23 September 2012

System.IndexOutOfRangeException:Index was outside the bounds of the array


Here I Got An Exception i.e., System.IndexOutOfRangeException:Index was outside the bounds of the array. In The Below Shown Image It Is Showing Exception At "System.Data.OleDb.OleDbDataReader.DoValueCheck(Int32 ordinal)" and At System.Data.OleDb.OleDbDataReader.GetValue(Int32 ordinal)" and At the Event Name "button1_click(Object sender,EventArgs e)"..

I Tried To Retrive The Data From The Database To The TextBox "txtTotalQty" At "Total Quantity" Label Which Is At Bottom Of The Form. But I Got The "System.IndexOutOfRangeException" Exception


Why I Got Exception ? Becuase From The Below Code I Had Shown That I Am Retrieving Only Single Field From The Database, While I Am Retrieving Using DataReader  I Had Put "dr.GetValue(3)"-My Mistake Is That I Had Thought That The Column Or Field "Qty" That I Am Retrieving From the Database Is   3rd Column In The Database..So What I Had Thought Is Mistake. 



After The Debugging I Recognize That While I Am Retrieving A Single Column or Field Value I Have To Give "zero"i.e.,'0'...If  I Am want To Retrieve 5 Columns Values, Then I Have To Give as Below:
textBox1.Text=dr.GetValue(0).ToString(0);
textBox2.Text=dr.GetValue(1).ToString(0);
textBox3.Text=dr.GetValue(2).ToString(0);
textBox4.Text=dr.GetValue(3).ToString(0);
textBox5.Text=dr.GetValue(4).ToString(0);

The Retrieved Data Is displayed In The Particular TextBoxes As We Assigned To The TextBoxes Respectively.
See The Below Code After The Exception Is Rectfied i.e., Error Is Fixed.



Now The Form Is Executed SuccessFully. See The Bottom Red Marked Box For The Successful Result.


No comments:

Post a Comment

Note: only a member of this blog may post a comment.